Skip to content

J040 Simple Use of Functions

A straightforward task exploring functions in ES (ECMAScript).

Within your app.js:

Function Basics

  • Create a function named addTogether which takes two numbers as arguments and returns their sum.
  • Create another function named print which takes a number as an argument and prints it to the console using console.log.

These functions should be callable as follows:

let result = addTogether(5, 5);
print(result);

// or simply in one line
print(addTogether(5, 5));
  • 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.