Loading...
More JavaScript Posts
//JavaScript program to swap two variables//take input from the userslet 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}`);
new StringBuilder(hi).reverse().toString()
// There are n rings and each ring is either red, green, or blue. The rings are distributed across ten rods labeled from 0 to 9.// You are given a string rings of length 2n that describes the n rings that are placed onto the rods. Every two characters in rings forms a color-position pair that is used to describe each ring where:// The first character of the ith pair denotes the ith ring's color ('R', 'G', 'B').// The second character of the ith pair denotes the rod that the ith ring is placed on ('0' to '9').// For example, "R3G2B1" describes n == 3 rings: a red ring placed onto the rod labeled 3, a green ring placed onto the rod labeled 2, and a blue ring placed onto the rod labeled 1.// Return the number of rods that have all three colors of rings on them.let rings = "B0B6G0R6R0R6G9";var countPoints = function(rings) {let sum = 0;// Always 10 Rodsfor (let i = 0; i < 10; i++) {if (rings.includes(`B${i}`) && rings.includes(`G${i}`) && rings.includes(`R${i}`)) {sum+=1;}}return sum;};console.log(countPoints(rings));
const geometricProgression = (end, start = 1, step = 2) =>Array.from({length: Math.floor(Math.log(end / start) / Math.log(step)) + 1,}).map((_, i) => start * step ** i);geometricProgression(256); // [1, 2, 4, 8, 16, 32, 64, 128, 256]geometricProgression(256, 3); // [3, 6, 12, 24, 48, 96, 192]geometricProgression(256, 1, 4); // [1, 4, 16, 64, 256]
const unzipWith = (arr, fn) =>arr.reduce((acc, val) => (val.forEach((v, i) => acc[i].push(v)), acc),Array.from({length: Math.max(...arr.map(x => x.length))}).map(x => [])).map(val => fn(...val));unzipWith([[1, 10, 100],[2, 20, 200],],(...args) => args.reduce((acc, v) => acc + v, 0));// [3, 30, 300]
require("dotenv").config();const mongoose = require("mongoose");const db = process.env.MONGO_URI;const connectDB = async () => {try {await mongoose.connect(db, {useNewUrlParser: true,useUnifiedTopology: true,useFindAndModify: false,useCreateIndex: true});console.log("MongoDB Connected");} catch (err) {console.error(err.message);}};module.exports = connectDB;