take the cosine of a number in javascript

To take the cosine of a number in JavaScript, you can use the built-in Math.cos() function. This function returns the cosine of an angle, which is specified in radians.

Here is an example JavaScript function that takes an angle in degrees and returns the cosine of that angle:

index.tsx
function cos(degrees) {
  var radians = degrees * (Math.PI / 180);
  return Math.cos(radians);
}
97 chars
5 lines

You can call this function with any angle in degrees that you want to find the cosine of, like this:

index.tsx
var angle = 45; // in degrees
var cosAngle = cos(angle); // the cosine of 45 degrees
console.log(cosAngle);
108 chars
4 lines

This will output the cosine of 45 degrees to the console, which should be approximately 0.707.

gistlibby LogSnag