Skip to content

Instantly share code, notes, and snippets.

@mmeigooni
Last active July 15, 2017 17:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mmeigooni/9a8e84a2a5a79a9f948efb3fd5143aec to your computer and use it in GitHub Desktop.
Save mmeigooni/9a8e84a2a5a79a9f948efb3fd5143aec to your computer and use it in GitHub Desktop.

Slides

  1. Write a function called average that takes an array of numbers as a parameter and returns the average of those numbers.

    After you write your function, you can test it using the above inputs to make sure that it behaves correctly.

  2. Write a function called min that finds the smallest number in an array of numbers.

    After you write your function, you can test it using the above inputs to make sure that it behaves correctly.

  3. Write a function shortestWord that works like longestWord, but returns the shortest word instead.

    After you write your function, you can test it using the above inputs to make sure that it behaves correctly.

  4. Write a function countCharacters that, when given a string as an argument, returns an object containing counts of the ocurrences of each character in the string.

    function countCharacters(s) {
      // ...
    }
    countCharacters("hello"); // => {"h": 1, "e": 1, "l": 2, "o": 1}

    After you write your function, you can test it using the above inputs to make sure that it behaves correctly.

    HINT: You will want to make use of the string method split. Try \"hello".split("") at a console to see how it works.

  5. Write a function select that accepts two arguments: an object and an array. The array should contain names of keys that will be selected from the object:

    function select(obj, keys) {
      // ...
    }
    select({a: 1, b: 2, c: 3}, ["a"]); // => {a: 1}
    select({a: 1, b: 2, c: 3}, ["a", "c"]); // => {a: 1, c: 3}
    select({a: 1, b: 2, c: 3}, ["a", "c", "d"]); // => {a: 1, c: 3}

    After you write your function, you can test it using the above inputs to make sure that it behaves correctly.

  6. Complete the below function called range that takes two integers as parameters, start and end, and returns an array containing all the whole numbers between them starting with start and up to end (you can use a for loop, while loop, each, or repetition with function invocation). The function definition should look like this:

    function range(start, end) {
      // YOUR CODE HERE
    }

    You should be able to use it like so:

    range(0, 4); // => [0, 1, 2, 3]
    range(2, 7); // => [2, 3, 4, 5, 6]
    range(10, 10); // => []
    range(10, 2); // => []

    After you write your function, you can test it using the above inputs to make sure that it behaves correctly.

  7. Write a function called squareNumericValues that takes an object as a parameter and returns an object with all of the numeric values in the object squared, e.g.

    function squareNumericValues(obj) {
      // TODO: Your code here
    }
    squareNumericValues({a: 4, b: 7, c: 2}); // => {a: 16, b: 49, c: 4}
    squareNumericValues({name: "Phuong", age: 25}); // => {name: "Phuong", age: 625}

    One observation to make when writing this function is that you'll need to only square the values that are actually numbers -- notice how in the second example invocation above (squareNumericValues({name: "Phuong", age: 25})) the value "Phuong" is unchanged because its value is a string.

    To handle this, you will need to use the typeof operator to determine each value's type. Enter the following into a console to get an idea of how typeof works:

    typeof 1; // => "number"
    typeof "hello"; // => "string"
    typeof true; // => "boolean"

    After you write your function, you can test it using the above inputs to make sure that it behaves correctly.

  8. Complete the below function called range that takes two integers as parameters, start and end, and returns an array containing all the whole numbers between them starting with start and up to end (you can use a for loop, while loop, each, or repetition with function invocation). The function definition should look like this:

    function range(start, end) {
      // YOUR CODE HERE
    }

    You should be able to use it like so:

    range(0, 4); // => [0, 1, 2, 3]
    range(2, 7); // => [2, 3, 4, 5, 6]
    range(10, 10); // => []
    range(10, 2); // => []

    After you write your function, you can test it using the above inputs to make sure that it behaves correctly.

  9. Given the following array of people, write a function that, when passed people as a parameter, returns the person (that is, your function should return an object) with the longest name (first, middle & last).

    var people = [
      {name: {first: "Alyssa", middle: "P.", last: "Hacker"}, age: 26},
      {name: {first: "Ben", last: "Bitdiddle"}, age: 34},
      {name: {first: "Eva", middle: "Lu", last: "Ator"}, age: 40},
      {name: {first: "Lem", middle: "E.", last: "Tweakit"}, age: 45},
      {name: {first: "Louis", last: "Reasoner"}, age: 21}
    ];
    
    function longestName(people) {
      // TODO: Your code here
    }
    longestName(people);
    // => {name: {first: "Alyssa", middle: "P.", last: "Hacker"}, age: 26}

After you write your function, you can test it using the above inputs to make sure that it behaves correctly.

HINT: It might be helpful to have a fullName function that, when given a person as a parameter, returns a person's full name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment