• Nov 19, 2022 •CodeCatch
0 likes • 0 views
//JavaScript program to swap two variables //take input from the users let a = prompt('Enter the first variable: '); let b = prompt('Enter the second variable: '); //using destructuring assignment [a, b] = [b, a]; console.log(`The value of a after swapping: ${a}`); console.log(`The value of b after swapping: ${b}`);
0 likes • 3 views
const flat = arr => [].concat.apply([], arr.map(a => Array.isArray(a) ? flat(a) : a)); // Or const flat = arr => arr.reduce((a, b) => Array.isArray(b) ? [...a, ...flat(b)] : [...a, b], []); // Or // See the browser compatibility at https://caniuse.com/#feat=array-flat const flat = arr => arr.flat(); // Example flat(['cat', ['lion', 'tiger']]); // ['cat', 'lion', 'tiger']
• Mar 11, 2021 •C S
0 likes • 2 views
import { configureStore } from "@reduxjs/toolkit"; import { combineReducers } from "redux"; import profile from "./profile"; import auth from "./auth"; import alert from "./alert"; const reducer = combineReducers({ profile, auth, alert, }); const store = configureStore({ reducer }); export default store;
const union = (...arr) => [...new Set(arr.flat())]; // Example union([1, 2], [2, 3], [3]); // [1, 2, 3]
const gcd = (...arr) => { const _gcd = (x, y) => (!y ? x : gcd(y, x % y)); return [...arr].reduce((a, b) => _gcd(a, b)); }; gcd(8, 36); // 4 gcd(...[12, 8, 32]); // 4
const permutations = arr => { if (arr.length <= 2) return arr.length === 2 ? [arr, [arr[1], arr[0]]] : arr; return arr.reduce( (acc, item, i) => acc.concat( permutations([...arr.slice(0, i), ...arr.slice(i + 1)]).map(val => [ item, ...val, ]) ), [] ); }; permutations([1, 33, 5]); // [ [1, 33, 5], [1, 5, 33], [33, 1, 5], [33, 5, 1], [5, 1, 33], [5, 33, 1] ]