Check a Weekend-Trip Budget

In the on-site exercises so far, the tests did the checking for you. Each one called your function and compared what it returned with an expected result, a value worked out from the task before anyone looked at your code. A green test meant your actual result, the value your code produced, matched it.

Your own scripts don’t come with tests. When you run a script in VS Code, it prints a number, and nothing tells you whether that number is right. In this lesson, you’ll take over the test’s job. You’ll plan a small weekend trip budget, work out the results by hand first, and then run a script and compare. By the end, you’ll also pick a few extra cases worth checking and decide what to do when the task doesn’t say.

Work Out the Expected Result First

You already did this once. In the marathon brief from the arithmetic lesson, you noted the values and worked out the total minutes on paper before writing any code. That paper answer is what makes a check possible, because it comes from the brief and not from the script.

That independence matters. If you work out the expected value by reading your own code, then you’ll repeat any mistake the code makes, and the two numbers will agree even when both are wrong.

Here’s the brief for this lesson. You’re planning two nights in Vienna with a budget of $250:

  • Train: $45 each way, and you need a ticket there and a ticket back
  • Hostel: $38 per night for two nights
  • Food: $25 per day for two days
  • Museum: One ticket for $20

The script should print the trip’s total cost and how much of the budget is left over. Grab a pen and paper and work out both numbers now. Write them down before you scroll any further, because the script below will print its own answers, and you want yours to come first.

Run the Script and Compare

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.