/    Sign up×
Community /Pin to ProfileBookmark

I am creating a simple page that has to do with room rates. It shows if you stay this night to this night it’s this etc….but I need it to calculate the total cost. Example..If you stay 7 nights, your cost per night $105.00, but it also need to show the total cost is…so the price per night * the number of nights. I am missing something in my process and can’t quite figure it out. Here is what I have.

<!DOCTYPE HTML>
<html>
<head>
<title>roomRates</title>
<script>
function calcPrice() {
var night=document.getElementById(“roomPrice”).value;
var price=0;

if (night <= 3) {
price = 125;
} else if (night < 8) {
price = 105;
} else if (night < 11) {
price = 95;

} else {
price = 85;
}

document.getElementById(“outPrice”).innerHTML = “The price per night is: $” + price.toFixed(2) +”.”;
document.getElementById(“outCost”).innerHTML = “The total cost is: $” + toalCost.toFixed(2) +”.”;

}
</script>
</head>

<body>
<h1>roomRate<h1>

<p>
Number of Nights: <input id=”roomPrice” type=”number” min=”1″ max=”15″ size=”8″>
</p>

<button type=”button” onClick=”calcPrice()”>Submit</button>
<br/>

<p id= “outPrice”></p>
<p id= “outCost”></p>

</body>
</html>

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@NachfolgerApr 02.2020 — @BuffySummer#1616897

  • 1. Use .. brackets next time.

  • 2. Your variable/input naming makes no sense. Why is the number of nights input named roomPrice?

  • 3. You're missing the total calculation.

  • 4. Use let in a function scope, it prepares you for modern ES6 design

  • 5. Use documentElement.textContent if you're ONLY updating the text. Not in this instance, but in many cases you can open yourself to HTML injection by using innerHTML without needing to.


  • ``<i>
    </i>function calcPrice() {
    // Variables
    let nights = parseInt(document.getElementById("roomPrice").value) || 0;
    let price = 0;

    // Checks
    if (night &lt;= 3) {
    price = 125;
    } else if (night &lt; 8) {
    price = 105;
    } else if (night &lt; 11) {
    price = 95;
    } else {
    price = 85;
    }

    document.getElementById("outPrice").textContent =
    The price per night is: $${parseFloat(price).toFixed(2)};
    document.getElementById("outCost").textContent =
    The total cost is: $${parseFloat(price * nights).toFixed(2)};
    }<i>
    </i>
    ``
    ×

    Success!

    Help @BuffySummer spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.24,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...