1

I'm really new to Javascript and trying to create a form where I'm running into some trouble...

When I use + it does not add up to the value, instead it just puts it back to back. Ex: 5+10 (510)

Here's my code if you want to take a look at it. I'd appreciate any help since I can't figure this out on my own.

var service = document.getElementById("service");
var serviceprice = service.options[service.selectedIndex].id;

var tech = document.getElementById("tech");
var techprice = tech.options[tech.selectedIndex].id;

var hours = document.getElementById("hours").value;

// The error happens here
var total = techprice * hours + serviceprice; 

I also have an html part which the script gets the data from.

4 Answers 4

4

That happens whenever you have a string rather than a number. The + operator performs concatenation for strings. Make sure you parse your strings to numbers using parseFloat or parseInt:

var service = document.getElementById("service");
var serviceprice = parseInt(service.options[service.selectedIndex].id, 10);
var tech = document.getElementById("tech");
var techprice = parseInt(tech.options[tech.selectedIndex].id, 10);
var hours = parseInt(document.getElementById("hours").value, 10);

Note that parseInt takes an argument to specify the base. You almost always want base 10.

Sign up to request clarification or add additional context in comments.

6 Comments

+1 on the base 10 note. That's bitten me more than once when I've accepted leading zeros and end up with base-8 numbers.
And just to make it interesting, see this link for benchmarks comparing speed of various string to number conversions functions and methods: jsperf.com/number-vs-plus-vs-toint-vs-tofloat/13
@meewoK Cool comparison. Also a dictionary of ways to convert a string to a number.
You can also multiply your stringified numbers by 1 before adding.
@Paul Yup, there are tons of ways to convert a number, but many of them work slightly different with certain strings. meewoK's perf has a whole bunch of ways to do the conversion.
|
0

Try changing this line:

var total = techprice * hours + serviceprice;

to

var total = techprice * hours + parseFloat(serviceprice);

I suspect 'servicePrice' is a string, and it will then try to concatenate the first value (let's say: 100) with the second value (which is, not a number, but a string, let's say 'test'), the result being '100test'.

Comments

0

Try to convert the string to int first with parseInt or to float with parseFloat

Comments

0

This is not especially elegant, but I find it simple, easy, and useful:

var total = -(-techprice * hours - serviceprice);

or even:

var total = techprice * hours -(-serviceprice);

They both eliminate the ambiguous + operator.

2 Comments

I do not think he wants to eliminate and remove the + operator . He wants a value for it ,which would probably not be his answer as he's passing strings by default.
The point is to force the strings to numeric, which '+' won't do. My suggestion accomplishes that easily.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.