In this article, I’ll explain how to solve freeCodeCamp’s “Confirm the Ending” challenge. This involves checking whether a string ends with specific sequence of characters.
在本文中,我将解释如何解决freeCodeCamp的“ Confirm the Ending ”挑战。 这涉及检查字符串是否以特定的字符序列结尾。
There are the two approaches I’ll cover:
我将介绍两种方法:
- using the substr() method 使用substr()方法
- using endsWith() method 使用endsWith()方法
算法挑战说明 (The Algorithm Challenge Description)
Check if a string (first argument, str
) ends with the given target string (second argument, target
).
检查字符串(第一个参数, str
)是否以给定的目标字符串(第二个参数, target
)结尾。
Check if a string (first argument, str
) ends with the given target string (second argument, target
).
检查字符串(第一个参数, str
)是否以给定的目标字符串(第二个参数, target
)结尾。
Check if a string (first argument, str
) ends with the given target string (second argument, target
).This challenge can be solved with the .endsWith()
method, which was introduced in ES2015. But for the purpose of this challenge, we would like you to use one of the JavaScript substring methods instead.
检查字符串(第一个参数, str
)是否以给定的目标字符串(第二个参数, target
)结尾。 可以通过.endsWith()
中引入的.endsWith()
方法解决此挑战。 但是出于此挑战的目的,我们希望您改用一种JavaScript子字符串方法。
function confirmEnding(string, target) {
return string;
}
confirmEnding("Bastian", "n");
提供的测试用例 (Provided test cases)
confirmEnding("Bastian", "n") should return true.
confirmEnding("Connor", "n") should return false.
confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") should return false.
largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000].
confirmEnding("He has to give m