Loading...
More JavaScript Posts
const jwt = require("jsonwebtoken");const authToken = (req, res, next) => {const token = req.headers["x-auth-token"];try {req.user = jwt.verify(token, process.env.ACCESS_TOKEN_SECRET);next();} catch (err) {console.log(err.message);res.status(401).json({ msg: "Error authenticating token" });}};module.exports = authToken;
const reverse = str => str.split('').reverse().join('');reverse('hello world');// Result: 'dlrow olleh'
import React, { useState } from 'react' import Welcome from '../components/Welcome' function About() { const [showWelcome, setShowWelcome] = useState(false) return ( <div> {showWelcome ? <Welcome /> : null} </div> <div> {showWelcome && <Welcome /> } </div> ) } export default App
const bucketSort = (arr, size = 5) => {const min = Math.min(...arr);const max = Math.max(...arr);const buckets = Array.from({ length: Math.floor((max - min) / size) + 1 },() => []);arr.forEach(val => {buckets[Math.floor((val - min) / size)].push(val);});return buckets.reduce((acc, b) => [...acc, ...b.sort((a, b) => a - b)], []);};bucketSort([6, 3, 4, 1]); // [1, 3, 4, 6]
const gcd = (...arr) => {const _gcd = (x, y) => (!y ? x : gcd(y, x % y));return [...arr].reduce((a, b) => _gcd(a, b));};gcd(8, 36); // 4gcd(...[12, 8, 32]); // 4
const linearSearch = (arr, item) => {for (const i in arr) {if (arr[i] === item) return +i;}return -1;};linearSearch([2, 9, 9], 9); // 1linearSearch([2, 9, 9], 7); // -1