In: Computer Science
(Javascript) Write a function that makes the following calculation: param1 + param2 - param3 + param4 – param5…and keeps with this pattern. If any non-numerical character is encountered, ¼ of its value is computed as the numerical value. The result should truncate precision.
Solution
function expression(param1, param2, param3, param4, param5,
param6)
{
if (param1 !== parseInt(param1)) param1 = param1.charCodeAt(0) /
4;
if (param2 !== parseInt(param2)) param2 = param2.charCodeAt(0) /
4;
if (param3 !== parseInt(param3)) param3 = param3.charCodeAt(0) /
4;
if (param4 !== parseInt(param4)) param4 = param4.charCodeAt(0) /
4;
if (param5 !== parseInt(param5)) param5 = param5.charCodeAt(0) /
4;
if (param6 !== parseInt(param6)) param6 = param6.charCodeAt(0) /
4;
return Math.floor(param1 + param2 - param3 + param4 - param5 +
param6);
}
console.log(expression(11, 'a', 2, 30, 'z', 3));
Screenshot and Output
--
--
all the best