Code in Chaos

Handy javascript snippets for building


Handy javascript snippets for building

Creates an array of numbers from 1 to n
const range = (n) => Array.from({ length: n }, (_, i) => i + 1);
// Usage: range(5) => [1, 2, 3, 4, 5]
Shuffles the elements of an array randomly
const shuffleArray = (arr) => arr.sort(() => 0.5 - Math.random());
// Usage: shuffleArray([1, 2, 3, 4]) => [3, 1, 4, 2] (random order)
Reverses a given string
const reverseString = (str) => str.split('').reverse().join('');
// Usage: reverseString('hello') => 'olleh'
Generates a random integer within a specified range
const random = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
// Usage: random(1, 10) => a number between 1 and 10
Generates a random integer within a specified range
const random = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
// Usage: random(1, 10) => a number between 1 and 10
Mini Pictionary
Play Mini Pictionary
Mini Pictionary
Play Mini Boggle