I'm busy with my Js final project at the end of my Js "kinda" beginners course. The website set up for the final project requires a section including a form used to calculate different prices (budgets) for different websites depending on the different options chosen in the form. My last line for this callculation goes like this:

The problem is, I know it is not new at all in this forum, that "total" and "total_check" variables, containing numeric values, don't get mathematically added through the line of code I wrote, but written side by side, as two different pieces of text, in the "id:resultado" <input type="text">.

My doubts:

1.- Should I have specified through parseInt or parseFloat ("total" variable might result on a decimal value depending on the options chosen in the form), somewhere earlier in the code, that "total" and "total_check" variables would be numeric values used for mathematical operations or is it possible to do it in this last line of code alone?

2.- If it can be done in this last line, how exactly should this last line look like?

Thx very much in advance.

Paul_Wilkins
—
2012-05-04T04:37:19Z —
#2

whitecreek said:

1.- Should I have specified through parseInt or parseFloat ("total" variable might result on a decimal value depending on the options chosen in the form), somewhere earlier in the code, that "total" and "total_check" variables would be numeric values used for mathematical operations or is it possible to do it in this last line of code alone?

It's better to do it as early as you can, to help prevent such issues if you make use of it later on elsewhere too. Since the values will possibly contain a decimal value, it's better to use parseFloat here in this case.

whitecreek said:

2.- If it can be done in this last line, how exactly should this last line look like?

Given that this is for a final project, I'll leave this part as an exercise for the reader

whitecreek
—
2012-05-04T10:22:18Z —
#3

Hi Paul,

Thanks for answering.

I read the info on parseFloat at MDN, which is pretty much the same information given in the book of the course I'm doing. What I don´t get to understand is how to properly use parseFloat. I mean, it seems the string in the function parseFloat [parseFLoat(string);] has to be a number. If there is any text, appart from any number, parseFloat will pay no attention to it, and return only the numeric characters. And if there is only text, parseFloat will return "NaN". So, how do I make it to relate parseFloat to a variable, which is always refered at through text?

I'm including here the js code for the budget callculation function in order to make myself clearer:

total variable would be whatever value chosen through the drop-down menu, initially ""

total will then have substrated a percentage if at <input type="text" name="meses" id="meses" class="plazo" onKeyUp="presupuesto()" /> the user introduces 1, 2, 3 or 4, as number of months.

total_check variable would be "" or 400 multiplied by the number checkboxes selected in the form.

Paul_Wilkins
—
2012-05-04T11:35:33Z —
#8

Okay, with the total we have this:

var total = document.getElementById("num").value;

So the total could be an empty string, or one of the values in the options. The empty string is guaranteed to cause problems here, so you need to ensure that the total variable makes numeric sense here right from the start.

Just before reading your last answer I thought the empty string could be the problem, since it is neither text nor number (especially not a number which is what is needed in this case!), so I went into the html file >> <form> >> <select> and introduced '0' as the default value for the selected <option>. I did the same, one by one, with all empty string values related to any variable affecting 'var total_check', and it worked :).

In addition I used 'parseInt' for 'total_check', since it will never be a decimal number. It will look better in my project using both functions :lol:.

However I like any of your solutions a lot better. They keep you form making changes in the html which is very often much more comfortable. I like especially the last one. I find it easier to handle. I´m giving serious thoughts to using it

This works and it definetely shortens things up, but it doesn´t really do what I was needing. The idea was adding 400 (euros in this case) to 'total' variable, each time a checkbox is selected. That is, 400 per checkbox selected. And substracting 400 if any of the selected checkboxes is deselected. That is, subtracting 400 per checkbox deselected. The code you wrote equals 'total_check' to 'precio_check' (being this last one a fixed constant value of 400) if there is a checkbox selection, no matter how many checkboxes are selected. If the user ticks on either 1 or 4 checkboxes, for instance, the same value will be added to 'total' variable: 400 only. It doesn't multiply 400 by the number of checkboxes selected.

I'm not sure if there is an easy way to get that through this shortened code way you wrote...

Thanks again in advance.

Paul_Wilkins
—
2012-05-07T08:45:11Z —
#13

whitecreek said:

That is, 400 per checkbox selected.

Ahh, well you can use the += operator to add 400 to the total on each occurrence instead: