2
   

While (condition) loop in JavaScript not working fine

 
 
Reply Wed 28 May, 2014 08:57 pm
I have i simple piece of code that receives inputs from the user till the user presses zero, but the code receives this zero also, I don't know why here is the code:


var counter=0;
var myValues = [];
var InputNumber=prompt("Enter any number, Exit with zero");
InputNumber=parseInt(InputNumber);
myValues [0]=InputNumber;

while (InputNumber != 0)
{
InputNumber=prompt("Enter any number,Exit with zero");
myValues=parseInt(InputNumber);
i++;
myValues [counter] =InputNumber;
}
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Question • Score: 2 • Views: 1,278 • Replies: 1
No top replies

 
npdule
 
  1  
Reply Mon 7 Jul, 2014 05:11 pm
@samy youssef,
Hi,
I noticed some errors on your code and i fix that and this is the correct code:
var counter=0;
var myValues = [];
var InputNumber = prompt("Enter any number, Exit with zero");
var Num = parseInt(InputNumber);
myValues [0] = Num;

while (InputNumber != 0)
{
InputNumber=prompt("Enter any number,Exit with zero");
var someNum = parseInt(InputNumber);
counter+=1;
myValues [counter] = someNum;
}
document.write("Values are: ");
for(var i = 0;i<myValues.length;i++){
document.write(myValues{i} + ", ");
}
I added something to write the results of array and when i output this in browser and if i add 0 it finish execution and the result is following:
Values are: 1, 2, 3, 0,

P.S.: in code inside for loop you need to change {} brackets with correct one because its not showing when i pasted code here ...

Hope this can help you.
Cheers.
0 Replies
 
 

Related Topics

Clone of Micosoft Office - Question by Advocate
Do You Turn Off Your Computer at Night? - Discussion by Phoenix32890
The "Death" of the Computer Mouse - Discussion by Phoenix32890
Windows 10... - Discussion by Region Philbis
Surface Pro 3: What do you think? - Question by neologist
Windows 8 tips thread - Discussion by Wilso
GOOGLE CHROME - Question by Setanta
.Net and Firefox... - Discussion by gungasnake
Hacking a computer and remote access - Discussion by trying2learn
 
  1. Forums
  2. » While (condition) loop in JavaScript not working fine
Copyright © 2024 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.04 seconds on 11/12/2024 at 02:11:07