Giter Club home page Giter Club logo

06-02-iteration_in_javascript-epic_mode's Introduction

06-02 Iteration in JavaScript Epic Mode

My solution:

const listOfNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

const listOfEvenNumbers = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

const listOfNumbersWithOneOdd = [2, 4, 6, 8, 10, 12, 14, 3, 16, 18, 20]

// Map ---------------------------------
const _map = (array) => {
  const newArray = []
  array.forEach((number) => {
    newArray.push(number * 2)
  })
  return newArray
}

console.log(_map(listOfNumbers))

// Filter ---------------------------------
const _filter = (array) => {
  const newArray = []
  array.forEach((number, index) => {
    if (number % 2 === 0) {
      newArray.push(number)
    }
  })
  return newArray
}

console.log(_filter(listOfNumbers))

// Reduce ---------------------------------
const _reduce = (array) => {
  let total = 0
  array.forEach((number) => {
    total = total + number
  })
  return total
}

console.log(_reduce(listOfNumbers))

// Every ---------------------------------
const _every = (array) => {
  let allNumbersAreEven = true
  array.forEach((number) => {
    if (number % 2 === 1) {
      allNumbersAreEven = false
    }
  })
  return allNumbersAreEven
}

console.log(_every(listOfEvenNumbers))
console.log(_every(listOfNumbersWithOneOdd))

// Some ---------------------------------
const _some = (array) => {
  let atleastOneOdd = false
  array.forEach((number) => {
    if (number % 2 === 1) {
      atleastOneOdd = true
    }
  })
  return atleastOneOdd
}

console.log(_some(listOfEvenNumbers))
console.log(_some(listOfNumbersWithOneOdd))

Assignment Overview:

Objectives

After completing this assignment, you should be able to:

  • Demonstrate understanding of iteration/enumeration

Instructions

  1. Fork this repository to your own account.
  2. Change into your projects directory:
  3. Clone your repository: hub clone js-iteration
  4. Change into your project's directory: cd js-iteration
  5. Install the dependencies: yarn install (or just yarn for short)
  6. Open in your editor]
  7. Start the test runner: yarn test
  8. Open src/functions.test.js and work on functions until tests pass.
  9. Commit and push your work to GitHub.
  10. Turn in the URL to your GitHub repo.

Epic Mode

  • Using the forEach looping method and no other looping or enumeration helper, write your own version of the following methods, except name yours _map, _filter, etc.
    • map
    • filter
    • reduce
    • every
    • some
  • Write some example usage of your new methods to demonstrate that they work

Additional Resources

Reference the documentation on DevDocs to find what kind of helpful functions might already be in JavaScript.

06-02-iteration_in_javascript-epic_mode's People

Contributors

breadkenty 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.