File tree Expand file tree Collapse file tree 4 files changed +24
-38
lines changed Expand file tree Collapse file tree 4 files changed +24
-38
lines changed Original file line number Diff line number Diff line change @@ -8,22 +8,11 @@ const subtract = function(num1, num2) {
8
8
} ;
9
9
10
10
const sum = function ( numArr ) {
11
- let sumArr = 0 ;
12
- for ( let i = 0 ; i < numArr . length ; i ++ ) {
13
- sumArr += numArr [ i ] ;
14
- }
15
- return sumArr ;
11
+ return numArr . reduce ( ( total , num ) => total + num , 0 ) ;
16
12
} ;
17
13
18
14
const multiply = function ( numArr ) {
19
- let product = 1 ;
20
- if ( numArr . length === 0 ) {
21
- return 0 ;
22
- }
23
- for ( let i = 0 ; i < numArr . length ; i ++ ) {
24
- product *= numArr [ i ] ;
25
- }
26
- return product ;
15
+ return numArr . reduce ( ( total , num ) => total * num , 1 ) ;
27
16
} ;
28
17
29
18
const power = function ( num1 , num2 ) {
Original file line number Diff line number Diff line change 1
1
const palindromes = function ( string ) {
2
- string = string . replace ( / [ . , ! ] / g, "" ) ;
3
- string = string . toLowerCase ( ) ;
4
- // console.log(string);
5
- // string = string.split('');
6
- for ( let i = 0 ; i < Math . floor ( string . length / 2 ) ; i ++ ) {
7
- // conso
8000
le.log(`${string[i]}\t${string[string.length-i-1]}`)
8
- if ( string [ i ] != string [ string . length - 1 - i ] ) {
9
- return false ;
10
- }
11
- }
12
- return true ;
2
+ processedString = string
3
+ . toLowerCase ( )
4
+ . replace ( / [ ^ a - z ] / g, "" ) ;
5
+
6
+ return (
7
+ processedString
8
+ . split ( "" )
9
+ . reverse ( )
10
+ . join ( "" ) === processedString
11
+ ) ;
13
12
} ;
14
13
15
14
// Do not edit below this line
Original file line number Diff line number Diff line change 1
1
const getTheTitles = function ( books ) {
2
- bookTitles = Array ( ) ;
3
- for ( let i = 0 ; i < books . length ; i ++ ) {
4
- bookTitles . push ( books [ i ] . title ) ;
5
- }
6
- return bookTitles ;
2
+ return books . map ( book => book . title ) ;
7
3
} ;
8
4
9
5
// Do not edit below this line
Original file line number Diff line number Diff line change 1
1
const findTheOldest = function ( people ) {
2
- const currentYear = parseInt ( new Date ( ) . getFullYear ( ) ) ;
3
- let age = Array ( ) ;
4
- for ( let i = 0 ; i < people . length ; i ++ ) {
5
-<
8000
/span> if ( people [ i ] . yearOfDeath === undefined ) {
6
- age . push ( currentYear - people [ i ] . yearOfBirth ) ;
7
- } else {
8
- age . push ( people [ i ] . yearOfDeath - people [ i ] . yearOfBirth ) ;
9
- }
2
+ return people . reduce ( ( oldest , person ) => {
3
+ return getAge ( oldest ) > getAge ( person ) ? oldest : person ;
4
+ } ) ;
5
+ } ;
6
+
7
+ const getAge = function ( person ) {
8
+ if ( person . yearOfDeath === undefined ) {
9
+ const currentYear = parseInt ( new Date ( ) . getFullYear ( ) ) ;
10
+ return currentYear - person . yearOfBirth ;
11
+ } else {
12
+ return person . yearOfDeath - person . yearOfBirth ;
10
13
}
11
- return people [ age . indexOf ( Math . max ( ...age ) ) ] ;
12
14
} ;
13
15
14
16
// Do not edit below this line
You can’t perform that action at this time.
0 commit comments