arrow-functions.js 1.3 KB

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