-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestString.js
More file actions
22 lines (16 loc) · 790 Bytes
/
Copy pathlongestString.js
File metadata and controls
22 lines (16 loc) · 790 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Write a function that takes an array of strings and return the longest string in the array.
// For example:
// const strings1 = ['short', 'really, really long!', 'medium'];
// console.log(longestString(strings1)); // <--- 'really, really long!'
// Edge case: If you had an array which had two "longest" strings of equal length, your function should just return the first one.
// For example:
// const strings2 = ['short', 'first long string!!', 'medium', 'abcdefghijklmnopqr'];
// console.log(longestString(strings2)); // <--- 'first long string!'
function longestString(arr) {
let longestString = arr.reduce(
(a, b) => (a.length > b.length || a.length == b.length ? a : b),
""
);
return longestString;
}
const strings1 = ["short", "really, really long!", "medium"];