Advertisement
elena1234

Pure functions - JavaScript

Nov 30th, 2021
1,034
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let solution = (() => {
  2.     return {
  3.         add: (vec1, vec2) => [vec1[0] + vec2[0], vec1[1] + vec2[1]],
  4.         multiply: (vec1, scalar) => [vec1[0] * scalar, vec1[1] * scalar],
  5.         length: (vec1) => Math.sqrt(vec1[0] * vec1[0] + vec1[1] * vec1[1]),
  6.         dot: (vec1, vec2) => vec1[0] * vec2[0] + vec1[1] * vec2[1],
  7.         cross: (vec1, vec2) => vec1[0] * vec2[1] - vec2[0] * vec1[1],
  8.     }
  9. })()
  10.  
  11. console.log(
  12.     solution.add([1, 1], [1, 0]),
  13.     solution.multiply([3.5, -2], 2),
  14.     solution.length([3, -4]),
  15.     solution.dot([1, 0], [0, -1]),
  16.     solution.cross([3, 7], [1, 0])
  17. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement