Giter Club home page Giter Club logo

arguments_and_closure_assignment's Introduction

Exercises

  1. Create two functions: double and square. double should take a number and return the number times two.
const double = num => num * 2;

square should take a number and return the number squared.

const square = num => num * num

  • Create a third function doubleSquare that uses both of the functions to return a number that is first doubled and then squared.
const double = num => num * 2;
const square = num => num * num;
const doubleSquare = num => square(double(num));
console.log(doubleSquare(2));

  1. Create a function classyGreeting that takes as input the strings firstName and lastName, and returns a string with a greeting using the two.
const classyGreeting=(...args)=> `Hi my name is  ${args[0]}${args[1]}`
console.log(classyGreeting('Thomas', ' Perez'));

  • Create a second function yell that takes string as input and returns the string in all capitalized letters.
const yell = word => word.toUpperCase();
console.log(yell('hello'));

  • Create a third function yellGreeting that will take the firstName and lastName as inputs and yell a greeting using the two.
const classyGreeting=(...args)=> `Hi my name is  ${args[0]}${args[1]}`
const yell = word => word.toUpperCase();
const yellGreeting = (firstname,lastname)=>
yell(classyGreeting(firstname,lastname))
console.log(yellGreeting('Thomas', ' Perez'));
  1. The concat array method is used to merge two (or more) arrays. Write a removeDupes function that takes an array as an argument and returns a copy without any duplicate elements. Then, write a function concatAndRemoveDupes that combines two arrays and removes any duplicates.
function removeDupes(arr){
  let answer = [];
  for(let i = 0; i < arr.length; i++)
  answer.push(i)
  if(answer === answer){
    console.log(answer - 1);
  } else {

  }
return arr
}
console.log(removeDupes(['luigi','toad','mario','bowser','mario']));

Hint: Use the array method includes, an object, or a Set. Or the spread operator instead of concat.

  1. Given a list of grades, we can get the median grade by sorting the list and taking the middle element, or the average of the two middle elements. Create the functions sort and middleElement, and then use them to create the functions median.

let grades = [91, 85, 100, 92, 88];

console.log(median(grades)); // Should log 91

const median = (arr) => {
  arr = sort(arr);
  let midIdx = Math.floor(arr.length / 2)
  if(arr.length % 2 === 0 ){
    return (arr[midIdx -1] + arr[midIdx] / 2)
  } else {
    return arr[midIdx]
  }
  }
  const sort = (arr) => {
    return arr.sort((a,b) => a - b)
  }
let answer = median([91, 85, 100, 92, 88])
console.log(answer);

  1. Write a function called repeat that takes in a string and numberOfTimes. The function should log to the screen the string however many times as numberOfTimes. If the user does not enter a numberOfTimes it should default to 2.
const repeat = (string , numOfTimes) => {
  if ( numOfTimes > 0){
    return string.repeat(numOfTimes);
  } else if (numOfTimes === 0){
      return string.repeat(2);
    } else {
      return '';
    }
    return repeat
}
console.log(repeat('tommy', 5 ));
  1. Using the spread operator, write a function that can take any number of arguments and return the sum of all of them.
const operator = (...arg) => {
  let answer = 0;
  for (let i = 0; i < arg.length; i++){
  answer += arg[i]
  }
return answer
}
console.log(operator(1,2,3,4,5));
  1. Write a function called adder takes in one number and returns a function that will add that number with another number. Using adder create an add5 and an add9 function. Hint: Closures!

arguments_and_closure_assignment's People

Contributors

coreyladovsky avatar jevit01 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.