• 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}`);
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
• Jan 25, 2023 •C S
0 likes • 8 views
// Vanilla JS Solution: var app = document.getElementById('app'); var typewriter = new Typewriter(app, { loop: true }); typewriter .typeString("I'm John and I'm a super cool web developer") .pauseFor(3000) .deleteChars(13) // "web developer" = 13 characters .typeString("person to talk with!") // Will display "I'm John and I'm a super cool person to talk with!" .start(); // React Solution: import Typewriter from 'typewriter-effect'; <Typewriter options={{ loop: true }} onInit={typewriter => { typewriter .typeString("I'm John and I'm a super cool web developer") .pauseFor(3000) .deleteChars(13) // "web developer" = 13 characters .typeString("person to talk with!") // Will display "I'm John and I'm a super cool person to talk with!" .start(); }} />
• Oct 15, 2022 •CodeCatch
1 like • 272 views
const express = require('express') const app = express() const port = 3000 app.get('/', (req, res) => { res.send('Hello World!') }) app.listen(port, () => { console.log(`Example app listening on port ${port}`) })
0 likes • 2 views
const luhnCheck = num => { let arr = (num + '') .split('') .reverse() .map(x => parseInt(x)); let lastDigit = arr.splice(0, 1)[0]; let sum = arr.reduce( (acc, val, i) => (i % 2 !== 0 ? acc + val : acc + ((val *= 2) > 9 ? val - 9 : val)), 0 ); sum += lastDigit; return sum % 10 === 0; }; luhnCheck('4485275742308327'); // true luhnCheck(6011329933655299); // true luhnCheck(123456789); // false
0 likes • 1 view
let nums = [1,2,3,1] var containsDuplicate = function(nums) { let obj = {}; for(let i =0; i< nums.length; i++){ if(obj[nums[i]]){ obj[nums[i]] += 1; return true; }else{ obj[nums[i]] = 1; } } return false; }; console.log(containsDuplicate(nums));