OliverJ90
OliverJ90

Reputation: 1321

Proper implementation of a for loop

I need to basically repeat the below for through to a10 and g10. So I could theoretically continue as below and end up with a ton of duplication but I know that's terrible practice. What's the best way to streamline this?

function enterall() {


    var a1 =    localStorage.getItem('a1');
    var b1 =    localStorage.getItem('b1');
    var c1 =    localStorage.getItem('c1');
    var d1 =    localStorage.getItem('d1');
    var e1 =    localStorage.getItem('e1');
    var f1 =    localStorage.getItem('f1');
    var g1 =    localStorage.getItem('g1');

    var a2 =    localStorage.getItem('a2');
    var b2 =    localStorage.getItem('b2');
    var c2 =    localStorage.getItem('c2');
    var d2 =    localStorage.getItem('d2');
    var e2 =    localStorage.getItem('e2');
    var f2 =    localStorage.getItem('f2');
    var g2 =    localStorage.getItem('g2');



db.transaction(function (tx) {

               tx.executeSql("INSERT INTO mytable (id, item, code, val, val2, val3, val4) VALUES (?,?,?,?,?,?,?)", [a1, b1, c1, d1, e1, f1, g1], function (tx, results){

                             alert('records in');

                             });
              });


db.transaction(function (tx) {

               tx.executeSql("INSERT INTO mytable (id, item, code, val, val2, val3, val4) VALUES (?,?,?,?,?,?,?)", [a2, b2, c2, d2, e2, f2, g2], function (tx, results){

                             alert('records in');

                             });
              });
};

Upvotes: 0

Views: 50

Answers (4)

FreeAsInBeer
FreeAsInBeer

Reputation: 12979

You should probably use nested for loops and an array to contain your values:

var values = new Object();
var letters = ["a", "b", "c", "d", "e", "f", "g"];

letters.forEach(function(letter) {
    for (var i = 1; i <= 10; i++) {
        values[letter + i] = localStorage.getItem(letter + i);
    }
});

Upvotes: 1

mr_greb
mr_greb

Reputation: 208

If you don't want to create an array you can also use this:

for(var num=1; num <= 10; num++) {
    var arr = [];
    for(var letter = 'a'.charCodeAt(0); letter <= 'g'.charCodeAt(0); letter ++) {
        arr.push(localStorage.getItem(String.fromCharCode(letter) + num));
    }
    db.transaction(function (tx) {
        tx.executeSql("INSERT INTO mytable (id, item, code, val, val2, val3, val4) VALUES (?,?,?,?,?,?,?)", arr, function (tx, results){
            alert('records in');
        });
    });
}

Upvotes: 0

t1nr2y
t1nr2y

Reputation: 656

In addition to the prior answers, I would also suggest using a loop for the db.transaction function. Both of these would be better off as separate functions, called from within your enterall() function. Another suggestion I would have is make these into objects, both when adding to localStorage and then taking them out, to make your code a bit cleaner as well.

Upvotes: 0

gitaarik
gitaarik

Reputation: 46350

Try something like this:

var results = {'first': [], 'second': []};
var letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];

for (var i = 0; i < 7; i++) {
    results['first'].push(localStorage.getItem(letters[i] + '1'));
    results['second'].push(localStorage.getItem(letters[i] + '2'));
}

Upvotes: 0

Related Questions