Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function excercise1(human) {
- console.log(human.name);
- console.log(human.age);
- console.log(human.fatherName);
- human.friends.forEach(console.log);
- human.hobbies.forEach(hobby => hobby())
- }
- const animal = {
- name: "Levi",
- age: 21,
- friends: ['Beni', 'Tamas', 'Zsombi'],
- fatherName: 'Béla',
- hobbies: [
- function () { console.log('Programozas') },
- function () { console.log('Peca') }
- ]
- }
- // excercise1(animal)
- function isPrime(num) {
- if (isNaN(num) || !isFinite(num) || num % 2 === 0 || num <= 1 || Math.sqrt(num) * Math.sqrt(num) === num)
- return false;
- if (num === 2)
- return true
- for (let index = 3; index < Math.sqrt(num); index += 2) {
- if (num % index === 0)
- return false;
- }
- return true;
- }
- function range(a, b) {
- let array = []
- for (let index = a; index <= b; index++) {
- const element = index;
- array.push(element);
- }
- return array;
- }
- function excercise2() {
- /* TIP: there are some builtin helper functions like isNaN() and isFinite(), Math.sqrt() */
- const arr = range(4, 10); // should return [4,5,6,7,8,9,10]
- console.log(arr);
- console.log(isPrime(6));
- console.log(isPrime(6 / 5));
- console.log(isPrime(7));
- console.log(isPrime(997525853));
- console.log(isPrime(-10));
- console.log(isPrime('SURPISE!'));
- console.log(isPrime({ hello: [1, 2, 3] }));
- }
- // excercise2()
- function makeCounter(number = 0) {
- let value = number
- return {
- value: function () { return value },
- increment: function () { value++; },
- decrement: function () { value-- },
- };
- }
- function excercise3() {
- const counter1 = makeCounter();
- const counter2 = makeCounter(10);
- console.log(0, counter1.value()); // 0
- counter1.increment();
- counter1.increment();
- console.log(2, counter1.value()); // 2
- counter1.decrement();
- console.log(1, counter1.value()); // 1
- console.log(10, counter2.value()); // 10
- }
- // excercise3()
- function map(array, myFunc) {
- let newArray = [];
- for (let index = 0; index < array.length; index++) {
- const element = myFunc(array[index]);
- newArray.push(element);
- }
- return newArray;
- }
- function filter(array, myFunc) {
- let newArray = [];
- for (let index = 0; index < array.length; index++) {
- if (myFunc(array[index])){
- newArray.push(array[index]);
- }
- }
- return newArray;
- }
- function excercise4() {
- const array = [1, 2, 3, 4];
- console.log(map(array, (x) => x * 2), [2, 4, 6, 8]);
- console.log(map(array, (x) => x + 1), [2, 3, 4, 5]);
- console.log(map(array, (x) => `number ${x}`), ['number 1', 'number 2', 'number 3', 'number 4']);
- const array2 = range(3, 6)
- console.log(filter(array, (x) => x % 2 === 0), [2, 4]);
- console.log(filter(array, (x) => array2.includes(x)), [3, 4]);
- }
- excercise4();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement