Javascript Algorithm Geometry calculate Hypotenuse

Introduction

calculateHypotenuse(a,b) will return the length of the hypotenuse for a right angled triangle with the other two sides having a length equal to the inputs.

The returned value should be a number rounded to three decimal places

An error should be thrown if an invalid input is provided.

calculateHypotenuse(1,1); // returns 1.414
calculateHypotenuse(3,4); // returns 5
calculateHypotenuse(-2,1); // throws error
calculateHypotenuse("one", "two"); // throws error
'use strict'//from  www  .  java2 s.  c o m

function calculateHypotenuse(a, b) {
  let isNumber = typeof a === 'number' && typeof b === 'number';
  let isPositive = a > 0 && b > 0;
  if(!isNumber || !isPositive) throw 'error: invalid input';

  let myAnswer = Math.sqrt(a * a + b * b);
  return Math.round(myAnswer * 1000) / 1000;
}

console.log(calculateHypotenuse(1, 1));
console.log(calculateHypotenuse(3, 4));

// test errors
console.log(calculateHypotenuse(-2, 1));
console.log(calculateHypotenuse('one', 'two'));
console.log(calculateHypotenuse('one', 1));



PreviousNext

Related