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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    May 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    simple code problem - adding variables acquired from prompt()

    I need help with this piece of code please:

    Code:
    		for(count=1; count<4; count++)
    		{
    			number=prompt("Enter a number please");
    
    
    			total=(number+number+number);
    		}
    
    			document.write(total);
    My problem lies in the line with total=.
    I can't figure out how to add all the results aquired from number's prompt function. Can someone please help?

  • #2
    JUD
    JUD is offline
    New Coder
    Join Date
    Jul 2005
    Location
    I'm right here
    Posts
    92
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Try this

    Code:
    var i, number, total = 0;
    
    for(i = 1; i < 4; i++){
      number = prompt("Please enter a number", "");
    
      total += number;
    }
    
    document.write(total);

  • #3
    New to the CF scene
    Join Date
    May 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, but it doesn't work...

    Can anyone else help please?
    Last edited by corpus; 05-08-2006 at 02:25 AM.

  • #4
    JUD
    JUD is offline
    New Coder
    Join Date
    Jul 2005
    Location
    I'm right here
    Posts
    92
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Sorry I missed something out

    Code:
      var i, number, total = 0;
      
      for(i = 1; i < 4; i++){
        number = prompt("Please enter a number", "");
    
        total += parseInt(number);
      }
    
      document.write(total);

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    parseInt(number,10)

    or even better

    Number(number)
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    New to the CF scene
    Join Date
    May 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you!!


  •  

    Posting Permissions

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