Skip to content

Instantly share code, notes, and snippets.

@lior-amsalem
lior-amsalem / calculator.js
Created October 17, 2021 18:41
Create a simple calculator that given a string of operators (), +, -, *, / and numbers separated by spaces returns the value of that expression. see example below
/**
Example:
Calculator().evaluate("2 / 2 + 3 * 4 - 6") # => 7
**/
const Calculator = function() {
this.resolveMultiDivision = mathString => {
var resolvedTop = mathString.replace(new RegExp(/(\d*\.?\d*)([\/\*])(\d*\.?\d*)/), (match, group1,group2,group3) => {
return group2 === '*' ? group1 * group3 : group1 / group3;
@lior-amsalem
lior-amsalem / sudoku-resolve-function.js
Created October 17, 2021 18:40
Sudoku is a game played on a 9x9 grid. The goal of the game is to fill all cells of the grid with digits from 1 to 9, so that each column, each row, and each of the nine 3x3 sub-grids (also known as blocks) contain all of the digits from 1 to 9.
/**
example:
validSolution([
[5, 3, 4, 6, 7, 8, 9, 1, 2],
[6, 7, 2, 1, 9, 5, 3, 4, 8],
[1, 9, 8, 3, 4, 2, 5, 6, 7],
[8, 5, 9, 7, 6, 1, 4, 2, 3],
[4, 2, 6, 8, 5, 3, 7, 9, 1],
[7, 1, 3, 9, 2, 4, 8, 5, 6],
[9, 6, 1, 5, 3, 7, 2, 8, 4],
@lior-amsalem
lior-amsalem / connect-four-who-won-function.js
Created October 17, 2021 18:38
Connect Four is a game in which two players take turns dropping red or yellow colored discs into a vertically suspended 7 x 6 grid. Discs fall to the bottom of the grid, occupying the next available space. in this function we'll resolve if the game is done/won given array
/**
array of game for example:
[['-','-','-','-','-','-','-'],
['-','-','-','-','-','-','-'],
['-','-','-','R','R','R','R'],
['-','-','-','Y','Y','R','Y'],
['-','-','-','Y','R','Y','Y'],
['-','-','Y','Y','R','R','R']];
**/
@lior-amsalem
lior-amsalem / make-spiral.js
Created October 17, 2021 18:37
Function purpose is to create a NxN spiral with a given size
/**
output of size = 5 example:
00000
....0
000.0
0...0
00000
**/
@lior-amsalem
lior-amsalem / smaller-than-me.js
Created October 17, 2021 18:35
given array of numbers, return array of numbers that tells how many numbers to the right are smaller than current value
function smaller(nums) {
/** developer comment:
- we have to return the amount of numbers that are smaller than arr[i] to the right.
- example: smaller([5, 4, 3, 2, 1]) === [4, 3, 2, 1, 0] or smaller([1, 2, 0]) === [1, 1, 0]
- go over each elemt
- cut with slice base on current index
- filter which one is smaller then current value
- use lenght to count how smaller than me?
**/
let res = nums.map((value, index, array) => {
@lior-amsalem
lior-amsalem / remove-odd-index-elem-from-array.js
Created October 17, 2021 18:32
Simply remove every odd index element in array
function removeEveryOther(arr){
return arr.filter((word, index) => !(index%2));
}
@lior-amsalem
lior-amsalem / calculate-with-function-names.js
Created October 17, 2021 18:30
we want to write calculations using functions and get the results. example: seven(times(five())); // must return 35
/**
Developer comments
- we want to write calculations using functions and get the results. example: seven(times(five())); // must return 35
- use return to understand the structure those functinos are being called
- use array to pass and store all previous data of those functions
- build single function to resolve the array we build
**/
function resolve(a) {
if(a.length >= 3) {
if(a[1] === '*') {
/**
Developer comment
- This is a pseudo-encryption algorithm which given a string S and an integer N concatenates all the odd-indexed
characters of S with all the even-indexed characters of S, this process should be repeated N times.
- use recursive function
- decress the N
- every step encrypt the text
- make sure we have exist statement, than return the text
**/
function encrypt(text, n) {
/**
Developer comment
- This is a difference function, which subtracts one list from another and returns the result.
- we'll go over all 'b' values
- we'll use filter to knock down each of 'b' values from 'a' array
**/
function arrayDiff(a, b) {
let newArray = a;
@lior-amsalem
lior-amsalem / get-domain-name-from-url.js
Last active October 17, 2021 18:25
Get domain name from a URL
/**
Developer comment
- we'll use regex to build few capturing groups with the next structure:
first: https://wwww
second: <the domain>
thrid: .<end of domain>
- than we pick the relevant group from the array (base on the code task requriement)
**/
function domainName(url){
return url.match(/^(https?:\/\/)?([w]+\.)?([a-z0-9-]+)\.[a-z]+/)[3];