Skip to content

Instantly share code, notes, and snippets.

View mihailgaberov's full-sized avatar
🎱

Mihail Gaberov mihailgaberov

🎱
View GitHub Profile
// This problem was asked by Google.
// Given a string of words delimited by spaces, reverse the words in string. For example, given "hello world here", return "here world hello"
// Follow-up: given a mutable string representation, can you perform this operation in-place?
const reverseWords = (str) => str.split(' ').reverse().join(' ')
console.log(reverseWords("hello world here") === "here world hello") // true
console.log(reverseWords("hello world here") === "hello world here") // false
console.log(reverseWords("who the hell are you") === "you are hell the who") // false
function createGreeter(greeting) {
return function (name) {
console.log(greeting + ", " + name);
};
}
const sayHello = createGreeter("Hello");
sayHello("Joe"); // Hello, Joe
sayHello("Mihail"); // Hello, Mihail
sayHello("Chris"); // Hello, Chris
@mihailgaberov
mihailgaberov / grouping.js
Last active September 14, 2022 09:57
Grouping price levels by ticket size
/* const levels = [
[1001, 1],
[1000.5, 1],
[1000, 1],
[999, 1],
[988.5, 1],
[988, 1]
] */
const levels = [
@mihailgaberov
mihailgaberov / rounded.js
Created September 5, 2021 07:43
Returns the number rounded to the nearest interval - use `Math.round` if you need upper rounding.
const levels = [
[1001, 1],[1000.5, 1],[1000, 1]
]
/**
* Returns the number rounded to the nearest interval.
* Example:
*
* roundToNearest(80, 100); // 100
* roundToNearest(25, 15); // 30