forked from HowProgrammingWorks/Iteration
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
repo with tasks HowProgrammingWorks#4
- Loading branch information
1 parent
a6d200c
commit 1163f87
Showing
7 changed files
with
47 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
'use strict'; | ||
"use strict"; | ||
|
||
const sum = (...args) => { | ||
// Use for loop and accumulator variable | ||
// to calculate sum of all given arguments | ||
// For example sum(1, 2, 3) should return 6 | ||
let res = 0; | ||
for (let i = 0; i < args.length; i++) { | ||
res += args[i]; | ||
} | ||
return res; | ||
}; | ||
|
||
module.exports = { sum }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
'use strict'; | ||
"use strict"; | ||
|
||
const sum = (...args) => { | ||
// Use for..of loop and accumulator variable | ||
// to calculate sum of all given arguments | ||
// For example sum(1, 2, 3) should return 6 | ||
let res = 0; | ||
for (const num of args) { | ||
res += num; | ||
} | ||
return res; | ||
}; | ||
|
||
module.exports = { sum }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
'use strict'; | ||
"use strict"; | ||
|
||
const sum = (...args) => { | ||
// Use while loop and accumulator variable | ||
// to calculate sum of all given arguments | ||
// For example sum(1, 2, 3) should return 6 | ||
let res = 0; | ||
while (args.length > 0) { | ||
res += args.shift(); | ||
} | ||
return res; | ||
}; | ||
|
||
module.exports = { sum }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
'use strict'; | ||
"use strict"; | ||
|
||
const sum = (...args) => { | ||
// Use do..while loop and accumulator variable | ||
// to calculate sum of all given arguments | ||
// For example sum(1, 2, 3) should return 6 | ||
if (args.length === 0) return 0; | ||
let res = 0; | ||
do { | ||
res += args.shift(); | ||
} while (args.length > 0); | ||
return res; | ||
}; | ||
|
||
module.exports = { sum }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,5 @@ | ||
'use strict'; | ||
"use strict"; | ||
|
||
const sum = (...args) => 0; | ||
// Use Array.prototype.reduce method | ||
// to calculate sum of all given arguments | ||
// For example sum(1, 2, 3) should return 6 | ||
const sum = (...args) => args.reduce((acc, args) => acc + args, 0); | ||
|
||
module.exports = { sum }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
'use strict'; | ||
"use strict"; | ||
|
||
const max = matrix => { | ||
// Use nested for loop to find max value in 2d matrix | ||
// For example max([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) | ||
// should return 9 | ||
const max = (matrix) => { | ||
let arr2d = matrix[0][0]; | ||
for (let i = 0; i < matrix.length; i++) { | ||
const row = matrix[i]; | ||
for (let a = 0; a < row.length; a++) { | ||
const col = row[a]; | ||
if (arr2d < col) arr2d = col; | ||
} | ||
} | ||
return arr2d; | ||
}; | ||
|
||
module.exports = { max }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,12 @@ | ||
'use strict'; | ||
"use strict"; | ||
|
||
const ages = persons => { | ||
// Use for..in to calculate age for each person | ||
// For example ages({ | ||
// lenin: { born: 1870, died: 1924 }, | ||
// mao: { born: 1893, died: 1976 }, | ||
// gandhi: { born: 1869, died: 1948 }, | ||
// hirohito: { born: 1901, died: 1989 }, | ||
// }) | ||
// should return { | ||
// lenin: 54, | ||
// mao: 83, | ||
// gandhi: 79, | ||
// hirohito: 88, | ||
// } | ||
}; | ||
const ages = (persons) => { | ||
const staf = {}; | ||
|
||
for (const i in persons) { | ||
const info = persons[i]; | ||
staf[i] = info.died - info.born; | ||
} | ||
return staf; | ||
}; | ||
module.exports = { ages }; |