Loading...
More JavaScript Posts
const primes = num => {let arr = Array.from({ length: num - 1 }).map((x, i) => i + 2),sqroot = Math.floor(Math.sqrt(num)),numsTillSqroot = Array.from({ length: sqroot - 1 }).map((x, i) => i + 2);numsTillSqroot.forEach(x => (arr = arr.filter(y => y % x !== 0 || y === x)));return arr;};primes(10); // [2, 3, 5, 7]
const mongoose = require('mongoose')const UserSchema = new mongoose.Schema({username: {type: String,required: true},email: {type: String,unique: true,required: true},password: {type: String,required: true},date: {type: Date,default: Date.now}})module.exports = User = mongoose.model('user', UserSchema)
/*** @param {number[]} nums* @return {number[]}*/var productExceptSelf = function(nums) {var output = [];var leftMult = 1;var rightMult = 1;for (var i=nums.length - 1; i >= 0; i--) {output[i] = rightMult;rightMult *= nums[i];console.log({output: JSON.stringify(output), i})}for (var j=0; j < nums.length; j++) {output[j] *= leftMult;leftMult *= nums[j];console.log({output: JSON.stringify(output), j})}return output;};console.log(productExceptSelf([1, 2, 3, 4]))
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 express = require('express')const app = express()const port = 3000app.get('/', (req, res) => {res.send('Hello World!')})app.listen(port, () => {console.log(`Example app listening on port ${port}`)})
const compactObject = val => {const data = Array.isArray(val) ? val.filter(Boolean) : val;return Object.keys(data).reduce((acc, key) => {const value = data[key];if (Boolean(value))acc[key] = typeof value === 'object' ? compactObject(value) : value;return acc;},Array.isArray(val) ? [] : {});};const obj = {a: null,b: false,c: true,d: 0,e: 1,f: '',g: 'a',h: [null, false, '', true, 1, 'a'],i: { j: 0, k: false, l: 'a' }};compactObject(obj);// { c: true, e: 1, g: 'a', h: [ true, 1, 'a' ], i: { l: 'a' } }