Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // --- Preparation for JS Interview Tasks ---
- // Task 1: Iterate over the array below and if the current number is divisible by 3, print 'Fizz', if its
- // divisible by 5, print 'Buzz', if it is divisible by both 3 and 5, print 'FizzBuzz'
- const fizzBuzzArray = [1, 2, 3, 4, 5, 7, 8, 13, 15];
- // fizzBuzzArray.forEach(n => {
- // if (n % 3 === 0 && n % 5 === 0) {
- // console.log('FizzBuzz');
- // } else if (n % 3 === 0) {
- // console.log('Fizz');
- // } else if (n % 5 === 0) {
- // console.log('Buzz');
- // }
- // });
- // Task 2: Generate the first 10 fibonacci numbers
- // let f1 = 0;
- // let f2 = 1;
- // const fibonacci = Array.from({ length: 10 }, () => {
- // let temp = f2;
- // f2 += f1;
- // f1 = temp;
- // return f2 - f1;
- // });
- // console.log(fibonacci);
- // Task 3: Generate the first 15 fibonacci numbers, then filter only the even numbers and transform them
- // into an objects that have `number` property - { number: 1 }
- // let f1 = 0;
- // let f2 = 1;
- // const fibonacci = Array.from({ length: 15 }, () => {
- // let temp = f2;
- // f2 += f1;
- // f1 = temp;
- // return f2 - f1;
- // }).filter(n => n % 2 !== 1).map(n => ({ number: n }));
- // console.log(fibonacci);
- // Task 4: If you have the function `add`, your task is to create the behavior that whenever the function
- // is invoked, to not only execute it, but log the times that function was called, without
- // changing the `add` function directly
- // const add = (a, b) => a + b;
- // const getFuncWithCounterLog = (func) => {
- // let counter = 0;
- // return (a, b) => {
- // console.log(++counter);
- // return func(a, b);
- // };
- // };
- // const addWithLogger = getFuncWithCounterLog(add);
- // addWithLogger(1, 2);
- // addWithLogger(1, 2);
- // addWithLogger(1, 2);
- // addWithLogger(1, 2);
- // addWithLogger(1, 2);
- // addWithLogger(1, 2);
- // Task 5: If you have the function `add`, your task is to create the behavior that when the function
- // is called the first time with certain parameters and return a certain value, to store the
- // parameters and the return value and if the function is called again with the same parameters
- // to immediately return the stored return value, without invoking the function again
- // const add = (a, b) => a + b;
- // const getAddWithMemoization = (func) => {
- // const cache = {};
- // return (a, b) => {
- // const key = `${a} + ${b}`;
- // if (cache.hasOwnProperty(key)) {
- // return cache[key];
- // } else {
- // const sum = func(a, b);
- // cache[key] = sum;
- // return sum;
- // }
- // };
- // };
- // const addWithMemoization = getAddWithMemoization(add);
- // addWithMemoization(1, 2);
- // addWithMemoization(1, 2);
- // addWithMemoization(1, 2);
- // addWithMemoization(1, 2);
- // addWithMemoization(1, 2);
- // Task 6: Use promises to resolve the number `5` after 1 second and log it in the console
- // const promise = new Promise((resolve, reject) => {
- // setTimeout(() => {
- // resolve('5');
- // }, (1000));
- // });
- // promise.then(console.log);
- // const func = async () => {
- // console.log(await promise);
- // };
- // func();
- // Task 7: Use async-await to reproduce the same behaviour
- // const promise = new Promise((resolve, reject) => {
- // setTimeout(() => {
- // resolve('5');
- // }, (1000));
- // });
- // promise.then(console.log);
- // const func = async () => {
- // console.log(await promise);
- // };
- // func();
- // Task 8: Use async-await to get 2 numbers asynchronously, sum their value when they come
- // and return it as a result from the async function
- // const p1 = new Promise((resolve, reject) => {
- // setTimeout(() => {
- // resolve(2);
- // }, 1000);
- // });
- // const p2 = new Promise((resolve, reject) => {
- // setTimeout(() => {
- // resolve(3);
- // }, 5000);
- // });
- // const getSum = async (p1, p2) => {
- // console.time('promise');
- // // const [num1, num2] = await Promise.all([p1, p2]);
- // const num1 = await p1;
- // const num2 = await p2;
- // console.timeEnd('promise');
- // return num1 + num2;
- // };
- // getSum(p1, p2).then(console.log);
- // Task 9: Write a function that accepts an array of numbers and a target sum and returns a boolean that indicates
- // if there is a sum between 2 of the numbers in the array that is equal to the target sum
- // const func = (arr, targetSum) => {
- // const hasTargetSum = arr.reduce(((acc, el, i1) => {
- // if (arr.some((v, i2) => i1 !== i2 && el + v === targetSum)) {
- // acc = true;
- // }
- // return acc;
- // }), false);
- // return hasTargetSum;
- // };
- // console.log(func([1, 2, 3, 4, 5, 6, 7, 8, 9], 3));
- // HOMEWORK: Implement stack, queue and linked list data structures
- // HOMEWORK: Solve the previous task but with unknown count of numbers that can result
- // to the target sum ( not just 2 ) - https://www.youtube.com/watch?v=s6FhG--P7z0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement