Giter Site home page Giter Site logo

array_methods_assignment's Introduction

Exercises

  1. Given an array of numbers, write a function that returns a new array where every element is incremented by 10.
Input: [1,2,3,4,5]
Output: [11,12,13,14,15]
  1. Given an array, return a new array where every non-string element is converted to an empty string.
Input: ["a", 123, "b", "c", {name: "cat"}]
Output: ["a", "", "b", "c", ""]
  1. Write a function that returns only the even elements in an array of numbers.
Input: [1,2,3,4,5]
Output: [2,4]
  1. Write a function that checks if all elements in the array are the same.
Input: [1,1,1,1,1]
Output: true

Input: [1,1,1,1,2]
Output: false
  1. Write a function countZeroes, which takes an array of numbers as its argument and returns the amount of zeroes that occur in it.
Input: [1,0,0,2,0,3,0,0,0]
Output: 6
  1. Write a function that doubles every element in an array.
Input: [1,2,3,4]
Output: [2,4,6,8]
  1. Write a function that whether every tripled number in an array is less than 20.
Input: [1,2,4,7]
Output: false (7 * 3 = 21)

Input: [3,4,5]
Output: true
  1. Write a function elementDivisibleBy with parameters divisor and arr. Return a new array of every element of arr that can be evenly divided by divisor.
Input: 5, [4,5,6,7,8,9,10,11,12,13,14,15]
Output: [5,10,15]
  1. Write a numberTimesIdx that uses map and multiples each number in the array by its index.
Input: [6,7,8,9]
Output: [0,7,16,27]
  1. Write a function that returns whether or not every value in an array is a positive even number that doesn't end in 0.
Input: [2,4,6,8,12]
Output: true

Input: [2,4,6,8,10]
Output: false
  1. Write a function that returns a new array containing all of the strings in the original array uppercased.
Input: [{}, 1, "cat", 3, ["hi"], {name: "dog"}, "dog", "bear"]
Output: ["CAT", "DOG", "BEAR"]

Bonus:

You can write your own array methods!

Array.prototype.sayHi = function() {
  console.log("Hi")
}

Now every array that you write has a sayHi method that you can call.

let myArr = [1,2,3]
myArr.sayHi() //Logs "Hi" to the console
  1. Using Array.prototype add a new method myForEach that mimics the behavior of the built in forEach.

  2. Use your myForEach to add myMap to the Array prototype. myMap should behave the same as regular map.

  3. Write a myFilter that behaves the same as filter.

  4. Write a myEvery that behaves the same as every.

  5. Write a myReduce. It should use the first element in the array if none is given.

  6. Write a myJoin function.

  7. Write a mySlice function.

  8. Write a myTranspose. This function should transpose a matrix.

let mtx = [
            [1, 2],
            [3, 4],
            [5, 6]
          ]

mtx.myTranspose();

// => [
        [1, 3, 5],
        [2, 4, 6]
       ]
  1. Write a myFlatten. This should take a multi-dimensional array and return it as one array.
let arr = [1, 2, [3, 4, 5, [6, 7, 8]]]
  arr.myFlatten();
  // => [1, 2, 3, 4, 5, 6, 7, 8]
  1. Write a function that returns the sum of every element in an array.
Input: [1,2,3,4,5]
Output: 15
  1. Write a function that finds largest number in an array.
Input: [1,2,6,3,4,5]
Output: [6]
  1. Write a function that returns the sum of all odd numbers in an Array

Hint: What is the second argument in reduce?

Input: [2,3,4,5]
Output: 8

Input: [1,2,3,4,5]
Output: 9

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.