Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: im stuck on something simple :)

1. ## im stuck on something simple :)

i have 2 variables
var num1 = 1;
var num2 = 1;

no i also need to increment the numbers in a for loop.
but i also need to use these variables as a string for this code

num1 = num1 + num2;
which i should get "11" instead i am getting "2"
it is just adding them together.
on the other hand if i define the variables as num = "1";then i cant seem to increment the numbers.

• + adds numbers and concatenates strings. If you want to concatenate two numbers you must convert them to strings. If you want to add two strings you must convert them to numbers.

Code:
```var num1 = 1;  // a number
var num2 = 1;  // a number
num1 = num1.toString() + num2.toString();
alert (num1);  //  11;

var num1 = "1";  // a string
var num2 = "1";  // a string
var newstring = num1 + num2;
alert (newstring);  // 11
var newnum = Number(num1) + Number(num2);
alert (newnum);  // 2```
But it is silly and misleading to assign the variable name num1 to a string.

Made with real ingedients. - Walker's Thai chilli flavour crisps packet

• thankyou that solved that problem but could you look at this as it failes on the if statement.

Code:
```		var stepup = 1;
var stepupnum = 1;
for (i=1; i<5; i++)
{
if(i==4 && stepupnum<9){i=1; stepupnum++; stepup++;}

var insert = "color"+stepup;
document.getElementById(insert).style.color = tcolor1;

stepup = stepup.toString() + stepupnum.toString();

}```

• for (i=1; i<5; i++) {
if(i==4 && stepupnum<9){i=1; stepupnum++; stepup++;}

What is this supposed to do?

stepup = stepup.toString() + stepupnum.toString();
reaches infinity.
You are changing stepup from a number to a string within the loop.

• i am trying to make a for loop that changes the variable "insert"
to have
color1
color11
color111
color1111
color11111
color111111
color1111111
color11111111
and then start over with
color2
color22
color222
and so on
until it reaches
color88888888
and then exit.
could you please point out my mistake

edit: the color11111111 should have 33 x 1 not 8 x 1

• this is what i have now and i think i am getting close

Code:
```	var stepup = "1";
var stepupnum = "1";
var numcount = 1;

for (i=1; i<34; i++)
{

var insert = "color"+stepup;

document.getElementById(insert).style.color = tcolor1;

stepup = stepup.toString() + stepupnum.toString();
if(i==33 && numcount<9){i=1; numcount++; stepup = numcount; stepupnum = numcount;}

}```
but it errors out again

• i have done it
this is my code

Code:
```	var stepup = "1";
var stepupnum = "1";
var numcount = 1;

for (i=1; i<34; i++)
{

var insert = "color"+stepup;

document.getElementById(insert).style.color = tcolor1;

stepup = stepup+stepupnum;

if(i==33 && numcount<8){i=0; numcount++; stepup = numcount.toString();  stepupnum = numcount.toString();}

}```

• Originally Posted by gerble1000
i am trying to make a for loop that changes the variable "insert"
to have
color1
color11
color111
color1111
color11111
color111111
color1111111
color11111111
and then start over with
color2
color22
color222
and so on
until it reaches
color88888888
and then exit.
could you please point out my mistake

edit: the color11111111 should have 33 x 1 not 8 x 1
Code:
```for ( var colornum = 1; colornum <= 8; ++colornum )
{
var colorstring = String(colornum);
var result = "color";
for ( var step = 1; step <= 33; ++step )
{
result += colorstring;
// do something with result
// for demo purposes only:
document.write( result + "<br/>" );
}
}```
The magic is here: `String(colornum)`

That cause the number to be converted to a string and then when you use the + operator you do get string concatenation.

Tested. Works.

• yours looks better... thankyou anyway

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•