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 me a new name", "name")should return true.
confirmEnding("Open sesame", "same") should return true.
confirmEnding("Open sesame", "pen") should return false.
confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") should return false.
Do not use the built-in method .endsWith() to solve the challenge.
方法1:使用内置函数确认字符串的结尾—使用substr() (Approach 1: Confirm the Ending of a String With Built-In Functions — with substr())
For this solution, you’ll use the String.prototype.substr() method:
对于此解决方案,您将使用String.prototype.substr()方法:
The
substr()
method returns the characters in a string beginning at the specified location through the specified number of characters.substr()
方法以指定的字符数返回从指定位置开始的字符串中的字符。
Why are you using string.substr(-target.length)
?
为什么要使用string.substr(-target.length)
?
If the target.length is negative, the substr() method will start the counting from the end of the string, which is what you want in this code challenge.
如果target.length为负,则substr()方法将从字符串的末尾开始计数,这是您在此代码挑战中想要的。
You don’t want to use string.substr(-1)
to get the last element of the string, because if the target is longer than one letter:
您不想使用string.substr(-1)
来获取字符串的最后一个元素,因为如果目标的长度大于一个字母:
confirmEnding("Open sesame", "same")
…the target won’t return at all.
……目标根本不会回来。
So here string.substr(-target.length)
will get the last index of the string ‘Bastian’ which is ‘n’.
所以在这里string.substr(-target.length)
将获得字符串'Bastian'的最后一个索引'n'。
Then you check whether string.substr(-target.length)
equals the target (true or false).
然后,检查string.substr(-target.length)
是否等于目标(正确或错误)。
function confirmEnding(string, target) {
// Step 1. Use the substr method
if (string.substr(-target.length) === target) {
// What does "if (string.substr(-target.length) === target)" represents?
// The string is 'Bastian' and the target is 'n'
// target.length = 1 so -target.length = -1
// if ('Bastian'.substr(-1) === 'n')
// if ('n' === 'n')
// Step 2. Return a boolean (true or false)
return true;
} else {
return false;
}
}
confirmEnding('Bastian', 'n');
Without comments:
没有评论:
function confirmEnding(string, target) {
if (string.substr(-target.length) === target) {
return true;
} else {
return false;
}
}
confirmEnding('Bastian', 'n');
You can use a ternary operator as a shortcut for the if statement:
您可以将三元运算符用作if语句的快捷方式:
(string.substr(-target.length) === target) ? true : false;
This can be read as:
可以理解为:
if (string.substr(-target.length) === target) {
return true;
} else {
return false;
}
You then return the ternary operator in your function:
然后,您可以在函数中返回三元运算符:
function confirmEnding(string, target) {
return (string.substr(-target.length) === target) ? true : false;
}
confirmEnding('Bastian', 'n');
You can also refactor your code to make it more succinct by just returning the condition:
您还可以通过返回条件来重构代码,使其更简洁:
function confirmEnding(string, target) {
return string.substr(-target.length) === target;
}
confirmEnding('Bastian', 'n');
方法2:使用内置函数确认字符串的结尾—使用endsWith() (Approach 2: Confirm the Ending of a String With Built-In Functions — with endsWith())
For this solution, you’ll use the String.prototype.endsWith() method:
对于此解决方案,您将使用String.prototype.endsWith()方法:
The
endsWith()
method determines whether a string ends with the characters of another string, returningtrue
orfalse
as appropriate. This method is case-sensitive.endsWith()
方法确定一个字符串是否以另一个字符串的字符结尾,并根据情况返回true
或false
。 此方法区分大小写。
function confirmEnding(string, target) {
// We return the method with the target as a parameter
// The result will be a boolean (true/false)
return string.endsWith(target); // 'Bastian'.endsWith('n')
}
confirmEnding('Bastian', 'n');
I hope you found this helpful. This is part of my “How to Solve FCC Algorithms” series of articles on the freeCodeCamp Algorithm Challenges, where I propose several solutions and explain step-by-step what happens under the hood.
希望对您有所帮助。 这是我关于freeCodeCamp算法挑战的“如何解决FCC算法”系列文章的一部分,在该系列文章中我提出了几种解决方案并逐步解释了实际情况。
在JavaScript中重复字符串的三种方法 在本文中,我将解释如何解决freeCodeCamp的“重复字符串重复字符串”挑战。 这涉及…
在JavaScript中反转字符串的三种方法 本文基于Free Code Camp基本算法脚本“反转字符串”
在JavaScript中分解数字的三种方法 本文基于Free Code Camp基本算法脚本“简化数字”
用JavaScript检查回文的两种方法 本文基于Free Code Camp基本算法脚本“检查回文”。
在JavaScript中查找字符串中最长单词的三种方法 本文基于Free Code Camp基本算法脚本“查找字符串中最长单词”。
用JavaScript给句子加标题的三种方法 本文基于Free Code Camp基本算法脚本“标题加句子”。
If you have your own solution or any suggestions, share them below in the comments.
如果您有自己的解决方案或任何建议,请在下面的评论中分享。
Or you can follow me on Medium, Twitter, Github and LinkedIn, right after you click the green heart below ;-)
或者,您也可以在单击下面的绿色心脏之后立即在Medium , Twitter , Github和LinkedIn上关注我;-)
#StayCurious, #KeepOnHacking & #MakeItHappen!
#StayCurious,#KeepOnHacking和#MakeItHappen!