Operation on numbers and use of template literals

javascript

Instructions:

Crate a variable x which holds random value between 1 to 100. Another variable y that holds random value between 1 to 50.

Perform basic mathematical operations on it like sum, difference, product, quotient and remainder. Log the output of each operation along with the value of x and y using template literals.

Expected result:

1 + 2 = 3
1 - 1 = 0
2 * 2 = 4
4 / 2 = 2
5 % 2 = 1

Solution:

// generate random values
const x = Math.floor(Math.random() * 100 + 1);
const y = Math.floor(Math.random() * 50 + 1);

// addition
const addValue =  x + y;
console.log(`${x} + ${y} = ${addValue}`);

// difference
const diffValue =  x - y;
console.log(`${x} - ${y} = ${diffValue}`);

// product
const productValue =  x * y;
console.log(`${x} * ${y} = ${productValue}`);

// quotient
const quoValue =  x / y;
console.log(`${x} / ${y} = ${quoValue}`);

// remainder
const remValue =  x % y;
console.log(`${x} % ${y} = ${remValue}`);

Same solution removing few lines of code and extra variables:

// generate random values
const x = Math.floor(Math.random() * 100 + 1);
const y = Math.floor(Math.random() * 50 + 1);

// addition
console.log(`${x} + ${y} = ${x + y}`);

// difference
console.log(`${x} - ${y} = ${ x - y}`);

// product
console.log(`${x} * ${y} = ${x * y}`);

// quotient
console.log(`${x} / ${y} = ${x / y}`);

// remainder
console.log(`${x} % ${y} = ${ x % y}`);

Sample output:

59 + 25 = 84
59 - 25 = 34
59 * 25 = 1475
59 / 25 = 2.36
59 % 25 = 9