0

I'm BRAND new to javascript so this is probably an easy fix I can't figure out.

I'm writing a easy script to calculate age Leapyear, Dog years and plus Five.

var age = prompt ("what's your age?");

var dog = age + 7;
var leapyear = age / 4;
var plusFive = age + 5; 

document.write(dog, leapyear, plusFive);

JS isn't calculating age + 7. It's writing the age + 7. So if I write 15 in the age prompt it will print in the browser 157.(15) and (7) Which i understand and know why. but how do I get it to compute the math.

It's actually returning 1573.75155

thanks

Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
brewhaha
  • 45
  • 4

5 Answers5

1

Like what everyone's been saying, prompt returns a string so it needs to be converted. There are a number of ways to do this, some of which have already been mentioned:

parseInt('123')
parseFloat('123')
Number('123')

These are probably the most common and depending on context also quite possibly the most clear and intuitive ways of doing it. There are also a couple of very terse and interesting ways of converting strings to numbers, depending on which kind of number you'd like. For instance, to convert a number in a string to a float, you can prefix it with the + operator:

+'1.23'

This can seem really counter intuitive, particularly since 4 + '1.23' will actually return 41.23. So what's going on? Well, the + operator, when used as a unary operator (that is, it only has one operand on the right hand side) will always convert the operand value to a number. Compare these two (try them in a javascript console):

4 + '1.23' // returns 41.23
4 + +'1.23' // returns 5.23; the second + is a unary operator

In contrived examples such as this, it really reads rather badly so you might not want to use this trick everywhere. But in your code, it reads quite well:

var age = +prompt("What's your age?")
var dog = age + 7;
var leapyear = age / 4;
var plusFive = age + 5; 

If you understand the workings of the unary plus operator (it really isn't rocket surgery) then you can get some nice terse but quite comprehensible results.

The unary + operator will always convert the value to a Number, i.e. floating point. Now, you might want an integer instead, in which case you can use the bitwise not operator twice, like so:

4 + ~~'1.23' // returns 5; note the double operator

This operator first converts the value to an integer, and then returns the bitwise complement of the value, meaning all the bits are inverted. Using it twice will mean that we get the complement's complement, i.e. the original value but this time as an integer instead of a float.

Hope this was informative!

Community
  • 1
  • 1
Marcus Stade
  • 4,724
  • 3
  • 33
  • 54
  • This is very helpful and solved my problem thanks. I figured it out and with some + "
    " lines I got it to print correctly on separate lines.
    – brewhaha Oct 27 '13 at 01:31
0

Right now Javascript handles the input as a string, so age is a string. You're gonna want to convert that to an int using the function parseInt(age).

Official documentation

Also, I'd suggest you read this about types in JS

Joren
  • 3,068
  • 25
  • 44
0

Use either parseInt(age) or parseFloat(age) depending on whether you want to accept non-integer ages.

cjc343
  • 3,735
  • 1
  • 29
  • 34
0

Your prompt is returning your number as a string, so when you calculate 15 + 7 it's actually just concatenating "7" on to "15".

You need to convert your string to an number:

var age = prompt ("what's your age?");
age = parseInt(age,10);     // Converts to an integer
                            // parseFloat(age) will allow fractional ages

var dog = age + 7;
var leapyear = age / 4;
var plusFive = age + 5; 

document.write(dog, leapyear, plusFive);
  • Why 2 lines of code, when you can do it in 1? `var age = parseInt(prompt("what's your age?"));` – Nicolas Marengo Oct 24 '13 at 22:52
  • @NicolasMarengo For clarity. The whole thing could be done in two lines and just one variable, but OP is new to Javascript. –  Oct 24 '13 at 22:57
  • Why `parseInt`, we don't like fractional ages? :) – Marcus Stade Oct 24 '13 at 23:06
  • @macke I haven't used a fractional age since I was seven and a half. –  Oct 24 '13 at 23:17
  • @MikeW I see what you did there. :o) – Marcus Stade Oct 24 '13 at 23:32
  • This returns 408.2538 ? – brewhaha Oct 27 '13 at 00:55
  • Try putting some separators in your output. For your input of 33 it's actually returning 40, 8.25 and 38, but your `document.write()` is presenting them as a string of characters. –  Oct 27 '13 at 01:01
  • ahh yes. I get it. I got this to work correctly. although I'm not sure why you used (age,10) instead of just (age). it worked correctly both ways. Is (age,10) telling it to the "10th degree" or taking it out of a fraction? I saw that you said it turns it to an integer. could you help explain that. thanks! – brewhaha Oct 27 '13 at 01:35
0

the age is going to be treated as string so string plus int result in string you have to convert age to int :

var age= parseInt(prompt("what's your age"));

updated...

emanuel.virca
  • 598
  • 1
  • 6
  • 13