Code for calculating/updating price

Hello everyone,

I’m not competent when it comes to JavaScript and have been reading articles, which I don’t really understand, and watching YouTube videos. I can’t work out what is wrong or missing from the following code (which is modelled on a YouTube example). It works for the most part but a price update is missing. Please see the images for what I would like to do. All help would be much appreciated.

Teri :slight_smile:

$w.onReady( function () { //calculate basic price
$w(‘#wordCount’).onChange( () => {
var words = Number($w(‘#wordCount’).value);
var basicRate = “0.015”;
var basicPrice = words*basicRate;
$w(‘#total’).text = parseFloat(basicPrice).toFixed(2).toString();

$w('#express').onChange( () => { 

var returnRate = Number($w(‘#express’).value);
var returnPrice = basicPrice*returnRate;
$w(‘#total’).text = parseFloat(returnPrice).toFixed(2).toString();

});
});
});

  1. basicPrice calculates when wordCount is entered

  1. returnPrice calculates when return is selected

  1. PROBLEM: returnPrice doesn’t update when wordCount is increased/decreased AFTER the return is selected. Only the basicPrice updates. I need the returnPrice to update too. The total below should say 18.75 to include the return.


Any ideas anyone?

Thank you in advance.

Hi, teri.a.c .

I just took a very quick look at your post; the following two/2 lines jumped out at me:

var basicRate = "0.015";
var basicPrice = words * basicRate; 

You set (initialize) basicRate to a string (viz., “0.015”) but then use it in a mathematical expression, viz:

var basicPrice = words * basicRate; 

Mathematical expressions operate against numbers, not strings.

Does replacing the following line:

var basicRate = "0.015";

with the following line:

var basicRate = 0.015;

make your calculation work?

Hi, thank you for responding. Removing “” doesn’t make the calculation do what I need it to do - it functions in the same way as before, but thank you for teaching me something new! I didn’t know that about “”. :slight_smile:

Hi, Teri.

Does replacing the following line:

     $w('#total').text = parseFloat(returnPrice).toFixed(2).toString();

with the following line:

     $w('#total').text = parseFloat(Number($w('#total').value) + returnPrice).toFixed(2).toString();

or, even better, the following line:

     $w('#total').text = parseFloat(basicPrice + returnPrice).toFixed(2).toString();

give you the correct total (viz., 18.75)?

That doesn’t work either I’m afraid. I just think something is missing from the code. I just don’t know what it is. I watched another video and thought maybe I need a for statement… but I have no idea.

We can now debug this; normally/often, I would log to the console but to make it easier for you for now, we can have the information displayed right over the web page.

Just before the following line:

w('#total'#total').text = parseFloat(basicPrice + returnPrice).toFixed(2).toString();

insert the following two/2 lines:

 alert(basicPrice);
 alert(returnPrice); 

Then refresh the web page and let us know what two/2 values are displayed (one after the other).

Hi, Thanks for your help :slight_smile: Unfortunately, that doesn’t work. I think I may need a for with a loop statement for the wordCount change so that it recalculates or something. I’m not sure… I will keep researching.

Hi, Teri.

What were the values that displayed by the following debugging statements:

 alert(basicPrice);
 alert(returnPrice); 

?

TIA,

Art