Skip to content

Instantly share code, notes, and snippets.

@paulknulst
Last active August 30, 2022 04:42
  • Star 13 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
// 1. Shorten the console log
const log = console.log.bind(document)
log("does it work?")
log("yes")
log(5)
// 2. Merge two arrays into one
const array1 = ["One", "Two", "Three"]
const array2 = ["Four", "Five", "Six"]
const merged = array1.concat(array2)
console.log(merged) // "One", "Two", "Three", "Four", "Five", "Six"
// 3. Merge two objects into one
const user = {
name: 'Paul Knulst',
gender: 'Male'
};
const article = {
title: 'JavaScript tips',
date: '2021-11-19'
};
const summary = {...user, ...article}
console.log(summary)
// Output:
// date: "2021-11-19",
// gender: "Male",
// name: "Paul Knulst",
// title: "JavaScript tips"
// 4. Shorten an array
const big_array = ["One", "Two", "Three", "Four", "Five", "Six"]
big_array.length = 3
console.log(big_array) // ["One", "Two", "Three"]
// 5. Shuffle an array
const array = ["One", "Two", "Three", "Four", "Five", "Six"];
array.sort(function(){ return Math.random() - 0.5});
console.log(array)
// 6. Use isNum to verify a number
function isNum(n) { return !isNaN(parseFloat(n)) && isFinite(n); }
console.log(isNum(1337)) // true
console.log(isNum(13.37)) // true
console.log(isNum("JavaScript")) // false
// 7. Use isStr to verify a string
const isStr = value => typeof value === 'string';
console.log(isStr('JavaScript')); // true
console.log(isStr(345)); // false
console.log(isStr(true)); // false
// 8. Use isNull
const isNull = value => value === null || value === undefined;
console.log(isNull(null)) // true
console.log(isNull()) // true
console.log(isNull(123)) // false
console.log(isNull("J")) // false
// 9. Calculate the performance of a function
const start = performance.now();
//some program
const end = performance.now();
const total = start - end
console.log("function takes " + total + " milisecond");
// 10. Remove duplicates from an array
const delDuplicates = array => [...new Set(array)]
console.log(delDuplicates(["One", "Two", "Three", "Two", "Four",
"Five", "Four","Three", "One", "Five",
"Five", "Three", "Four", "One", "Two"]))
// Output:
// "One", "Two", "Three", "Four", "Five"
// 11. Use logical AND/OR for conditions
// executing functions
const input = 2
input === 5 && console.log("it is 5")
input === 5 || console.log("it is not 5")
// assigning values
function defaultsTo5(arg) {
arg = arg || 5; // arg will have 5 as a default value if not set
console.log(arg)
}
let arg1 = 2
let arg2 = null
defaultsTo5(arg1) // 2
defaultsTo5(arg2) // 5
// 12. Ternary operator
function temparature(temp) {
return (temp > 39 || temp < 35.5) ? 'Visit Doctor!'
: (temp < 37.5 && temp > 36.5) ? 'Go Out and Play!!'
: (temp <= 39 && temp >= 35.5) ? 'Take Some Rest!' : ''
}
console.log(temparature(38)) // Take Some Rest!
console.log(temparature(36)) // Take Some Rest!
console.log(temparature(39.1)) // Visit Doctor!
console.log(temparature(35.1)) // Visit Doctor!
console.log(temparature(37)) // Go Out and Play
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment