for loop Help

+1 Mike Higgins · June 15, 2014
I understand that for and while loops are pretty much beginner stuff and would assume most members here are not this, due to the absence of this subject/Topic here, well Hi,, I'm very much a beginner and anxious to learn all I can, so here we go with a solution I need help in...

With this code I have below code that prompts the user 20 times,,, 


      var name=prompt("Please input your first name");
var str = " ";
document.write(name.toUpperCase() + str + name.length + "<br>");
var name=prompt("Lets input yet another name");
var str = " ";
document.write(name.toUpperCase() + str + name.length + "<br>");
var name=prompt("Lets input yet another name");
var str = " ";
document.write(name.toUpperCase() + str + name.length + "<br>");
var name=prompt("Lets input yet another name");
var str = " ";
document.write(name.toUpperCase() + str + name.length + "<br>");
var name=prompt("Lets input yet another name");
var str = " ";
document.write(name.toUpperCase() + str + name.length + "<br>");
var name=prompt("Lets input yet another name");
var str = " ";
document.write(name.toUpperCase() + str + name.length + "<br>");
var name=prompt("Lets input yet another name");
var str = " ";
document.write(name.toUpperCase() + str + name.length + "<br>");
var name=prompt("Lets input yet another name");
var str = " ";
document.write(name.toUpperCase() + str + name.length + "<br>");
var name=prompt("Lets input yet another name");
var str = " ";
document.write(name.toUpperCase() + str + name.length + "<br>");
var name=prompt("Lets input yet another name");
var str = " ";
document.write(name.toUpperCase() + str + name.length + "<br>");
var name=prompt("Lets input yet another name");
var str = " ";
document.write(name.toUpperCase() + str + name.length + "<br>");
var name=prompt("Lets input yet another name");
var str = " ";
document.write(name.toUpperCase() + str + name.length + "<br>");
var name=prompt("Lets input yet another name");
var str = " ";
document.write(name.toUpperCase() + str + name.length + "<br>");
var name=prompt("Lets input yet another name");
var str = " ";
document.write(name.toUpperCase() + str + name.length + "<br>");
var name=prompt("Lets input yet another name");
var str = " ";
document.write(name.toUpperCase() + str + name.length + "<br/>");
var name=prompt("Lets input yet another name");
var str = " ";
document.write(name.toUpperCase() + str + name.length + "<br>");
var name=prompt("Lets input yet another name");
var str = " ";
document.write(name.toUpperCase() + str + name.length + "<br>");
var name=prompt("Input yet another name, almost there");
var str = " ";
document.write(name.toUpperCase() + str + name.length + "<br>");
var name=prompt("Lets input yet another name, 1 more after this");
var str = " ";
document.write(name.toUpperCase() + str + name.length + "<br>");
var name=prompt("Lets input your 20th and final name");
var str = " ";
document.write(name.toUpperCase() + str + name.length + "<br>");




OK LoL, maybe there was a shorter way to write that code,, again ---> beginner here :-)

I now need to replace the twenty prompt statements with just one statement by using a For Loop to do this instead,,,,,
and then I need to use another For Loop to output/document.write the information onto the browser window.

Post a Reply

Replies

Oldest  Newest  Rating
0 Ethan Fraser · June 15, 2014

for(var i = 0; i < 20; i++) {
var str = " ";
switch(i) {
case 0:
var name=prompt("Please input your first name");
document.write(name.toUpperCase() + str + name.length + "<br>");
break;
case 18:
var name=prompt("Lets input yet another name, 1 more after this");
document.write(name.toUpperCase() + str + name.length + "<br>");
break;
case 19:
var name=prompt("Lets input your 20th and final name");
document.write(name.toUpperCase() + str + name.length + "<br>");
break;
default:
var name=prompt("Lets input yet another name");
document.write(name.toUpperCase() + str + name.length + "<br>");
break;
}
}


Need it explained? Just ask
-2 Declan Hester · June 15, 2014
A bare-bones for loop is as follows:

for(statement a; statement b; statement c){
//code
}


where
statement a: Defines what variable we're counting with. For example: n=0.
statement b: Defines a condition which must be true in order for the loop to run. For example: n<20

statement c: Defines a condition in which the counter changes each time the code inside the loop has been executed. For example: n++
Take note of the semi colon after the first two statements. This is extremely important to have. Your loop will not work without it.

Unless you particularly need the two loops, this is how I would write your solution out as. I'm sure there are better ways of doing it (including Ethan's use of a switch statement) I just wasn't sure how far along you are in your learning ^_^. If you do need the two loops, I'm fairly sure you need to store the data from the first loop in to an array.


for(n=0;n<20;n++)
{
if(n==0)
name = prompt("Please enter your first name");
else if(n==17)
name = prompt("Input yet another name, almost there");
else if(n==18)
name = prompt("Lets input yet another name, 1 more after this");
else if(n==19)
name = prompt("Lets Input your 20th and final name");
else
name = prompt("Lets input yet another name");
document.write(name.toUpperCase() + " " + name.length + "<br />");
}




Basically this reads as:
set n to 0
while n is less than 20, I want the code in this loop to be executed.
if n is equal to zero, prompt the user for their first name
otherwise if n is equal to seventeen, prompt the user for the third last name
otherwise if n is equal to eighteen, prompt the user for the second last name
otherwise if n is equal to nineteen, prompt the user for the last name
otherwise give the user the default prompt.
once the prompt has been given, display their response in upper case, followed by a single space, followed by the length of their name, and finally a line break.
increase n by 1
  • 1

Javascript

107,132 followers
About

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

Links
Moderators