Skip to content

Commit c066026

Browse files
committed
add codewars problem solution
1 parent c711d0b commit c066026

2 files changed

Lines changed: 23 additions & 21 deletions

File tree

javascript/codewar/array/16.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Parts of a list (7 kyu)
3+
*
4+
* Divide a list (an array) of at least two elements into two non-empty parts.
5+
* Elements of a pair must be in the same order as in the original array.
6+
*
7+
* @param {string} arr
8+
* @return {string} 二維陣列
9+
*/
10+
function partlist(arr) {
11+
let result = [];
12+
for(let i = 1;i < arr.length;++i) {
13+
let inside = [];
14+
inside.push(arr.slice(0,i).join(" "));
15+
inside.push(arr.slice(i).join(" "));
16+
result.push(inside);
17+
}
18+
return result;
19+
}
20+
let arr = ["I", "wish", "I", "hadn't", "come"];
21+
// let expect = [["I", "wish I hadn't come"], ["I wish", "I hadn't come"], ["I wish I", "hadn't come"], ["I wish I hadn't", "come"]]
22+
// console.log(assert.deepEqual(partlist(arr),expect));
23+
console.log(partlist(arr));

javascript/index.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,24 +1047,3 @@ let numerator = 1, denominator = 2;
10471047
// "0.5"
10481048
// console.log(fractionToDecimal(numerator,denominator));
10491049

1050-
function partlist(arr) {
1051-
// divide a list (an array) of at least two elements into two non-empty parts.
1052-
// Elements of a pair must be in the same order as in the original array.
1053-
let result = [];
1054-
// 切成兩個元素
1055-
// front多一個字,end則少一個字
1056-
let front = arr[0];
1057-
let original = arr.join(" ");
1058-
// console.log(original)
1059-
let inside = [];
1060-
for(let i = 0;i < arr.length;++i) {
1061-
1062-
// inside[i] = original[i];
1063-
// result.push(inside)
1064-
}
1065-
console.log(result)
1066-
}
1067-
let arr = ["I", "wish", "I", "hadn't", "come"];
1068-
// let expect = [["I", "wish I hadn't come"], ["I wish", "I hadn't come"], ["I wish I", "hadn't come"], ["I wish I hadn't", "come"]]
1069-
// console.log(assert.deepEqual(partlist(arr),expect));
1070-
console.log(partlist(arr));

0 commit comments

Comments
 (0)