Skip to content

Instantly share code, notes, and snippets.

@amandarfernandes
Created January 8, 2018 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amandarfernandes/328b9bc62cf58c16a793b61babac35dc to your computer and use it in GitHub Desktop.
Save amandarfernandes/328b9bc62cf58c16a793b61babac35dc to your computer and use it in GitHub Desktop.
JSRestSpreadExercises
/*
Write a function called smallestValue which accepts a variable number of parameters and returns the smallest parameters passed to the function.
Examples:
smallestValue(4,1,12,0) // 0
smallestValue(5,4,1,121) // 1
smallestValue(4,2) // 2
smallestValue(99,12321,12.2) // 2
*/
function smallestValue(){
return Math.min(...arguments);
}
/*
Write a function called placeInMiddle which accepts two parameters, an array and another array. This function should return the first array with all of the values in the second array placed in the middle of the first array.
Examples:
placeInMiddle([1,2,6,7],[3,4,5]) // [1,2,3,4,5,6,7]
placeInMiddle([1],[3,4,5]) // [3,4,5,1]
placeInMiddle([1,6],[2,3,4,5]) // [1,2,3,4,5,6]
placeInMiddle([],[2,3,4,5]) // [2,3,4,5]
*/
function placeInMiddle(arr, vals){
let mid = arr.length/2;
return [...arr.slice(0,mid),...vals,...arr.slice(mid)];
}
/*
Write a function called joinArrays which accepts a variable number of parameters (you can assume that each argument to this function will be an array) and returns an array of all of the parameters concatenated together
Examples:
joinArrays([1],[2],[3]) // [1,2,3]
joinArrays([1],[2],[3],[1],[2],[3]) // [1,2,3,1,2,3]
joinArrays([1,2,3],[4,5,6],[7,8,9]) // [1,2,3,4,5,6,7,8,9]
joinArrays([1],[3],[0],[7]) // [1,3,0,7]
*/
function joinArrays(...args){
let newarr=[];
for(let arr of args) {
newarr.push(...arr);
}
return newarr;
}
/*
// Write a function called sumEvenArgs which takes all of the parameters passed to a function and returns the sum of the even ones.
Examples:
sumEvenArgs(1,2,3,4) // 6
sumEvenArgs(1,2,6) // 8
sumEvenArgs(1,2) // 2
*/
function sumEvenArgs(...args){
return args.filter(n=>n%2===0).reduce((acc,num)=> acc+num);
}
/*
Write a function called flip which accepts a function and a value for the keyword this. Flip should return a new function that when invoked, will invoke the function passed to flip with the correct value of the keyword this and all of the parameters passed to the function REVERSED. HINT - if you pass more than two parameters to flip, those parameters should be included as parameters to the inner function when it is invoked. You will have to make use of closure!
Examples:
function personSubtract(a,b,c){
return this.firstName + " subtracts " + (a-b-c);
}
var person = {
firstName: 'Elie'
}
var flipFn = flip(personSubtract, person);
flipFn(3,2,1) // "Elie subtracts -4"
var flipFn2 = flip(personSubtract, person, 5,6);
flipFn(7,8). // "Elie subtracts -4"
flip(subtractFourNumbers,this,1)(2,3,4) // -2
flip(subtractFourNumbers,this,1,2)(3,4) // -2
flip(subtractFourNumbers,this,1,2,3)(4) // -2
flip(subtractFourNumbers,this,1,2,3,4)() // -2
flip(subtractFourNumbers,this)(1,2,3,4) // -2
flip(subtractFourNumbers,this,1,2,3)(4,5,6,7) // -2
flip(subtractFourNumbers,this)(1,2,3,4,5,6,7,8,9,10) // -2
flip(subtractFourNumbers,this,11,12,13,14,15)(1,2,3,4,5,6,7,8,9,10) // -22
*/
function flip(fn,thisRef,...args1) {
return function(...args2) {
let newArgs = [...args1,...args2].slice(0,3);
return fn.apply(thisRef,newArgs.reverse());
}
}
/*
Write a function called bind which accepts a function and a value for the keyword this. Bind should return a new function that when invoked, will invoke the function passed to bind with the correct value of the keyword this. HINT - if you pass more than two parameters to bind, those parameters should be included as parameters to the inner function when it is invoked. You will have to make use of closure!
Examples:
function firstNameFavoriteColor(favoriteColor){
return this.firstName + "'s favorite color is " + favoriteColor
}
var person = {
firstName: 'Elie'
}
var bindFn = bind(firstNameFavoriteColor, person);
bindFn('green') // "Elie's favorite color is green"
var bindFn2 = bind(firstNameFavoriteColor, person, 'blue');
bindFn2('green') // "Elie's favorite color is blue"
function addFourNumbers(a,b,c,d){
return a+b+c+d;
}
bind(addFourNumbers,this,1)(2,3,4) // 10
bind(addFourNumbers,this,1,2)(3,4) // 10
bind(addFourNumbers,this,1,2,3)(4) // 10
bind(addFourNumbers,this,1,2,3,4)() // 10
bind(addFourNumbers,this)(1,2,3,4) // 10
bind(addFourNumbers,this)(1,2,3,4,5,6,7,8,9,10) // 10
*/
function bind(fn, thisArg,...args1){
return function(...args2) {
return fn.apply(thisArg,[...args1,...args2]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment