-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain5-6.js
More file actions
24 lines (17 loc) · 972 Bytes
/
Copy pathmain5-6.js
File metadata and controls
24 lines (17 loc) · 972 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* Coding in function cutIt, function accept 1 parameter:arr. arr is a string array.
The first mission: Traversing arr, find the shortest string length.
The second mission: Traversing arr again, intercept all strings to the shortest string length(Start from index0). you can use one of slice() substring() or substr() do it. return the result after finished the work.
for example:
cutIt(["ab","cde","fgh"]) should return ["ab","cd","fg"]
cutIt(["abc","defgh","ijklmn"]) should return ["abc","def","ijk"]
cutIt(["codewars","javascript","java"]) should return ["code","java","java"] */
function cutIt(arr) {
//coding here...
let minLength = Math.min(...arr.map((element) => element.length));
return arr.map((x) => x.slice(0, minLength));
}
console.log(cutIt(["bic", "defgh", "i77"]));
function cutIt2(arr) {
const minLength = Math.min(...arr.map((x) => x.length));
return arr.map((x) => x.slice(0, minLength));
}