Practice and reinforce the concepts from Lesson 6
DO NOT DELETE the existing files in the template:
ONLY EDIT the necessary files.
Time needed: 20-25 minutes
stackblitz-starters-lpb8hrgzCreate a program that performs basic math operations using functions.
Get user input for two numbers:
x and use +prompt() to get a numbery and use +prompt() to get a numberCreate the addition function:
addition() with 2 parameters: x and yCreate the remaining math functions:
subtract() function (returns x - y)multiply() function (returns x * y)divide() function (returns x / y)Display the results:
console.log() to display the result of each function💡 Tip Function Structure Example:
function addition(x, y) {
return x + y;
}
Remember to call your functions with the values you collected!
When you enter 10 as the first number and 5 as the second number:


Your console output (press F12 to open) should look like this:

💡 Tip Having trouble with prompts?
- The
+beforeprompt()converts the string input to a number- Without it, "10" + "5" would give you "105" instead of 15!
Time needed: 15-20 minutes
stackblitz-starters-zyzgiklpCreate a button that displays an alert message when clicked.
In your JavaScript file (script.js):
myWordsmyWordsCreate the echo function:
echo()alert() to display the myWords variableIn your HTML file (index.html):
onclick="echo()" attribute to the button💡 Tip HTML Button Example:
<button onclick="echo()">Echo</button>
The onclick attribute tells the button which function to run when clicked!
Your webpage should show:

When you click the button, you'll see:

ℹ️ Learn More:
Time needed: 15-20 minutes
stackblitz-starters-mc6xixuyCreate a 6-sided dice that generates random numbers when clicked.
Create the random function:
random() (no parameters needed)diceNumberalert() to display the dice numberCreate the dice button:
onclick="random()" to the button💡 Tip Random Number Formula:
Math.floor(Math.random() * 6) + 1
Math.random() gives a decimal between 0 and 0.999...Math.floor() rounds down to get 0 to 5Your webpage should show:

When you click the button (example roll of 4):

ℹ️ Learn More:
Time needed: 25-30 minutes
stackblitz-starters-gnqmqjeaCreate a restaurant ordering system that tracks your money and allows you to order food.
Set up your wallet:
myMoneyCreate the menu buttons (in index.html):
order() with the food name as an argumentCreate the order function:
function order(food) {
// Check which food was ordered
// Call pay() with the correct price
}
pay() with the food name and priceCreate the balance function:
function balance(price) {
// Return myMoney minus the price
}
Create the pay function:
function pay(food, price) {
// Check if you have enough money
// Update myMoney and show messages
}
balance() to check if you can afford itmyMoney to the new balance💡 Tip Button Example:
<button onclick="order('Chicken Chop')">Chicken Chop RM12</button>
Notice how we pass the food name as a string in quotes!
After ordering all 3 foods and trying to order more:

⚠️ Warning Common Mistakes:
- Forgetting quotes around food names in onclick
- Not updating myMoney after each purchase
- Using wrong prices in the order() function
Exercise 1 - Calculator:
+prompt()Exercise 2 - Echo Button:
myWords declared with "Hello World"echo() that alerts the variableonclick="echo()"Exercise 3 - Dice Generator:
random() with no parametersExercise 4 - Restaurant System:
myMoney set to 50order(), balance(), and pay()⚠️ Warning Before You Submit:
- Test ALL your functions - make sure they work!
- Check that all buttons trigger the correct functions
- Verify console outputs match the expected results
- Make sure you haven't deleted any template files
Total time needed: 75-100 minutes
When you have completed all 4 exercises, submit them using the link below:
ℹ️ Submission Tips:
- Submit the Stackblitz link for each exercise
- Or upload your project files if you worked locally
- Double-check that all functions are working before submitting!
"My function isn't running when I click the button"
onclick matches exactlyonclick="myFunction()""My prompts show NaN or undefined"
+prompt() to convert strings to numbers"Console isn't showing anything"
console.log() not just log()"My math operations give weird results"
+, prompt returns strings💡 Tip Still stuck?
- Review the function syntax examples in each exercise
- Check the template code for hints
- Make sure all your brackets and parentheses match up