Full Stack Daily Morning 16-01-2023

Full Stack Daily Morning 16-01-2023

Scratchpad #2

// let age = 30;

// if (age >= 21) {
//     console.log('You are allowed to enter');
// }

// console.log('Program ends');

// let num = 21;

// if (num % 2 === 0) {
//     console.log('EVEN');
// }

// let age = 2;

// if (age >= 65) {
//     console.log('Drinks are free');
// } else if (age >= 21) {
//     console.log('You are allowed to enter and drink');
// } else if (age >= 18) {
//     console.log("You can enter but you can't drink");
// } else {
//     console.log("You aren't allowed");
// }

// let password = 'helloworld@123';

// if (password.length >= 6) {
//     if (password.indexOf(' ') !== -1) {
//         console.log('Password cannot contain spaces');
//     } else {
//         console.log('Valid password');
//     }
// } else {
//     console.log('Password is too short');
// }

// let loggedInUser = null;

// if (loggedInUser) {
//     console.log('Welcome. This is your dashboard.');
// } else {
//     console.log('Please login to access dashboard');
// }

// let age = 35;

// if (age >= 18 && age < 21) {
//     console.log("You can enter but you can't drink");
// } else if (age >= 21 && age < 65) {
//     console.log('You are allowed to enter and drink');
// } else if (age >= 65) {
//     console.log('Drinks are free');
// } else {
//     console.log("You aren't allowed");
// }

// let day = 100;

// switch (day) {
//     case 1:
//         console.log('Monday');
//         break;
//     case 2:
//         console.log('Tuesday');
//         break;
//     case 3:
//         console.log('Wednesday');
//         break;
//     case 4:
//         console.log('Thursday');
//         break;
//     case 5:
//         console.log('Friday');
//         break;
//     case 6:
//         console.log('Saturday');
//         break;
//     case 7:
//         console.log('Sunday');
//         break;
//     default:
//         console.log('Invalid day code');
// }

// if (day === 1) {
//     console.log('Monday');
// } else if (day === 2) {
//     console.log('Tuesday');
// } else if (day === 3) {
//     console.log('Wednesday');
// } else if (day === 4) {
//     console.log('Thursday');
// }

// let num = 32;

// num === 7 ? console.log('LUCKY') : console.log('UNLUCKY');

// if (num === 7) {
//     console.log('LUCKY');
// } else {
//     console.log('UNLUCKY');
// }

let currentStatus = 'idle';
// let color;
let color =
    currentStatus === 'offline'
        ? 'red'
        : currentStatus === 'online'
        ? 'green'
        : 'yellow';

// if (currentStatus === 'offline') {
//     color = 'red';
// } else if (currentStatus === 'online') {
//     color = 'green';
// } else {
//     color = 'yellow';
// }

console.log(color);

Scratchpad #3

// // // const product = {
// // //     imageUrl: 'https:.//.',
// // //     brand: 'Apple',
// // //     description: 'Some desc....',
// // //     price: 100000,
// // //     inStock: 500,
// // //     discounted: true,
// // //     name: 'iPhone 14',
// // //     'hello world': true,
// // //     1000: false,
// // // };

// // // const product2 = [
// // //     'iPhone 14',
// // //     'Apple',
// // //     'Some description...',
// // //     100000,
// // //     500,
// // //     true,
// // //     'https://....png',
// // // ];

// // // const names = ['john', 'jane', 'jack', 'jill'];

// // // const names = {0: 'john', 1: 'jane', 2: 'jack', 3: 'jill', length: 4}

// // const pallette = {
// //     red: '#eb4d4b',
// //     yellow: '#f9ca24',
// //     blue: '#30336b',
// // };

// // let color = 'yellow';

// // console.log(pallette[color]);

// // for (let i = 10; i >= 0; i--) {
// //     console.log(i, 'Hello World');
// // }

// // const nums = [12, 34, 56, 34, 78, 54, 23, 12];

// // for (let i = 0; i < nums.length; i++) {
// //     console.log(nums[i]);
// // }

// // const movies = [
// //     { movieName: 'Inception', rating: 3.8 },
// //     { movieName: 'Avengers', rating: 3.4 },
// //     { movieName: 'Iron Man', rating: 2.9 },
// // ];

// // for (let i = 0; i < movies.length; i++) {
// //     const movie = movies[i];
// //     console.log(`${movie.movieName} has a rating of ${movie.rating}`);
// // }

// // const word = 'hello world';

// // let reveredWord = '';
// // for (let i = word.length - 1; i >= 0; i--) {
// //     reveredWord += word[i];
// // }

// // console.log(reveredWord);

// // for (let i = 0; i < 5; i++) {
// //     console.log(`Outer Loop: ${i}`);

// //     for (let j = 0; j < 5; j++) {
// //         console.log(`        Inner loop: ${j}`);
// //     }
// // }

// // const gameBoard = [
// //     [4, 64, 8, 4],
// //     [128, 32, 4, 16],
// //     [16, 4, 4, 32],
// //     [2, 16, 16, 2],
// // ];

// // let total = 0;

// // for (let i = 0; i < gameBoard.length; i++) {
// //     for (let j = 0; j < gameBoard[i].length; j++) {
// //         total += gameBoard[i][j];
// //     }
// // }

// // console.log(total);

// const gameBoard = [
//     [
//         [4, 64, 8, 4],
//         [4, 64, 8, 4, 123, 3],
//         [4, 64, 8, 4],
//         [4, 64, 8, 4],
//         [4, 64, 8, 4],
//         [4, 64, 8, 4],
//         [4, 64, 8, 4],
//         [4, 64, 8, 4],
//     ],
//     [
//         [4, 64, 8, 4],
//         [4, 64, 8, 4],
//         [4, 64, 8, 4],
//         [4, 64, 8, 4],
//         [4, 64, 8, 4],
//     ],
//     [
//         [4, 64, 8, 4],
//         [4, 64, 8, 4],
//         [4, 64, 8, 4],
//         [4, 64, 8, 4],
//         [4, 64, 8, 4],
//     ],
//     [
//         [4, 64, 8, 4],
//         [4, 64, 8, 4],
//         [4, 64, 8, 4],
//         [4, 64, 8, 4],
//         [4, 64, 8, 4],
//     ],
// ];

// // loop over and print all the numbers
// // total
// // total even numbers
// // total evens and odds

// let evens = 0;
// let odds = 0;

// // -------------------------------

// // -------------------------------

// for (let i = 0; i < 10; i++) {
//     console.log(i);
// }

// let i = 0;

// while (i < 10) {
//     console.log(i);
//     i++;
// }

// let target = Math.floor(Math.random() * 10) + 1;
// let guess = Math.floor(Math.random() * 10) + 1;

// while (guess !== target) {
//     console.log(`Target: ${target} | Guess: ${guess}`);
//     if (guess === 1) {
//         break;
//     }
//     guess = Math.floor(Math.random() * 10) + 1;
// }

// console.log(`Game over. Target: ${target} | Guess: ${guess}`);

let target = Math.floor(Math.random() * 10) + 1;
let guess = Math.floor(Math.random() * 10) + 1;

while (true) {
    console.log(`Target: ${target} | Guess: ${guess}`);
    if (target === guess) {
        break;
    }
    guess = Math.floor(Math.random() * 10) + 1;
}

console.log(`Game over. Target: ${target} | Guess: ${guess}`);

Scratchpad #4

let categories = [
    'fashion',
    'electronics',
    'mobiles',
    'books',
    'toys',
    'groceries',
];

// for (let i = 0; i < categories.length; i++) {
//     console.log(categories[i]);
// }

// for (let category of categories) {
//     console.log(category);
// }

// for (let char of 'hello') {
//     console.log(char);
// }

// const matrix = [
//     [1, 4, 7],
//     [9, 7, 2],
//     [9, 4, 6],
// ];

// for (let row of matrix) {
//     for (let col of row) {
//         console.log(col);
//     }
// }

// const cats = ['fashion', 'mobiles', 'books'];
// const prods = ['tshirt', 'samsung', '1984'];

// for (let i = 0; i < cats.length; i++) {
//     console.log(cats[i], prods[i]);
// }

const productPrices = {
    Apple: 80000,
    OnePlus: 50000,
    Samsung: 90000,
};

// for (let item of Object.keys(productPrices)) {
//     console.log(item);
// }

// for (let item of Object.values(productPrices)) {
//     console.log(item);
// }

// for (let key of Object.keys(productPrices)) {
//     console.log(key, productPrices[key]);
// }

// for (let key in productPrices) {
//     console.log(key, productPrices[key]);
// }

// const movieRating = {
//     pursuitOfHappiness: 4.8,
//     satya: 4.8,
//     gangsOfWasepur: 4,
//     robot: -3,
// };

// for (let movie in movieRating) {
//     // console.log(movie)
//     console.log(`${movie} has a rating of ${movieRating[movie]}`);
// }

// function greet() {
//     console.log('Hello');
//     console.log('World');
//     console.log('Universe');
// }

// for (let i = 0; i < 10; i++) {
//     greet();
// }

// function rollDie() {
//     let roll = Math.floor(Math.random() * 6) + 1;
//     console.log(`Rolled: ${roll}`);
// }

// function throwDice() {
//     rollDie();
//     rollDie();
//     rollDie();
// }

// throwDice();

// function greet(name) {
//     console.log(`Hello, ${name}`);
// }

// greet('John');
// greet('Jane');

// function rollDie() {
//     let roll = Math.floor(Math.random() * 6) + 1;
//     console.log(`Rolled: ${roll}`);
// }

// function throwDice(count) {
//     for (let i = 0; i < count; i++) {
//         rollDie();
//     }
// }

// throwDice(5);

// function greet(firstName, lastName) {
//     return `Hello, ${firstName} ${lastName}`;
// }

// console.log(greet('jill', 'smith'));

// // const result = greet('John', 'Doe');

// // console.log('Result =>', result);

// function greet(firstName, lastName) {
//     console.log('Test');
//     return `Hello, ${firstName} ${lastName}`;
// }

// console.log(greet('Jane', 'Doe'));

// let fullName = 'John Doe';

// function greet() {
//     let fullName = 'Jane Smith';
//     console.log(fullName);
// }

// console.log(fullName);

// greet();

// let fullName = 'John Doe';

// if (true) {
//     var fullName = 'Jane Smith';
//     console.log(fullName);
// }

// console.log(fullName);

// for (var i = 0; i < 10; i++) {
//     console.log(i);
// }

// console.log(i);

// function outer() {
//     function inner() {
//         let movie = 'Iron Man';
//         console.log(movie);
//     }

//     inner();
// }

// outer();

// function greet() {
//     console.log('hello world');
// }

// const test1 = 100;

// const test = greet;

// test();

// const testFunc = function () {
//     console.log('hello world');
// };

// testFunc();

// const arr = [
//     10,
//     'hello',
//     function () {
//         return 'hello world';
//     },
// ];

// console.log(arr[2]());

// const arr = [
//     function (a, b) {
//         return a + b;
//     },
//     function (a, b) {
//         return a - b;
//     },
//     function (a, b) {
//         return a * b;
//     },
//     function (a, b) {
//         return a / b;
//     },
// ];

// console.log(arr[3](10, 5));

const math = {
    add: function (a, b) {
        return a + b;
    },
    sub: function (a, b) {
        return a - b;
    },
    PI: 3.14,
    mul: function (a, b) {
        return a * b;
    },
};

console.log(math.mul(10, 5));
console.log(Math.PI);

// rahul@rstforum.co.in

Scratchpad #5

// // function math(a, b, fn) {
// //     return fn(a, b);
// // }

// // function add(a, b) {
// //     return a + b;
// // }

// // console.log(math(10, 5, add));

// // function repeat(func, num) {
// //   for (let i = 0; i < num; i++) {
// //     func()
// //   }
// // }

// // function sayHello() {
// //   console.log('Hello World!')
// // }
// // function sayGoodbye() {
// //   console.log('Bye World!')
// // }

// // repeat(sayHello, 10)
// // repeat(sayGoodbye, 5)

// // function randomPick(f1, f2) {
// //   let randNum = Math.random()
// //   if (randNum < 0.5) {
// //     f1()
// //   } else {
// //     f2()
// //   }
// // }

// // randomPick(sayHello, sayGoodbye)

// // function raiseBy(num) {
// //     return function (x) {
// //         return x ** num;
// //     };
// // }

// // const square = raiseBy(2);
// // const cube = raiseBy(3);

// // console.log(cube(10));

// function isBetween(x, y) {
//     return function (num) {
//         return num >= x && num < y;
//     };
// }

// const isUnderAge = isBetween(0, 18);
// const isLegalAge = isBetween(18, 21);

// // console.log(isUnderAge(35));
// console.log(isLegalAge(35));

// function isBetween(x, y) {
//   return function (num) {
//     return num >= x && num <= y
//   }
// }

// const isUnderAge = isBetween(0, 18)
// const canEnterButNotDrink = isBetween(18, 21)
// const canDrink = isBetween(21, 65)
// const isSenior = isBetween(65, 100)

// console.log(isUnderAge(5))
// console.log(canEnterButNotDrink(20))
// console.log(canDrink(30))
// console.log(isSenior(70))

// function math(a, b, fn) {
//     return fn(a, b);
// }

// function add(a, b) {
//     return a + b;
// }

// console.log(math(10, 5, add));

// setTimeout(function () {
//     console.log('Hello World');
// }, 3000);

// function add(a, b) {
//     return a + b;
// }

// console.log(add(10, 5));

// var hello;

// console.log(hello);
// var hello = 'world';

// const nums = [9, 2, 4, 6, 2, 3, 7, 6];

// // for (let num of nums) {
// //     console.log(num);
// // }

// nums.forEach(function (num) {
//     console.log(num);
// });

// const movies = [
//     {
//         title: 'Avengers',
//         rating: 3.8,
//     },
//     {
//         title: 'Dr. Strange',
//         rating: 3.9,
//     },
//     {
//         title: 'Tenet',
//         rating: 4.3,
//     },
//     {
//         title: 'Joker',
//         rating: 4.7,
//     },
// ];

// movies.forEach(function (movie, i) {
//     console.log(`${i} - ${movie.title} has a rating of ${movie.rating}.`);
// });

// const names = ['john', 'jack', 'jane', 'james'];

// const upperNames = names.map(function (name) {
//     // ['JOHN', 'JACK', 'JANE', 'JAMES']
//     return name.toUpperCase();
// });

// const upperNames = [];

// for (let name of names) {
//     upperNames.push(name.toUpperCase());
// }

// console.log(upperNames);

// const nums = [2, 3, 4, 7, 6, 8, 13, 10, 19, 12, 14, 22, 21, 16];

// const doubles = nums.map(function (num) {
//     return num * 2;
// });

// console.log(doubles);

// const numDetails = nums.map(function (num) {
//     return { number: num, isEven: num % 2 === 0 };
// });

// console.log(numDetails);

// function square(num) {
//     return num ** 2;
// }

// const square = n => n ** 2;

// console.log(square(10));

// const nums = [2, 3, 4, 7, 6, 8, 13, 10, 19, 12, 14, 22, 21, 16];

// const doubles = nums.map(function (num) {
//     return num * 2;
// });

// const doubles2 = nums.map(num => num * 2);

// const nums = [1,2,3,4,5,6]

// // using regular function expressions
// const doubles1 = nums.map(function (n) {
//   return n * 2
// })

// // using arrow functions
// const doubles2 = nums.map(n => {
//   return n * 2
// })

// // using arrow function's one-liner implicit return
// const doubles3 = nums.map(n => n * 2)
// const parityList = nums.map(n => n % 2 === 0 ? 'Even' : 'Odd')

// let movies = ['The Terminator', 'The Avengers', 'Jurassic Park', 'Titanic'];

// // const result = movies.find((movie) => movie.includes('Park'));
// // const result = movies.find((movie) => movie[0] === 'T');
// const result = movies.find((movie) => movie.indexOf('Hello') === 0);
// console.log(result);

// const books = [
//     {
//         title: 'The Shining',
//         author: 'Stephen King',
//         rating: 4.1,
//     },
//     {
//         title: 'Sacred Games',
//         author: 'Vikram Chandra',
//         rating: 4.5,
//     },
//     {
//         title: '1984',
//         author: 'George Orwell',
//         rating: 4.9,
//     },
//     {
//         title: 'The Alchemist',
//         author: 'Paulo Coelho',
//         rating: 3.5,
//     },
//     {
//         title: 'The Great Gatsby',
//         author: 'F. Scott Fitzgerald',
//         rating: 3.8,
//     },
// ];

// // const result = books.find((book) => book.author.includes('King'));
// // const result = books.find((book) => book.rating >= 4);
// const result = books.filter((book) => book.rating >= 4);
// console.log(result);

// const goodBook = books.filter(b => b.rating >= 4.3)

// const georgeBooks = books.filter(b => (
//   b.author.includes('George')
// ))

// const lowRated = books.filter(book => {
//   return book.rating < 4
// })

// let query = 'the'
// const filteredBooks = books.filter(book => {
//   const title = book.title.toLowerCase()
//   return title.includes(query)
// })

// console.log(goodBook)
// console.log(georgeBooks)
// console.log(lowRated)
// console.log(booksWithThe)

// const names = ['jack', 'james', 'john', 'jane', 'josh', 'jrad'];

// const result = names.every((name) => name[0] === 'j');
// console.log(result);

// const bestBooks = books.every((book) => book.rating >= 4);

// const notAuthor = books.every(
//     (book) => book.author.toLowerCase() !== 'chetan bhagat'
// );

// console.log(bestBooks);
// console.log(notAuthor);

// const names = ['jack', 'james', 'john', 'jane', 'josh', 'jrad'];

// const result = names.some((name) => name[0] === 'b');
// console.log(result);

const prices = [500.4, 211, 23, 5, 4, 22.2, -23.2, 9233];

prices.sort((a, b) => b - a);
console.log(prices);

Scratchpad #6

// // const nums = [3, 2, 4, 6, 9];

// // acc       currVal
// // 3            2
// // 5            4
// // 9            6
// // 15            9
// // 24

// // let total = nums.reduce((acc, currVal) => acc - currVal);
// // console.log(total);

// // let total = 0;
// // for (let num of nums) {
// //     total += num;
// // }

// // console.log(total);

// // let nums = [21, 221, 2, 1, 34, 123, 4342, 56, 4];

// // acc     currVal
// // 21                221
// // 221            2
// // 221            1
// // 221            34
// // 221

// // const result = nums.reduce((acc, currVal) => {
// //     if (currVal > acc) {
// //         return currVal;
// //     }
// //     return acc;
// // });

// // console.log(result);

// // const nums = [3, 2, 4, 6, 9];

// // let total = nums.reduce((acc, currVal) => acc + currVal, 100);
// // console.log(total);

// // function multiply(a, b) {
// //     if (typeof b === 'undefined') {
// //         b = 1;
// //     }
// //     return a * b;
// // }

// // function multiply(a, b = 3) {
// //     return a * b;
// // }

// // console.log(multiply(12));

// function printNames(a, b, c) {
//     console.log(a);
//     console.log(b);
//     console.log(c);
// }

// // const names = ['john', 'jack', 'jane'];

// // // printNames(names[0], names[1], names[2]);
// // // printNames(...names);
// printNames(...'hello');

// // // printNames('john', 'jack', 'jane');

// // const nums = [5, 10, 11, 2, 1];

// // console.log(Math.max(...nums));

// const user = {
//     firstName: 'John',
//     lastName: 'Doe',
//     age: 20,
// };

// const users = [user, user, user, user];

// // console.log(users[1].age);

// users[1].firstName = 'Abhishek';

// console.log(users[0].firstName);

// function add(...nums) {
//     return nums.reduce((acc, currVal) => acc + currVal);
// }

// console.log(add(10, 5, 56, 23, 100));

// function add(a, b, ...nums) {
//     console.log(a, b, nums);
// }

// add(10, 20, 30, 40, 50, 60);

// const users = ['john', 'jane', 'jack'];

// const admin = users[0];
// const mod = users[1];
// const user = users[2];

// const [admin, mod, user] = users;
// console.log(admin, mod, user);

// const [admin, , user] = users;
// console.log(admin, user);

// const [admin, ...others] = users;
// console.log(admin, others);

// const user = {
//     firstName: 'John',
//     lastName: 'Doe',
//     email: 'john.doe@gmail.com',
//     phone: 99982234567,
// };

// const { firstName, lastName, ...others } = user;

// console.log(firstName, lastName, others);

function printProfile({ firstName, lastName, age, company }) {
    console.log(
        `Hello, my name is ${firstName} ${lastName} and I am ${age} years old. I work for ${company}.`
    );
}

printProfile({ firstName: 'Jane', lastName: 'Doe', age: 20, company: 'Apple' });

Scratchpad #7

// // // const movieReviews = [4.5, 5.0, 3.2, 2.1, 4.7, 3.8, 3.1, 3.9, 4.4];
// // // const highest = Math.max(...movieReviews);
// // // const lowest = Math.min(...movieReviews);

// // // let total = 0;
// // // movieReviews.forEach((rating) => (total += rating));
// // // const average = total / movieReviews.length;

// // // const info = { highest, lowest, average, hello: 'world' };

// // // console.log(info);

// // // const getReviewDetails = (arr) => {
// // //     const highest = Math.max(...arr);
// // //     const lowest = Math.min(...arr);
// // //     const total = arr.reduce((accumulator, nextVal) => accumulator + nextVal);
// // //     const average = total / arr.length;

// // //     return {
// // //         highest,
// // //         lowest,
// // //         total,
// // //         average,
// // //     };
// // // };

// // // const reviewList = [4.5, 5.0, 3.2, 2.1, 4.7, 3.8, 3.1, 3.9, 4.4];

// // // const statistics = getReviewDetails(reviewList);
// // // console.log(statistics);

// // // const username = 'janedoe';
// // // const role = 'admin';

// // // const user1 = {
// // //     [role]: username,
// // // };

// // // console.log(user1);

// // // const addProperty = (obj, k, v) => {
// // //     return { ...obj, [k]: v };
// // // };

// // // console.log(addProperty({ firstName: 'John' }, 'age', 20));

// // // const math = {
// // //     add(a, b) {
// // //         return a + b;
// // //     },
// // //     sub(a, b) {
// // //         return a - b;
// // //     },
// // // };

// // // console.log(math.add(10, 4));

// // // function helloworld() {
// // //     console.log('Hello World');
// // //     console.log(this);
// // // }

// // // helloworld();

// // function func() {
// //     console.log(`Hello my name is ${this.firstName} ${this.lastName}`);
// // }

// // const user1 = {
// //     firstName: 'John',
// //     lastName: 'Doe',
// //     age: 20,
// //     func,
// // };

// // const user2 = {
// //     firstName: 'Jane',
// //     lastName: 'Doe',
// //     age: 20,
// //     func,
// // };

// // user1.func();
// // user2.func();

// // // // const user = {
// // // //     firstName: 'John',
// // // //     lastName: 'Doe',
// // // //     age: 20,
// // // //     greet() {
// // // //         console.log(this);
// // // //     },
// // // // };

// // // user.greet();

// // // let helloworld = 'hello world';
// // console.log(this);

// // const user = {
// //     firstName: 'John',
// //     lastName: 'Doe',
// //     role: 'admin',
// //     fullName() {
// //         return `${this.firstName} ${this.lastName} is an ${this.role}`;
// //     },
// //     logDetails() {
// //         console.log(`${this.fullName()} and is cool!`);
// //     },
// // };

// // user.logDetails();

// // const log = user.logDetails;
// // log();

const hellos = {
    messages: [
        'hello world',
        'hello universe',
        'hello darkness',
        'hello hello',
        'heylo',
    ],
    pickMsg() {
        const index = Math.floor(Math.random() * this.messages.length);
        return this.messages[index];
    },
    start() {
        setInterval(() => {
            // this -> hellos
            console.log(this.pickMsg());
        }, 1000);
    },
};

// hellos.start();

// const user = {
//     firstName: 'John',
//     lastName: 'Doe',
//     role: 'admin',
//     fullName: function () {
//         return `${this.firstName} ${this.lastName} is an ${this.role}`;
//     },
//     logDetails: () => {
//         console.log(`${this.fullName()} and is cool!`);
//     },
// };

// console.log(user.logDetails());