J040 Simple Use of Functions
A straightforward task exploring functions in ES (ECMAScript).
Within your app.js:
Function Basics
- Create a function named
addTogetherwhich takes two numbers as arguments and returns their sum. - Create another function named
printwhich takes a number as an argument and prints it to the console usingconsole.log.
These functions should be callable as follows:
- Create an additional function called
priceWithVAT. This function should take a price and a VAT percentage as its arguments and return the price inclusive of the VAT.
Solution
"use strict";
function addTogether(a, b) {
return a + b;
}
function print(value) {
console.log(value);
}
function priceWithVAT(price, vatPercentage) {
return price + (price * (vatPercentage / 100));
}
let result = addTogether(5, 5);
print(result);
print(addTogether(5, 5));
let price = 100; // example price
let vat = 20; // example VAT percentage
print(priceWithVAT(price, vat)); // prints 120 assuming the price is 100 and VAT is 20%
This script introduces and demonstrates simple function definitions and invocations in JavaScript.