Why are functions like Math.cos() and Math.sin() not working on array? So if I have an array, do I have to loop through to compute the cosine of all numbers in the array? Is there a distinction between arrays and matrices? Can I define a 2-dimensional matrix and access the elements using subscripts like x[2,3] for 2nd row and 3rd column?
1 Answer
- Yes you have to iterate yourself. The
Mathtrig functions work on individual values only. - JavaScript really has only one-dimensional arrays, but values of a one-dimensional array can be arrays. That gives an effect like multi-dimensional arrays, but it's really not exactly the same thing. (For most programming purposes, it's close enough.)
- Multiple dimensions are accessed as
myArray[1][2], not with a comma-separated list of indexes.
1 Comment
Pointy
@canon they accept multiple arguments, but they do not accept arrays (unless they're called via
.apply() of course). The point is that they're not "vector" operations. Still, edited to stipulate "trig functions".