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
Create 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 y
Create 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:bulb: 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:
:bulb: 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
Create a button that displays an alert message when clicked.
In your JavaScript file (script.js):
myWords
myWords
Create the echo function:
echo()
alert()
to display the myWords
variableIn your HTML file (index.html):
onclick="echo()"
attribute to the button:bulb: 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:
:information_source: Learn More:
Time needed: 15-20 minutes
Create a 6-sided dice that generates random numbers when clicked.
Create the random function:
random()
(no parameters needed)diceNumber
alert()
to display the dice numberCreate the dice button:
onclick="random()"
to the button:bulb: 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):
:information_source: Learn More:
Time needed: 25-30 minutes
Create a restaurant ordering system that tracks your money and allows you to order food.
Set up your wallet:
myMoney
Create 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:bulb: 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: 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: 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:
:information_source: 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:bulb: 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