help with code

+1 william appiah-dankwah · March 9, 2015
i am trying to get this loop to work with this array i have put in an object literal i dont know what to do help needed.

var Yellow = {

say: function(){

var Can = new Array();

Can[0]  = "Blue";
Can[1] = "Green";

for(var i = 0; i < 10; i++){

document.getElementById('Plane').innerHTML = i[1];

}
}
};
Yellow.say();
































Post a Reply

Replies

- page 1
Oldest  Newest  Rating
0 william appiah-dankwah · March 9, 2015
i want to use getElementById() were do i find a list of properties and methods for the dom elements i think it is?
+1 Hossein Vahabi · March 16, 2015
try this


window.onload = function (){

var Can = new Array();

for(var i=0;i
0 william appiah-dankwah · March 16, 2015
Write out the full code 
0 william appiah-dankwah · March 17, 2015

window.onload = function() {

var Can = new Array();

Can[0] = "Blue";
Can[1] = "Green";

for(var i =0; i < 10; i++) {

var Flap = document.getElementById('Plane');

Flap.innerHTML += Can;
}
}


is this what you meant? 
0 william appiah-dankwah · March 17, 2015
Its definitely not an interger the loop 100% works in the code iv written so so should the array 
0 william appiah-dankwah · March 17, 2015
Is that the ligue lumie? 
0 william appiah-dankwah · March 17, 2015
The code is not asking to display an interger it's asking to display the array in a loop
0 william appiah-dankwah · March 18, 2015
fwhats your problem man you can see that in the code!

Var Flap = getElementById('Plane');

Flap.innerHTML += Can;

0 william appiah-dankwah · March 18, 2015
look man. how do you troll? did you use the code?
0 Doug Fresh · March 19, 2015
// Yellow is an Object, created at some point when you load this JS
var Yellow = {

// attribute that calls a function
say: function(){

// Can is a new blank array, with nothing in it.
var Can = new Array();

// we assign 2 new values to positions 0 and 1 in the array
Can[0] = "Blue";
Can[1] = "Green";

//begin for loop, after each loop is complete, check if i is less than 10. if less, increment i by 1
for(var j = 0;j < 10; j++){

// each loop, we get the plane element and assign the index point of i, (an integer as defined by the for loop), to the inner html of whatever element with an id of 'Plane'. i will be equal to a number between 0-9 at all times.
document.getElementById('Plane').innerHTML = j[1];


}
}
};

// call the attribute say.
Yellow.say();

after commenting out your code, problems are much easier to solve, and for others to solve. The answer is there. window.onload did nothing to solve your problem, the second time you posted the code you sort of solved the issue by appending the Can array, but now the for loop is useless because you have all the information being displayed with:

var Flap = document.getElementById('Plane');

Flap.innerHTML += Can;

Can references the entire array, while Can[j] references the indexes of the array. Even if you figured this out, you would have ran into another issue, Can only has a length of 1 (starting from 0). So the for loop with  Can[j] would have worked twice, then broke when j == 2. Commenting out your code can help catch errors like this, and make things easier for people trying to help.
  • 1
  • 2

Javascript

107,182 followers
About

A scripting language that is added to standard HTML to create interactive effects, apps, games for the browser.

Links
Moderators