Ignacio Bricchi
Ignacio Bricchi

Reputation: 15

my for loop doesn't work and I don't know why

$(document).onload(function(){
    for (var i = 0; i <max; i++) {
        if(pi[i]===undefined||pi[i]===null||pi[i]==0){
            pi[i]=1;
        }
    }
});
alert('hello');

I have jQuery installed and all the variables are set earlier on in the code, but anything after the for loop doesn't work such as the alert('hello'). If anyone cans see something wrong with this please tell me.

edit

var max=438;
localStorage.setItem('max', max);
var pi=[];
pi=JSON.parse(localStorage.getItem('pi'));
var i=localStorage.getItem('i');
var url1=Math.floor((Math.random() * max) + 1);
var url2=Math.floor((Math.random() * max) + 1);
if(url1==url2){

url2=url2+2;
if(url2>max){
    url2=url2-max;
}
}
document.getElementById('imgimg1').src='../img/'+url1+'.gif';
document.getElementById('imgimg2').src='../img/'+url2+'.gif';
var v1=url1-1;
var v2=url2-1;
function vote1() {
    pi[v1]=pi[v1]+(pi[v2]/pi[v1]);
    localStorage.setItem('pi',JSON.stringify(pi));
    location.reload();
}
function vote2() {
    pi[v2]=pi[v2]+(pi[v1]/pi[v2]);
    localStorage.setItem('pi',JSON.stringify(pi));
    location.reload();
}
$(document).load(function(){
    for (var i = 0; i <max; i++) {
        if(pi[i]===undefined||pi[i]===null||pi[i]==0){
            pi[i]=1;
        }
    }
});
alert(pi);

this is the full code

Upvotes: 0

Views: 131

Answers (4)

Mino
Mino

Reputation: 264

Try with this:

/* assuming this */
var max = 4; // for example
var pi = [0, 0, 0, 0];
/* end assuming this */

$(document).ready(function() {
    for (var i = 0; i < max; i++) {
        if(pi && pi[i] && pi[i] < 0) {
            pi[i] = -1;
        } else {
            pi[i] = 1;
        }
    }
    alert(JSON.stringify(pi));
});

It should write '[1, 1, 1, 1]'

[EDIT]

Regarding your entire code, why do you need to loop after document ready ?

Why not init pi like that:

var max=438;
localStorage.setItem('max', max);
var pi=[];
pi=JSON.parse(localStorage.getItem('pi'));
for(var i = 0; i < max; i++) {
    pi[i] = 1;
}
[...]

Upvotes: 0

xcopy
xcopy

Reputation: 2288

Assuming all variables are set correctly, your code should be:

$(document).load(function(){
    for (var i = 0; i <max; i++) {
        if(pi[i]===undefined||pi[i]===null||pi[i]==0){
            pi[i]=1;
        }
    }
    alert('hello');
});

Maybe this will help, although, I do not know what you are setting these variables to. Please note that instead of document load, I am using the jquery ready event.

<div id="message"></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script>
  var max = 100;
  var pi = [];
  
  $(function() {
    for (var i = 0; i <max; i++) {
        if(pi[i]===undefined||pi[i]===null||pi[i]==0){
            pi[i] = 1;
        }
    }
    $('#message').text(pi.length);
});
</script>

Upvotes: 1

Peter Kelley
Peter Kelley

Reputation: 4196

I assume you have "max" set to some positive number. I think the problem is that you coded $(document).onload(... when you should have coded $(document).ready(... Hope that helps! PK

Upvotes: 1

Mohit Bhardwaj
Mohit Bhardwaj

Reputation: 10083

$(document).onload(function(){
    for (var i = 0; i <max; i++) {
        if(typeof pi[i]==="undefined"||pi[i]===null||pi[i]==0){
            pi[i]=1;
        }
    }
});
alert('hello');

Upvotes: 1

Related Questions