This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A random integer between given min and max | |
*/ | |
const getRandomNumber = (min = 1, max = 10) => Math.floor(Math.random() * (max - min + 1)) + min | |
const min = 10 | |
const max = 15 | |
for (let i = 1; i <= 10; i++) { | |
console.log(`Random number between ${min} and ${max} = ${getRandomNumber(min, max)}`) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* An idea how to address this 'issue' | |
* js> 0.1+0.2==0.3 | |
* false | |
* | |
* The problem here is that number does not always equal to what is displayed | |
* js> (0.1).toPrecision(21) | |
* 0.100000000000000005551 | |
* js> (0.2).toPrecision(21) | |
* 0.200000000000000011102 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const twoSum = (arr, num) => { | |
const result = [] | |
const hash = [] | |
for (let i in arr) { | |
const currentNumber = arr[i] | |
const expectedNumber = num - currentNumber | |
if (hash.indexOf(expectedNumber) !== -1) { | |
result.push([currentNumber, expectedNumber]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getMedian = arr => { | |
const arrLen = arr.length | |
if (arrLen % 2 !== 0) { | |
return arr[Math.floor(arrLen / 2)] | |
} else { | |
const m1 = arr[(arrLen / 2) - 1] | |
const m2 = arr[arrLen / 2] | |
return (m1 + m2) / 2 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const reverseArrayInPlace = arr => { | |
for(let i = 0; i < (arr.length / 2)+1; i++) { | |
const first = arr.shift() | |
arr.splice(arr.length - i, 0, first) | |
} | |
return arr | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const reverseWords = string => { | |
const wordsArr = string.split(' ') | |
let result = [] | |
wordsArr.forEach(el => { | |
result.push(el.split('').reverse().join('')) | |
}) | |
return result.join(' ') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fizzBuzz = num => { | |
for(let i = 1; i <= num; i++) { | |
if (i % 15 === 0) { | |
console.log('FizzBuzz') | |
continue | |
} | |
let val = i | |
if (i % 3 === 0) val = 'Fizz' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const harmlessRansomNote = (search, text) => { | |
let result = true | |
const textArr = text.toLowerCase().split(' ') | |
const searchArr = search.toLowerCase().split(' ') | |
const hash = {} | |
for (let idx in textArr) { | |
const word = textArr[idx] | |
if (!hash[word]) hash[word] = 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ceasarCipher = (str, num) => { | |
const alphabetArr = 'abcdefghijklmnopqrstuvwxyz'.split('') | |
let myNum = num % alphabetArr.length | |
const strArr = str.toLowerCase().split('') | |
let resArr = [] | |
for (let c in strArr) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isOpen = (parenthesisChar, pts) => pts.filter(pt => pt[0] === parenthesisChar).length | |
const matches = (topOfStack, closed, pts) => pts.filter(pt => pt[0] === topOfStack && pt[1] === closed).length | |
const getValidCharacters = patterns => patterns.map(p => p.join('')).join('') | |
const verify = string => { | |
const charactersArr = string.split('') | |
const patterns = [ |