Advertisement
tankian202

Untitled

Mar 18th, 2024
494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function excercise1(human) {
  2.     console.log(human.name);
  3.     console.log(human.age);
  4.     console.log(human.fatherName);
  5.     human.friends.forEach(console.log);
  6.     human.hobbies.forEach(hobby => hobby())
  7. }
  8.  
  9. const animal = {
  10.     name: "Levi",
  11.     age: 21,
  12.     friends: ['Beni', 'Tamas', 'Zsombi'],
  13.     fatherName: 'BΓ©la',
  14.     hobbies: [
  15.         function () { console.log('Programozas') },
  16.         function () { console.log('Peca') }
  17.     ]
  18. }
  19.  
  20. // excercise1(animal)
  21.  
  22.  
  23.  
  24. function isPrime(num) {
  25.     if (isNaN(num) || !isFinite(num) || num % 2 === 0 || num <= 1 || Math.sqrt(num) * Math.sqrt(num) === num)
  26.         return false;
  27.     if (num === 2)
  28.         return true
  29.     for (let index = 3; index < Math.sqrt(num); index += 2) {
  30.         if (num % index === 0)
  31.             return false;
  32.     }
  33.     return true;
  34. }
  35.  
  36. function range(a, b) {
  37.     let array = []
  38.     for (let index = a; index <= b; index++) {
  39.         const element = index;
  40.         array.push(element);
  41.     }
  42.     return array;
  43. }
  44.  
  45.  
  46. function excercise2() {
  47.     /* TIP: there are some builtin helper functions like isNaN() and isFinite(), Math.sqrt() */
  48.     const arr = range(4, 10); // should return [4,5,6,7,8,9,10]
  49.     console.log(arr);
  50.     console.log(isPrime(6));
  51.     console.log(isPrime(6 / 5));
  52.     console.log(isPrime(7));
  53.     console.log(isPrime(997525853));
  54.     console.log(isPrime(-10));
  55.     console.log(isPrime('SURPISE!'));
  56.     console.log(isPrime({ hello: [1, 2, 3] }));
  57. }
  58.  
  59. // excercise2()
  60.  
  61. function makeCounter(number = 0) {
  62.     let value = number
  63.     return {
  64.         value: function () { return value },
  65.         increment: function () { value++; },
  66.         decrement: function () { value-- },
  67.     };
  68. }
  69.  
  70.  
  71.  
  72. function excercise3() {
  73.     const counter1 = makeCounter();
  74.     const counter2 = makeCounter(10);
  75.  
  76.     console.log(0, counter1.value()); // 0
  77.  
  78.     counter1.increment();
  79.     counter1.increment();
  80.     console.log(2, counter1.value()); // 2
  81.  
  82.     counter1.decrement();
  83.     console.log(1, counter1.value()); // 1
  84.     console.log(10, counter2.value()); // 10
  85. }
  86.  
  87. // excercise3()
  88.  
  89. function map(array, myFunc) {
  90.     let newArray = [];
  91.     for (let index = 0; index < array.length; index++) {
  92.         const element = myFunc(array[index]);
  93.         newArray.push(element);
  94.     }
  95.     return newArray;
  96. }
  97.  
  98. function filter(array, myFunc) {
  99.     let newArray = [];
  100.     for (let index = 0; index < array.length; index++) {
  101.         if (myFunc(array[index])){
  102.             newArray.push(array[index]);
  103.         }
  104.     }
  105.     return newArray;
  106. }
  107.  
  108. function excercise4() {
  109.     const array = [1, 2, 3, 4];
  110.     console.log(map(array, (x) => x * 2), [2, 4, 6, 8]);
  111.     console.log(map(array, (x) => x + 1), [2, 3, 4, 5]);
  112.     console.log(map(array, (x) => `number ${x}`), ['number 1', 'number 2', 'number 3', 'number 4']);
  113.  
  114.     const array2 = range(3, 6)
  115.     console.log(filter(array, (x) => x % 2 === 0), [2, 4]);
  116.     console.log(filter(array, (x) => array2.includes(x)), [3, 4]);
  117. }
  118.  
  119. excercise4();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement