Skip to main content

Global Variables

Oct 29, 2020LeifMessinger
Loading...

More JavaScript Posts

Mongoose Model

Mar 11, 2021CHS

0 likes • 1 view

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)

Reverse a string in Java

Nov 20, 2022Helper

0 likes • 1 view

new StringBuilder(hi).reverse().toString()

typewriter-effect in Vanilla JS and React

Jan 25, 2023CHS

0 likes • 7 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();
}}
/>

Destructuring Assignment

Nov 19, 2022CodeCatch

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}`);

bucket sort

Nov 19, 2022CodeCatch

0 likes • 0 views

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]

native data structures

Nov 19, 2022CodeCatch

0 likes • 0 views

const nums = [1, 2, 3];
const strs = Array.from('est');
nums.push(6);
nums.push(4, 9);
strs.unshift('t');
nums.length; // 6
nums[nums.length - 1]; // 9
strs[0]; // 't'
strs[2]; // 's'
nums.slice(1, 3); // [2, 3]
nums.map(n => n * 2); // [2, 4, 6, 12, 8, 18]
nums.filter(n => n % 2 === 0); // [2, 6, 4]
nums.reduce((a, n) => a + n, 0); // 25
strs.reverse(); // ['t', 's', 'e', 't']
strs.join(''); // 'test'
const nums = new Set([1, 2, 3]);
nums.add(4);
nums.add(1);
nums.add(5);
nums.add(4);
nums.size; // 5
nums.has(4); // true
nums.delete(4);
nums.has(4); // false
[...nums]; // [1, 2, 3, 5]
nums.clear();
nums.size; // 0
const items = new Map([
[1, { name: 'John' }],
[2, { name: 'Mary' }]
]);
items.set(4, { name: 'Alan' });
items.set(2, { name: 'Jeff' });
items.size; // 3
items.has(4); // true
items.get(2); // { name: 'Jeff' }
items.delete(2);
items.size; // 2
[...items.keys()]; // [1, 4]
[...items.values()]; // [{ name: 'John' }, { name: 'Alan' }]
items.clear();
items.size; // 0