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 1
Oldest  Newest  Rating
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 william appiah-dankwah · June 13, 2014
rah im that's external javascript straight of my editor
0 william appiah-dankwah · June 13, 2014
Don't get me wrong Declan I'm still slowly thinking about it
0 Ethan Fraser · June 13, 2014
Hey Will, do you remember what I showed you in the other thread?


var arrayName= new Array();
arrayName[0] = "Hello!"
for(var i = 0; i < arrayName.length; i++) {
console.log(arrayName);
}

It's a similar concept, to explain it briefly and easily what you are doing is starting a loop at 0 and waiting to see if it reaches less than 2 (that means reaching 1). The difference between my code and your code is how you make the array. You do it via
var arrayName = new Array(2);

arrayName[0] = "String 1";
arrayName[1] = "String 2";


That declares the length of the array before you declare the contents. What I suggest you do is slightly different to your process so I'll try to explain it as simply as possible.

var Candy = new Array(2);

for(var i = 0; i < Candy.length; i++) {
var s = prompt('What is your number " + (i+1) + "favourite candy?");
Candy = s;
}
document.write("I like " + Candy[0] + " and " + Candy[1] + " as well!");




Let's break it down, I've declared a variable called Candy that is an array with the length of 2 but I haven't given it any values. Next we start the loop, take not with what we do with " i ". We've set it to increment by 1 every time but we only set the loop to execute while it is LESS than the length of the array. That means it starts at 0, the first index of the array, and stops when it gets to 1, the final index and one less than the array length. Then we ask for there number 1 candy by sending them a message with the number declared by the small process of (i + 1) which takes the index (Let's say 0 for this example) and increases it by 1 just for that small moment, then it effectively goes back to 0 before the loop starts again. From there I prefer to treat the prompt as a separate variable. that way it can be edited, manipulated or cancelled as one may wish. Then we can declare the array at the index of i as the result of the prompt. After the loop has finished just say document.write(); and inside the brackets put what you want displayed.


Where are you learning JavaScript? If it's okay that I ask.
0 Declan Hester · June 14, 2014
Hah... yeah, I'm very sorry. The reason that my code didn't work is because this forum seems to use a BBCode type styling system for some stuff it seems, so got stripped out and turned everything afterwards in to italics. I've edited my previous post to switch with [n]. I'm sorry I didn't pick up on it earlier. I've tested the code out and it works -- if the board's styling system malforms the code again, I'll just send you a link to a JSFiddle or something.

Also, since you've asked how long I've been writing in Javascript: Not all that long actually. I started picking it up about 4 years ago when I was making a website for myself and dropped it until about a year ago when I was asked to be the Sr. programmer for a WiiU game. Most of the concepts I've explained to you I learned from my time programming in C/C++/C# as JavaScript's synatx is derived from C.

With a lot of languages, once you know the theory behind why something works, it's relatively easy to apply the concept to another language.
0 william appiah-dankwah · June 14, 2014
@ethan Fraser I'm learning at home from reading a few books I'm understanding but need to think it over iv just found out@ Declan its my web browser not your code it's playing up on my mac
-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
0 Ethan Fraser · June 14, 2014
@Kumi I'm not sure why you would post that on a thread to help a person with loops and arrays but it's seems slightly unnecessary and most likely confusing for the OP
  • 1
  • 2

Javascript

107,331 followers
About

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

Links
Moderators