user Input, to calculate, and display as Text.

Hello Wix community!

I have a date Picker variable startDate and endDate.
to calculate the numberOfNights is:
numberOfNights = endDate - startDate;

Now, i have inputs:

var desiredMiles;
var baseRate;

to get the user input for desiredMiles is easy,
but, for baseRate, its suppose to be, for every numberOfNight is $60
so if User chooses 5 nights, its $300.

So…
Question 1)
How do i generate a code to iterate the baseRate and Display it.
Variables to be displayed to text are
a) numberOfNights
b) Calculated baseRate
c) desiredMiles.

Please if you can help me with Question 1 only, i would be very happy, but if you feel you can Handle Question 2, Then i would not know how to repay you. Here it comes:-

Question 2:

After Getting and Displaying the: numberOfNights, baseRate and DesiredMiles, next i would like to Calculate the following:

First, a Variable or Function needs to Generate a randomNumber between 0 and 20.

Then the Total Output would be.

Total = [ BaseRate + (desiredMiles * 0.35) ] – [ ( BaseRate + (desiredMiles * 0.35) ) * ( randomNumber/1000 ) ]

*(Note: the [ ] is not an array.)

costPerNight = Total / numberOfNights

Display to the user…
The total cost for your trip ([startDate]-[endDate]) would be $[total] which includes [desiredMiles] miles. This works out to be $[costPerNight] per night.

I hope i am not asking too much as i really don’t know how on Earth i am going to achieve this without the Communities Help.

Thanks a Mill for anyone who can attempt any of the Two. thanks…

Hi Usman,

For your first question, your code would look something like this:

let baseRate = numberOfNight * 60;
$w(“#baseRate).text = “$” + baseRate.toFixed(2);

The toFixed(2) function helps format the output so it shows dollars and cents. Something like this: $60.00. The formatted result is set to a text field on you page called #baseRate.

For the second question, you can calculate a random number between 0 and 20 like this:

let rnd = Math.floor(Math.random()*21);

Then your full calculation would look like this:

let rnd = Math.floor(Math.random()*21);
let total = (BaseRate + (desiredMiles * 0.35) ) – ( ( BaseRate + (desiredMiles * 0.35) ) * ( rnd/1000 ) )

This should get you started in the right direction.

Yisrael

Will try it out, thanks