Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts

    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.

    how do i go about this

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,010
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    + 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
    Last edited by Philip M; 06-28-2012 at 10:40 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Nov 2011
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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();
    
    		}
    Last edited by gerble1000; 06-28-2012 at 10:39 AM.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,010
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New Coder
    Join Date
    Nov 2011
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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

  • #6
    New Coder
    Join Date
    Nov 2011
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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

  • #7
    New Coder
    Join Date
    Nov 2011
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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();}
    
    		}

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,506
    Thanks
    77
    Thanked 4,378 Times in 4,343 Posts
    Quote Originally Posted by gerble1000 View Post
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    New Coder
    Join Date
    Nov 2011
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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
    •