Skip to content

Instantly share code, notes, and snippets.

@lior-amsalem
Created October 17, 2021 18:30
Show Gist options
  • Save lior-amsalem/6459a5abf254707c079dcc6f8b54b648 to your computer and use it in GitHub Desktop.
Save lior-amsalem/6459a5abf254707c079dcc6f8b54b648 to your computer and use it in GitHub Desktop.
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] === '*') {
res = parseInt(a[2], 10) * parseInt(a[0], 10)
}
if(a[1] === '/') {
res = parseInt(a[2], 10) / parseInt(a[0], 10)
}
if(a[1] === '-') {
res = parseInt(a[2], 10) - parseInt(a[0], 10)
}
if(a[1] === '+') {
res = parseInt(a[2], 10) + parseInt(a[0], 10)
}
return Math.floor(res);
}
return a
}
function zero(a = []) {
a.push('0');
return resolve(a);
}
function one(a = []) {
a.push('1');
return resolve(a);
}
function two(a = []) {
a.push('2');
return resolve(a);
}
function three(a = []) {
a.push('3');
return resolve(a);
}
function four(a = []) {
a.push('4');
return resolve(a);
}
function five(a = []) {
a.push('5');
return resolve(a);
}
function six(a = []) {
a.push('6');
return resolve(a);
}
function seven(a = []) {
a.push('7');
return resolve(a);
}
function eight(a = []) {
a.push('8');
return resolve(a);
}
function nine(a = []) {
a.push('9');
return resolve(a);
}
function plus(a = []) {
a.push('+');
return resolve(a);
}
function minus(a = []) {
a.push('-');
return resolve(a);
}
function times(a = []) {
a.push('*');
return a;
}
function dividedBy(a = []) {
a.push('/');
return resolve(a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment