Posts

Javascript Practice 34: JS Hero - Math.PI

  Question:  Write a function area that calculates the area of a circle. The function is given the radius of the circle. Example: area(1) should return π and area(2) should return 4 * π. Answer:  function area (radius) { let squared = Math.pow(radius,2);  let final = Math.PI * squared;  return final;  };  > Area of a Circle = π * r² > declared a variable squared and initialized it with a value returned from the Math.pow method > Math.pow(x, y) returns the value of x to the power of y; Math.pow(radius, 2) > we then declare another variable final  > we use the Math.PI method and multiply it to the squared variable to return the value which would be the area > in the end we return final

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

Javascript Practice 32: JS Hero - Math

  Question:  Write a function hypotenuse that calculates the length of the hypotenuse of a right triangle. The length of the two legs is passed to the function. Tip: In a right triangle the Pythagorean theorem is valid. If a and b are the lengths of the two legs and c is the length of the hypotenuse, the following is true: a² + b² = c². Since 3² + 4² = 5² applies, hypotenuse(3, 4) should return 5. Answer:  function hypotenuse (a, b) { let s = a**2 + b**2;  let h = Math.sqrt(s);  return h;  };  or  function hypotenuse (a,b) { let firstLength = Math.pow(a,2);  let secondLength = Math.pow(b,2);  let thirdLength = firstLength + secondLength;  let xyz = Math.sqrt(thirdLength);  return  xyz;  }; > Pythagorean Theorem: a² + b² = c² > in the first example we use the exponentiation operator (**) which raises the first operand to the power of the second operand and declare it to variable 's' > we then use the Math.sqrt...

Javascript Practice 31: JS Hero - Parentheses

  Question:   Write a function mean that takes 2 numbers and returns their mean value. Example: mean(1, 2) should return 1.5. Answer:  function mean (n1, n2) { return (n1+n2) / 2;  };  > basic mathematical operation to be used 

Javascript Practice 30: JS Hero - Modulo

  Question:  Write a function onesDigit that takes a natural number and returns the ones digit of that number. Example: onesDigit(2674) should return 4. Answer:  function onesDigit (number) { return number % 10;  };  > modulo / modulus operation returns the remainder of a division, after one number is divided by another (called the modulus of the operation) > for eg. 25 mod 10 (or 25 % 10) would evaluate to 5, because 25 divided by 10 has a quotient of 2 and a remainder of 5 > this arithmetic operation will always return the ones digit of any number (parameter) we provide > notice that doing division with a calculator will not show the result of the modulo operation, and that the quotient will be expressed as a decimal fraction if a non-zero remainder is involved. 

Javascript Practice 29: JS Hero - Fahrenheit

  Question:  Write a function toFahrenheit that converts a temperature from Celsius to Fahrenheit. Example: toFahrenheit(0) should return 32. Answer:  function toFahrenheit (celsius) { return (celsius * 1.8) + 32;  }; > Celsius to Fahrenheit formula = (C * 1.8) + 32 > return the above formula in the statement to get the Fahrenheit

Javascript Practice 28: JS Hero - Increment

  Question:  Which value does x have after execution of the following code? let x = 3; x++; x = x * 2; x--; Answer:  x has a value of 7  > x = 3 > x++:  3+1 = 4 > x = x*2; i.e. 4*2 = 8 > x--: 8-1 = 7