...

View Full Version : im stuck on something simple :)



gerble1000
06-28-2012, 10:22 AM
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.

how do i go about this :confused:

Philip M
06-28-2012, 10:29 AM
+ 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.



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

gerble1000
06-28-2012, 10:34 AM
thankyou that solved that problem but could you look at this as it failes on the if statement.


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();

}

Philip M
06-28-2012, 10:48 AM
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.

gerble1000
06-28-2012, 12:46 PM
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

gerble1000
06-28-2012, 12:48 PM
this is what i have now and i think i am getting close


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

gerble1000
06-28-2012, 01:14 PM
i have done it
this is my 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();}

}

Old Pedant
06-28-2012, 09:38 PM
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



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.

gerble1000
06-28-2012, 10:00 PM
yours looks better... thankyou anyway :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum