-
Notifications
You must be signed in to change notification settings - Fork 0
06weekhigher order #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: gh-pages
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| 'use strict'; | ||
|
|
||
| // Create a new file called loops.js in the /04week folder of your workbook. | ||
| // Complete each of the following exercises. | ||
| // for loop | ||
|
|
||
| // Use a for loop to console.log each item in the array carsInReverse. | ||
| const carsInReverse = ['Ford', 'Chevy', 'Tesla', 'Nissan', 'Toyota']; | ||
| for (i = 0; i < carsInReverse.length; i++) { | ||
| console.log(carsInReverse[i]); | ||
| } | ||
|
|
||
| // Create an object (an array with keys and values) called persons with the following data: | ||
| // firstName: "Jane" | ||
| // lastName: "Doe" | ||
| // birthDate: "Jan 5, 1925" | ||
| // gender: "female" | ||
| const persons = { | ||
| firstName: "Jane", | ||
| lastName: "Doe", | ||
| birthDate: "Jan 5, 1925", | ||
| gender: "female" | ||
| }; | ||
|
|
||
| // Use a for...in loop to console.log each key. | ||
| for (const keys in persons) { | ||
| console.log(Object.keys(persons)); | ||
| } | ||
|
|
||
| // Then use a for...in loop and if state to console.log the value associated with the key birthDate. | ||
| for (const keys in persons) { | ||
| if (keys === 'birthDate') { | ||
| console.log(persons[keys]); | ||
| } | ||
| } | ||
|
|
||
| // Use a while loop to console log numbers 1 to 1000 | ||
| let num = 1; | ||
| while(num <= 1000) { | ||
| console.log(num); | ||
| num++; | ||
| } | ||
|
|
||
| // Use a do...while loop to console.log the numbers from 1 to 1000. | ||
| do { | ||
| console.log(num); | ||
| num++; | ||
| } | ||
| while(num <= 1000); | ||
|
|
||
| // When is a for loop better than a while loop? | ||
| // A forLoop is better when you know the amount of loops you need to do. When you know the cap. | ||
|
|
||
| // How is the readability of the code affected? | ||
| // A while loop can break easier because of the open ended autonomy. The for loop has a set limit which can be easier to control. | ||
|
|
||
| // What is the difference between a for loop and a for...in loop? | ||
| // for in loops are used mostly for objects | ||
|
|
||
| // What is the difference between a while loop and a do...while loop? | ||
| // A while loop goes until something is the argument is false and a do while goes while the condition is true. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,18 +2,55 @@ | |
|
|
||
| const assert = require('assert'); | ||
|
|
||
| function forEach(arr, callback) { | ||
| // Your code here | ||
| // Create a forEach() function that takes an array of items and a function that runs the function arr.length number of times. | ||
| // | ||
| // Create a map() function that takes an array of items and a function that returns an array with each item manipulated by that function. | ||
| // | ||
| // Create a filter() function that takes an array of items and a function that returns an array with only the items that return true in the function. | ||
|
|
||
| // a functon that takes an array and a function that runs the function arr.length | ||
|
|
||
| const numArray = [2, 4, 6, 8]; | ||
|
|
||
| function forEach(arr) { | ||
| for (i = 0; i <= arr.length; i ++){ | ||
| console.log(arr[i] + 2); | ||
| } | ||
| } | ||
|
|
||
| forEach(numArray); | ||
|
|
||
| // map consists of an array, a function, and a return array. | ||
|
|
||
| function map(arr, callback) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes! Perfect! |
||
| // Your code here | ||
| const newArr = []; | ||
| arr.forEach((item) => { | ||
| newArr.push(callback(item)); | ||
| }) | ||
| return newArr; | ||
| } | ||
|
|
||
| map(numArray, (item)=> item + 2); | ||
|
|
||
| const numTwoArray = [1,2,3,4,5,6,7,8] | ||
|
|
||
| const sortingFunction = (num) => { | ||
| if(num > 4) { | ||
| return num; | ||
| } | ||
| } | ||
|
|
||
| function filter(arr, callback) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From MDN:
Yours:
|
||
| // Your code here | ||
| const newFilterArr = []; | ||
| arr.forEach((num) => { | ||
| newFilterArr.push(callback(num)); | ||
| }) | ||
| return newFilterArr; | ||
| } | ||
|
|
||
| filter (numTwoArray, sortingFunction); | ||
|
|
||
|
|
||
| function some(arr, callback) { | ||
| // Your code here | ||
| } | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From MDN:
Yours: