loop

0 william appiah-dankwah · June 13, 2014
just wrote this code funny thing is bucky has a tutorial on it does anyone know why my browser displays the code and undefined inside the code

var Candy = new Array(2);

for(var i=0;i<2;i++)

{
Candy = prompt("DETAILS", "");
document.write(Candy[0] + Candy[1]);
}
 
can someone check and let me know if they get that

Post a Reply

Replies

- page 2
Oldest  Newest  Rating
0 Ethan Fraser · June 16, 2014
I know it's not a loop... It was just said it was broken so I helped to fix it. 
0 Declan Hester · June 13, 2014
I'm not sure why your browser is displaying the code with document.write, but I can help you with your loop~


The reason why it is printing the prompt value and then "undefined" because of the lines:

Candy = prompt("DETAILS", "");
document.write(Candy[0] + Candy[1]);


Because you aren't defining where to store the value the user returns from the prompt Javascript is assuming that the first value in the array (ie: Candy[0]) is being changed with each iteration of your loop. 


Assuming we have two candies (Mars Bar and Skittles), we need to tell Javascript that Candy[0] is Mars Bar and Candy[1] are Skittles. If you recall from my post in your Arrays thread, if we use...

for(n=0;n<2;n++){
//somecode
};

...then we're using the same counting scheme as the computer. Because of this, we can tell Javascript that when n is 0, we want to prompt the user for a candy name and change Candy[0] to be the same as how the user has responded, and when n is 1 we want to prompt the user again, but this time change Candy[1].


We do this with:

var Candy = new Array(2);
for(n=0;n<2;n++){
Candy[n] = prompt("DETAILS", "");
document.Write(Candy[0] + Candy[1]);
}

Because n is already the same value as the position of the variable we want to change in the array. 


HOWEVER now when we run the code we come in to a problem. The first time through the loop, we will see the output "Mars Barundefined". This is because we haven't actually defined what Candy[1] -is- yet, and the second time we run through the loop we will see the output "Mars BarundefinedMars BarSkittles". This is because we're saying every time we go through this loop, I want you to display the values for Candy[0] and Candy[1].


IF we wanted an output every time that we went through the loop (eg: If we were doing a loop for multiplication tables and wanted to see the output for each number we give) then this would be fine, but for this loop, it is unnecessary.


So in order to get around this, we should change the source code to:

var Candy = new Array(2);
for(n=0, n<2; n++){
Candy[n] = prompt("DETAILS", "");
}
document.write(Candy[0] + Candy[1]);

If we do this, then the output of your loop is posted after both values have been defined, so when the loop finishes instead of seeing "Mars BarundefinedMars BarSkittles", we'll only see "Mars BarSkittles".


OR


We could change it to:

var Candy = new Array(2);
for(n=0;i<2;i++){
Candy[n] = prompt("Please enter the name of candy #" + (n+1), "");
document.write(Candy[n]);
}

And that way each time we run through the loop, we show the name of the Candy to let the user see what they've already added.

FINALLY as some housekeeping: In order to improve readability, you may want to consider adding a space between the output of Candy[0] and Candy[1], as well as giving the user some details about what you're prompting them for.


For example:

var candy = new Array(2);
for(n=0;n<2;n++){
candy[n] = prompt("Please enter the name of candy #" + (n+1), "");
}
document.write(candy[0] + " " + candy[1]);


OR



var candy = new Array(2);
for(n=0;n<2;n++){
candy[n] = prompt("Please enter the name of candy #" + (n+1), "");
document.write(candy[n] + " ");
}


Hope this helps you. =]
0 william appiah-dankwah · June 13, 2014
Thanks I'm gonna take all that in slowly how long have you been writing javascript
0 william appiah-dankwah · June 13, 2014
Just wrote the codes Declan re-wrote and it's not working correctly Var Candy = new Array(2);for(var I = 0; I < Candy; I++){Candy = prompt("details"' "");Document.write(Candy[0] + Candy[1]);}Does not work and the other way with Candy.length as the array in the loop spits out to more than two loops before exacting the codeThanks for the rework thoughShould be easy to work
-1 Kumi Festus · June 14, 2014

function people(name,age){
this.name = name;
this.age = age;
this.yearsUntilRetire = yearsLeft;
}

function yearsLeft(){
var numYears = 65 - this.age;
return numYears;
}

var bucky = new people("Bucky Roberts",25);
var taylor = new people(Taylor Swift",21);

document.write(bucky.name + " loves " + taylor.name + " because " + taylor.name + " is " + taylor.age + " years old and beautiful");


Hahaha, I like this game
-1 william appiah-dankwah · June 13, 2014
rah im that's external javascript straight of my editor
  • 1
  • 2

Javascript

107,269 followers
About

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

Links
Moderators