0

Here is the code, when I run it it is going to infinite loop, any flaws in code?

function s4() {
  return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
};

function guid() {
  return [s4(), s4(), '-' , s4(), '-', s4(), '-', s4(), '-', s4(), s4(), s4()].join('');
}
function randomFromInterval(from,to){
    return Math.floor(Math.random()*(to-from+1)+from);
}
function get_random_color() {
    var letters = '0123456789ABCDEF'.split('');
    var color = '#';
    for (var i = 0; i < 6; i++ ) {
        color += letters[Math.round(Math.random() * 15)];
    }
    return color;
}
var data = new Array(500), types = ["date", "slider", "select", "string", "number", "rating", "level", "title"],
    allowModify = [true, false], str = "Cell";
for(var i = 0; i < 500; i++){
    var arr = new Array(100);
    for(var j = 0; j < 100; j++){
        var obj = {};
        obj.id = guid();
        obj.role = types[randomFromInterval(0, 6)];
        obj.selected = "";
        obj.writable = allowModify[randomFromInterval(0, 1)];
        switch(obj.role){
            case "slider":
                obj.range = [0, randomFromInterval(0, 100)];
                obj.text = 0;
                break;
            case "select":
                var num = randomFromInterval(2, 4), _options = Array(num);
                for(var opt = 0; opt < num; opt += 1 ){
                    _options[opt] = {
                        id : guid(),
                        text : ["Option[", opt + 1, "]"].join('')
                    }
                }
                obj.options = _options;
                obj.selected = _options[randomFromInterval(0, num - 1)];
                obj.text = obj.selected.text;
                obj.selected = obj.selected.id;
                break;
            case "date":
                obj.text = [randomFromInterval(2000, 2013), randomFromInterval(1, 12), randomFromInterval(1, 25)].join('/');
                break;
            case "string":
                obj.text = [str, "[", i , "][", j, "]"].join('');
                break;
            case "number":
                obj.text = randomFromInterval(1, 176432);
                break;
            case "rating":
                var _arr = [], colors = randomFromInterval(0, num - 1);
                for(var i = 0; i < colors; i++){
                    _arr[i] = get_random_color();
                }
                obj.colors = _arr;
                obj.text = _arr[1];
                break;
            case "level":
                obj.text = randomFromInterval(0, 100);
                obj.valid = {
                    "0-40" : get_random_color(),
                    "40-50" : get_random_color(),
                    "50-100" : get_random_color()
                }
                break;
        }
        arr[j] = obj;
    }
    data[i] = arr;
}
console.log(data);
3
  • You sure its infinite? Those two massive for loops plus a switch statement inside could cause some slow performance. Commented Oct 2, 2013 at 19:48
  • Get a copy of Firebug and step through the code in the debugger. It's a far more efficient method of debugging than asking SO contributors to guess where the error is. Commented Oct 2, 2013 at 19:50
  • @MikeW I have debugged the code and when I am going step by step there is no problem for first 3-4 iterations. But after that it is getting struck. Even for 100 loops, in the question I have mentioned 500. Commented Oct 2, 2013 at 19:51

2 Answers 2

3

You are using i two times in the loop, the first one in the outer loop and the second one in the loop under "rating", wich sets i to the number of colors, which I think is less than 500.

So if there is an object with the role "rating" in the first few objects, i will never reach 500 and thus ending in an infinite loop.

Sign up to request clarification or add additional context in comments.

Comments

2

You have this inner loop:

for(var i = 0; i < colors; i++){
    arr[i] = get_random_color();
}

It resets the value of i, messing up the outer loop. You can fix this by choosing another name instead of i here.

1 Comment

Nice catch :), dumb mistake from my end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.