# loop

 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 codevar 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

## Replies

- page 1

 william appiah-dankwah · June 13, 2014 Don't get me wrong Declan I'm still slowly thinking about it 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. 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. 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 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 Kumi Festus · June 14, 2014 Oh sorry, my mistake william appiah-dankwah · June 14, 2014 Your functions don't work kumi is it so posed to be like that Ethan Fraser · June 15, 2014 @WilliamStop taking codes from places and copying them, you need to learn it. You can't just read something from a book, realise you can't make it work then just ask us for help. You must learn it your way, books don't seem to work for you, I can't stress enough how much I advise the use of CodeCademy. Apart from that, his code was written wrong.``function people(name,age){ this.name = name; this.age = age; this.yearsUntilRetire = yearsLeft();}``I assume that will work. Again, LEARN IT. Don't just ask for the answers. william appiah-dankwah · June 15, 2014 Err Ethan what I know from learning from a book is that the function should work as a no click event correct me if I'm wrong that's why I have copied and asked william appiah-dankwah · June 15, 2014 Any way that's not a loop @Ethan
• 1
• 2

## Javascript

129,874 followers