DESCRIPTION

Write a function that when given a URL as a string, parses out just the domain name and returns it as a string.

SOLUTION

  • 方法一:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function domainName(url) {
let domain;
// find & remove protocol (http, ftp, etc.) and get domain
if (url.indexOf("://") > -1) {
domain = url.split("/")[2];
} else {
domain = url.split("/")[0];
}
// remove port number if any
domain = domain.split(":")[0];
// remove www if any
domain = domain.replace("www.", "");
// remove .com if any
domain = domain.replace(".com", "");
return domain.split(".")[0];
}
阅读全文 »


The Hashtag Generator

The marketing team is spending way too much time typing in hashtags.
Let’s help them with our own Hashtag Generator!

Here’s the deal:

It must start with a hashtag (#).
All words must have their first letter capitalized.
If the final result is longer than 140 chars it must return false.
If the input or the result is an empty string it must return false.

SOLUTION

  • 方法一:普通循环
1
2
3
4
5
6
7
8
9
10
11
12
13
14
function generateHashtag(str) {
// 判断是否是空字符串,或只包含多个空格的字符串
if (str.length === 0 || str.replace(/\s+/g, "").length === 0) {
return false;
}
// 将每个单词的首字母大写
let words = str.split(" ");
for (let i = 0; i < words.length; i++) {
words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
}
// 拼接 #
let result = "#" + words.join("");
return result.length > 140 ? false : result;
}
阅读全文 »


DESCRIPTION

Create a function that takes a Roman numeral as its argument and returns its value as a numeric decimal integer. You don’t need to validate the form of the Roman numeral.

Modern Roman numerals are written by expressing each decimal digit of the number to be encoded separately, starting with the leftmost digit and skipping any 0s. So 1990 is rendered “MCMXC” (1000 = M, 900 = CM, 90 = XC) and 2008 is rendered “MMVIII” (2000 = MM, 8 = VIII). The Roman numeral for 1666, “MDCLXVI”, uses each letter in descending order.

注意一点:题目没有描述,当较小的值出现在较大的值之前时,将从较大的值中减去该值。可参考文末的引用链接。

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
25
26
27
28
29
function solution(roman) {
// 定义一个罗马数字到整数的映射
const romanMap = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000,
};
// 初始化结果变量,以存储罗马数字的最终值
let result = 0;
// 遍历罗马数字
for (let i = 0; i < roman.length; i++) {
// 获取当前字母和下一个字母
let letter = roman[i];
let letterAfter = roman[i + 1];
// 如果当前字母小于后一个字母,则从结果中减去当前字母的值
if (romanMap[letter] < romanMap[letterAfter]) {
result -= romanMap[letter];
} else {
// 如果当前字母大于后一个字母,则从结果中加上当前字母的值
result += romanMap[letter];
}
}
// 返回罗马数字的最终值
return result;
}
阅读全文 »


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;
}
阅读全文 »
0%