-
Notifications
You must be signed in to change notification settings - Fork 39
Functions done #2
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: master
Are you sure you want to change the base?
Changes from 18 commits
8128f3d
c37148a
2174c93
3e88224
164ed32
fa11753
11b3f5c
271e122
568cf3f
f305395
4466fb1
4bb2753
3be1cf7
40d0a54
540cfdc
d1cf2df
570a93a
f34c4d9
337f143
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 |
|---|---|---|
| @@ -1,35 +1,97 @@ | ||
| // 1. Add function code goes here | ||
| exports.add = function add() {}; | ||
| exports.add = function add(x, y) { | ||
| return x + y | ||
| }; | ||
|
|
||
| // 2. Multiply function code goes here | ||
| exports.multiply = function multiply() {}; | ||
| exports.multiply = function multiply(x, y) { | ||
| return x * y | ||
| }; | ||
|
|
||
| // 3. OddOrEven function code goes here | ||
| exports.oddOrEven = function oddOrEven() {}; | ||
| exports.oddOrEven = function oddOrEven(x) { | ||
| if(x % 2 == 0) { | ||
| return ('even') | ||
| } else { | ||
| return ('odd') | ||
| } | ||
| }; | ||
|
|
||
| // 4. Write a function that returns an array that includes number 1 to 100 | ||
| // Ex: [1,2,3,4, ..., 99, 100] | ||
| exports.arrayGenerator = function arrayGenerator() {}; | ||
| exports.arrayGenerator = function arrayGenerator() { | ||
| let numbers = []; | ||
| for(i=1; i<=100; i++) { | ||
| numbers.push(i); | ||
| } | ||
| return(numbers); | ||
| }; | ||
|
|
||
| // 5. Fix this function. We want to see 2 in the console instead of undefined | ||
| exports.hoisting = function hoisting() { | ||
| console.log(y); // undefined | ||
| let y = 2; | ||
| return (y); | ||
| }; | ||
|
|
||
| // 6. Write a function that accepts unlimited amount of numbers as input | ||
| // and return the smallest value | ||
| exports.minValue = function minValue() {}; | ||
| exports.minValue = function minValue(...numbers) { | ||
| return Math.min(...numbers); | ||
| }; | ||
|
|
||
| // 7. Write a function that accepts an array of numbers as input | ||
| // and return a new array with all numbers doubled | ||
| // Ex: [1,2,3] => [2,4,6] | ||
| exports.doubleArray = function doubleArray() {}; | ||
| exports.doubleArray = function doubleArray([...x]) { | ||
| let doubledArray = []; | ||
| for(i = 0; i < x.length; i++) { | ||
| doubledArray.push(x[i] * 2); | ||
| }; | ||
|
Contributor
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. Let's use higher order function instead, check out the map method of array in javascript
Author
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. Thanks for the feedback, I updated the function! |
||
| return doubledArray; | ||
| }; | ||
|
|
||
| // 8. We have an array of students object, each object will have a name property | ||
| // write a function that accepts a student array as first parameter, and a name as second parameter | ||
| // and return the student with that name | ||
| // Example of student array: const students = [{ name: 'a' }, { name: 'b' }]; | ||
| exports.findStudentName = function findStudentName() {}; | ||
| exports.findStudentName = function findStudentName (studentList, name) { | ||
| return studentList.filter(student => student.name === name); | ||
| }; | ||
|
|
||
| // 9. Transform all of the above into arrow functions below here | ||
|
|
||
| // 1. ADD | ||
| const add = (x, y) => x + y; | ||
|
|
||
| // 2. MULTIPLY | ||
| const multiply = (x, y) => x * y; | ||
|
Comment on lines
+59
to
+63
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. may no need for spaces |
||
|
|
||
| // 3. ODDOREVEN | ||
| const oddOrEven = (x) => (x % 2 == 0) ? 'even' : 'odd'; | ||
|
|
||
| // 4. ARRAYGENERATOR | ||
| const arrayGenerator = () => { | ||
| let numbers = []; | ||
| for(i=1; i<=100; i++) { | ||
| numbers.push(i); | ||
| } | ||
| return(numbers); | ||
| }; | ||
|
|
||
| // 5. HOISTING | ||
| const hoisting = () => y = 2; | ||
|
|
||
| // 6. MINVALUE | ||
| const minValue = (...numbers) => Math.min(...numbers); | ||
|
|
||
| // 7. DOUBLEARRAY | ||
| const doubleArray = ([...x]) => { | ||
| let doubledArray = []; | ||
| for(i = 0; i < x.length; i++) { | ||
| doubledArray.push(x[i] * 2); | ||
| }; | ||
| return doubledArray; | ||
| }; | ||
|
|
||
| // 8. FINDSTUDENTNAME | ||
| const findStudentName = (studentList, name) => studentList.filter(student => student.name === name); | ||
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.
nice work.