DESCRIPTION

Write a function dirReduc which will take an array of strings and returns an array of strings with the needless directions removed (W<->E or S<->N side by side).

The Haskell version takes a list of directions with data Direction = North | East | West | South. The Clojure version returns nil when the path is reduced to nothing. The Rust version takes a slice of enum Direction {North, East, West, South}.

详细描述可以查看文档底部的引用链接。

SOLUTION

  • 方法一:模拟栈的方式。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function dirReduc(arr) {
const opposites = {
NORTH: "SOUTH",
SOUTH: "NORTH",
EAST: "WEST",
WEST: "EAST",
};
// 创建一个数组用来模拟一个栈,用于跟踪方向
const stack = [];
// 遍历输入的方向数组
for (const direction of arr) {
// 获取当前方向的反方向
const opposite = opposites[direction];
// 如果栈顶方向等于当前方向的反方向,则弹出栈顶方向
if (stack[stack.length - 1] === opposite) {
stack.pop();
} else {
// 否则,将当前方向压入栈顶
stack.push(direction);
}
}
// 最终,栈中剩下的方向就是去掉不必要的相反方向后的结果
return stack;
}
阅读全文 »


DESCRIPTION

Write a function that accepts an array of 10 integers (between 0 and 9), that returns a string of those numbers in the form of a phone number.

Example

createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) // => returns "(123) 456-7890"

The returned format must be correct in order to complete this challenge.

Don’t forget the space after the closing parentheses!

SOLUTION

  • 方法一:数组的 slice() 方法
1
2
3
4
5
6
7
8
9
10
11
function createPhoneNumber(numbers) {
// Create an array of the different parts of the phone number
const areaCode = `(${numbers.slice(0, 3).join("")})`;
const prefix = numbers.slice(3, 6).join("");
const suffix = numbers.slice(6, 10).join("");

// Combine the parts into the final phone number string
const phoneNumber = `${areaCode} ${prefix}-${suffix}`;

return phoneNumber;
}
阅读全文 »


DESCRIPTION

Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result.

It should remove all values from list a, which are present in list b keeping their order.

arrayDiff([1,2],[1]) == [2]

If a value is present in b, all of its occurrences must be removed from the other:

arrayDiff([1,2,2,2,3],[2]) == [1,3]

SOLUTION

  • 方法一:
1
2
3
function arrDiff(a, b) {
return a.filter((item) => !b.includes(item));
}
阅读全文 »


DESCRIPTION

Given an integral number, determine if it’s a square number:

In mathematics, a square number or perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself.

The tests will always use some integral number, so don’t worry about that in dynamic typed languages.

Examples

1
2
3
4
5
6
-1  =>  false
0 => true
3 => false
4 => true
25 => true
26 => false

SOLUTION

  • 方法一:
1
2
3
4
5
6
var isSquare = function (n) {
// 求平方根并取整
let roundSqrt = Math.round(Math.sqrt(n));
// 判断平方根相乘的结果是否与传入的数值相等
return n === roundSqrt * roundSqrt;
};
阅读全文 »


DESCRIPTION

In this kata you will create a function that takes a list of non-negative integers and strings and returns a new list with the strings filtered out.

Example

1
2
3
filter_list([1, 2, "a", "b"]) == [1, 2];
filter_list([1, "a", "b", 0, 15]) == [1, 0, 15];
filter_list([1, 2, "aasf", "1", "123", 123]) == [1, 2, 123];

SOLUTION

  • 数组的 filter() 方法
1
2
3
4
function filter_list(l) {
// Return a new array with the strings filtered out
return l.filter((word) => typeof word === "number");
}
阅读全文 »
0%