Wednesday, May 5, 2021

Javascript Practice 33: JS Hero - min and max

 
Question: 

Write a function midrange, that calculates the midrange of 3 numbers. The midrange is the mean of the smallest and largest number.

Example: midrange(3, 9, 1) should return (9+1)/2 = 5.




Answer: 

function midrange (a,b,c) {

let min = Math.min(a,b,c); 
let max = Math.max(a,b,c); 
let mean = (min + max) / 2;

return mean;

};

> declared the min & max variables and initialized with the values returned from using the methods below
Math.min method will return the number with the lowest value
Math.max method will return the number with the highest value
> declared the mean variable
> initialized it with a value returned from adding the two variables and then dividing the result by 2
(min + max) / 2
> in the end we return mean

No comments:

Post a Comment