Jump to content

First ever program

- - - - -

  • Please log in to reply
4 replies to this topic

#1
mmccreesh1

mmccreesh1

    Newbie

  • Members
  • Pip
  • 4 posts
Hi, JavaScript is my first ever programming language (well, I do know HTML and CSS but I don't think they count?).

I watched tutorials on JavaScript and I'm done now but have already messed up my program. It is just a very simple addition thing. But for some reason, it literally puts the two entered numbers together instead of adding them. I am a complete noob. Here's the code:
<script type="text/javascript">


alert("Welcome to my JavaScript additon program, please click OK.");


var first = prompt("Enter your first number", "");

var second = prompt("Enter our second number", "");


var answer = first + second;



alert(first + " + " + second + " is " + answer );


</script>

Could someone tell me what I am doing wrong.

Many thanks.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Hello,

The prompt for convenience returns a string of characters (a string) as that is what you are really entering. JavaScript allows you to use string+string to combine the two - in this case "22" instead of "4".

You will have to parse the string in to an integer each time:
var answer = parseInt(first) + parseInt(second);

There are also functions such as parseFloat for floating point numerics (i.e. "3.14") as integers are whole number only.

JavaScript parseInt() Function

Alexander.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
mmccreesh1

mmccreesh1

    Newbie

  • Members
  • Pip
  • 4 posts
Hi, thanks so much for the solution and also the link. I still don't understand what radix is, could you explain?

Many thanks.

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

mmccreesh1 said:

Hi, thanks so much for the solution and also the link. I still don't understand what radix is, could you explain?

Many thanks.
You are very welcome.

A synonym of radix can be "base" or "foundation". Base 2 is binary for example, base 10 is decimal, base 16 hexadecimal. You can leave that out unless you need it (it says "optional"), it is there for convenience in case you want to enter numbers of an other base (i.e. from a calculator script)

Alexander.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
mmccreesh1

mmccreesh1

    Newbie

  • Members
  • Pip
  • 4 posts
Oh yes, I understand. Thanks for the clear description.

I have watched a full beginner series on JavaScript but now can't think of how I could practise my new found skills. Any ideas?

Many thanks.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users