0

Instead of the number addition this code is doing the string concatation. How to specify the type of the input in the prompt field(eg:number,string).

var jonage = prompt("enter the johns age");
var jonHeight = prompt("enter the johns height");
var jonScore = jonHeight + jonage * 5;
console.log(jonScore);
vineethc4f
  • 259
  • 1
  • 3
  • 6

5 Answers5

5

prompt is extremely basic and offers no way to constrain the input other than the way browsers do by default (e.g., no line breaks).

Instead, convert the string to number afterward. There are a lot of ways to do that:

  • Unary +: var jonage = +prompt("enter the johns age");
  • Number: var jonage = Number(prompt("enter the johns age"));
  • parseInt: var jonage = parseInt(prompt("enter the johns age"));
  • parseFloat: var jonage = parseFloat(prompt("enter the johns age"));

Unary + and Number:

...work the same way:

  • They convert the entire string to number if possible, yielding NaN if the entire string can't be converted to number. So +"123abc" is NaN.
  • They convert an empty string ("") to 0 (surprisingly).
  • They respect JavaScript syntax for specifying the number base (radix), so +"0x10" is 16 and +"0o10" is 8.

parseInt

  • Converts only as much of the string as it can before the first invalid character, not the whole string. So parstInt("123abc") is 123.
  • Converts an empty string ("") to NaN (not 0).
  • Respects JavaScript syntax for specifying the number base (radix), e.g., parseInt("0x10") is 16.
  • Also lets you specify the radix explicitly as a second argument, to force it to treat the input as being in that number base: parseInt("0x10", 10) is 0 (because the x becomes invalid, 0x is no longer treated as indicating the number base. This used to be important with decimal in case end users entered strings like "010" and browsers implemented "legacy" Octal (leading 0 instead of leading 0o), but it's been eight years now (since the 5th edition spec) since parseInt was officially not allowed to do that.
  • As the name suggests, only converts the part of the string that defines a whole number (integer).

parseFloat

Like parseInt, but does fractional numbers and doesn't do radix prefixes. parseFloat("0x16") is 0, because the x is invalid (because it doesn't do radix prefixes).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

You can't. prompt will always return a string.

Instead, convert the string to whatever format you want (e.g. with parseFloat).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

The prompt is always returning it's input as a String value.

You need to parse it to either number (if decimal used) or integer if full numbers used:

Decimal example

var jonage = Number(prompt("enter the johns age"));
var jonHeight = Number(prompt("enter the johns height"));
var jonScore = jonHeight + jonage * 5;
console.log(jonScore);

Integer example

var jonage = parseInt(prompt("enter the johns age"));
var jonHeight = parseInt(prompt("enter the johns height"));
var jonScore = jonHeight + jonage * 5;
console.log(jonScore);
Jankapunkt
  • 8,128
  • 4
  • 30
  • 59
0

As others suggested, you cannot change the what the type for the prompt return. It will always return a string. Instead, convert the string into an integer before outputting the result.

Also, it seems like your math logic is wrong? If you want the addition first, then place it in parenthesis before doing the product. Remember that * and / are executed before + and -.

var jonage = prompt("enter the johns age");
var jonHeight = prompt("enter the johns height");
var jonScore = (parseInt(jonHeight) + parseInt(jonage)) * 5;
console.log(jonScore);
Chris
  • 57,622
  • 19
  • 111
  • 137
-1

You always need to convert prompt value as it always a string.

var jonage = prompt("enter the johns age");
var jonHeight = prompt("enter the johns height");
var jonScore = parseInt(jonHeight) + parseInt(jonage) * 5;
console.log(jonScore);
Manzurul
  • 633
  • 6
  • 11
  • 1
    While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations! – Blue Jan 12 '18 at 13:56
  • @FrankerZ Yeah I understood that just after posted the answer. – Manzurul Jan 12 '18 at 13:57
  • 1
    (And for the record, I was not the one who downvoted you) – Blue Jan 12 '18 at 13:58
  • 1
    Always include a radix with `parseInt`: `parseInt(value, 10)` – Cerbrus Jan 12 '18 at 14:03
  • @Cerbrus I hope you got your answer from above. – Manzurul Jan 12 '18 at 14:24