Jump to content

Help! JavaScript Looping through Numbers

Sergio45
Go to solution Solved by Mr_KoKa,
for(i=0; i<num; i++)
{
  number[i] = num; //Think about this line
}

You prompt user for input once, you set num variable once, so when you iterate from 0 to num - 1 and set every index of number array to num there is no surprise that all indexes are equal to num. It is i variable what changes trough iterations, it goes from 0 to num - 1 so to set it form 1 to num you would need to add 1.

I am trying to loop through a number that a user enters.

For example if a user enters the number 45. It should start a 1 and go through 45. (1,2,3,......45) . 

ANY guidance would be appreciated. 

The code that I have sets the results on a table(Thanks to VorticalBox and Mr_KoKa help).

It loops through but does not display the increments. 

So if I enter 8. it shows the number 8 times(88888888) not 12345678. 

Thanks again.

 

 

function testQuestion2() 
{
	//empty array to store our numbers
		var number =[]
		
		var num = parseInt(prompt("enter number"))
		
		//loop x times 
		for(i=0; i<num; i++)
		{
			
			//store each input in the array
			number[i] = num;
			
			
		}
		//start of list
		var ul = document.createElement('ul');
		//loop every item in array
		for (i=0;i<number.length; i++)
		{
			var li = document.createElement('li');
			//adds current array item to the page
			li.innerHTML = number[i];
			ul.appendChild(li)
		}
		//end of list
		document.body.appendChild(ul);
	
	
	
	
	


}

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

for(i=0; i<num; i++)
{
  number[i] = num; //Think about this line
}

You prompt user for input once, you set num variable once, so when you iterate from 0 to num - 1 and set every index of number array to num there is no surprise that all indexes are equal to num. It is i variable what changes trough iterations, it goes from 0 to num - 1 so to set it form 1 to num you would need to add 1.

Link to comment
Share on other sites

Link to post
Share on other sites

He is right ^^^ number should be set equal to i, which is the one that is increasing.

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Mr_KoKa said:

for(i=0; i<num; i++)
{
  number[i] = num; //Think about this line
}

You prompt user for input once, you set num variable once, so when you iterate from 0 to num - 1 and set every index of number array to num there is no surprise that all indexes are equal to num. It is i variable what changes trough iterations, it goes from 0 to num - 1 so to set it form 1 to num you would need to add 1.

Okay, I see Mr_KoKa. 

Reason why we want to go by i is because i is the counter and is keeping track of the numbers it has to go through loop. SO if user enter 5 for a number. "num" stores the 5 and "i" tells it as long as i is less than num (5) keep on going....12345! 

 

I get it, Thanks again Mr_KoKa.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×