arrow-functions.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. try {
  2. let getNumber = () => 42;
  3. assert(getNumber() === 42);
  4. let add = (a, b) => a + b;
  5. assert(add(2, 3) === 5);
  6. const addBlock = (a, b) => {
  7. let res = a + b;
  8. return res;
  9. };
  10. assert(addBlock(5, 4) === 9);
  11. const makeObject = (a, b) => ({ a, b });
  12. const obj = makeObject(33, 44);
  13. assert(typeof obj === "object");
  14. assert(obj.a === 33);
  15. assert(obj.b === 44);
  16. let returnUndefined = () => { };
  17. assert(typeof returnUndefined() === "undefined");
  18. const makeArray = (a, b) => [a, b];
  19. const array = makeArray("3", { foo: 4 });
  20. assert(array[0] === "3");
  21. assert(array[1].foo === 4);
  22. let square = x => x * x;
  23. assert(square(3) === 9);
  24. let squareBlock = x => {
  25. return x * x;
  26. };
  27. assert(squareBlock(4) === 16);
  28. const message = (who => "Hello " + who)("friends!");
  29. assert(message === "Hello friends!");
  30. const sum = ((x, y, z) => x + y + z)(1, 2, 3);
  31. assert(sum === 6);
  32. const product = ((x, y, z) => {
  33. let res = x * y * z;
  34. return res;
  35. })(5, 4, 2);
  36. assert(product === 40);
  37. const half = (x => {
  38. return x / 2;
  39. })(10);
  40. assert(half === 5);
  41. console.log("PASS");
  42. } catch {
  43. console.log("FAIL");
  44. }