Skip to content

Instantly share code, notes, and snippets.

View okovalov's full-sized avatar
🏠
Working from home

Oleksandr Kovalov okovalov

🏠
Working from home
View GitHub Profile
/**
* 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)}`)
/**
* 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
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])
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
}
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
}
const reverseWords = string => {
const wordsArr = string.split(' ')
let result = []
wordsArr.forEach(el => {
result.push(el.split('').reverse().join(''))
})
return result.join(' ')
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'
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
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) {
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 = [