[英]Remove Last Character From String If It's a “!” Using For-Loop - JavaScript
I have 我有
function remove(s) {
for (i = 0; i < s.length; i++) {
let lastChar = s.slice(-1);
if (lastChar === "!") {
s = s.substring(0, s.length - 1);
}
else {
return s;
}
}
return s;
}
And this is passing 105 tests but failing 1 on codewars . 这通过了105次测试,但在代码战中没有通过1 次 。
The test that it's failing is: 失败的测试是:
Expected: '\\'isl\\'', instead got: '\\'isl!\\''
for when (s) is "isl!!!!!"
Expected: '\\'isl\\'', instead got: '\\'isl!\\''
当(s)为"isl!!!!!"
时Expected: '\\'isl\\'', instead got: '\\'isl!\\''
I can't figure out why, in this case, it's not removing the last character in the string. 我不知道为什么在这种情况下没有删除字符串中的最后一个字符。
This should be removing the last character in the string whenever it's !
只要有,它就应该删除字符串中的最后一个字符
!
: :
if (lastChar === "!") {
s = s.substring(0, s.length - 1);
}
I've also tried: 我也尝试过:
s = s.replace("!", "");
But same result. 但结果相同。 Any ideas?
有任何想法吗?
Because you're increasing i
and checking i < s.length
on each loop. 因为您要增加
i
并在每个循环中检查i < s.length
。 At one point, you remove a !
有时,您删除了
!
(thus shortening the string) and i
is equal to s.length
and you never check the last char. (因此缩短了字符串),而
i
等于s.length
而您从不检查最后一个字符。
There's no reason for i
at all. 没有理由为
i
所有。 (Or a for
loop, but if that was the requirement in the challenge...) (或者是
for
循环,但是如果这是挑战中的要求...)
If you step through it with your debugger, you'll see the problem. 如果使用调试器逐步解决问题,则会发现问题。 This version using
console.log
also shows the problem: 使用
console.log
该版本还显示了问题:
function remove(s) { for (i = 0; i < s.length; i++) { let lastChar = s.slice(-1); if (lastChar === "!") { s = s.substring(0, s.length - 1); console.log(`i = ${i}, s = '${s}', s.substring(i) = '${s.substring(i)}'`); } else { console.log(`returning '${s}'`); return s; } } console.log(`returning '${s}' at end, because ${i} >= ${s.length}`); return s; } remove("isl!!!!!");
.as-console-wrapper { max-height: 100% !important; }
You can do this without using for loop. 您无需使用for循环即可执行此操作。
const stringRemover (str) => {
if (str[str.length-1] === "!") {
return str.slice(0,str.length-1);
} else {
return str;
}
}
You can create a recursive function and check if the last char using CharAt
if it is !
您可以创建一个递归函数,并使用
CharAt
检查最后一个char是否为!
. 。 If it is so then again call the same function but with new string which is created after removing the last
!
如果是这样,则再次调用相同的函数,但要使用删除最后一个字符串后创建的新字符串
!
Not sure why the for
is needed if the last character is needed 如果需要最后一个字符,则不确定为什么需要
for
function remove(str) { let getLastChar = str.charAt(str.length - 1); if (getLastChar === '!') { return remove(str.substring(0, str.length - 1)) } else { return str; } } console.log(remove("isl!!!!!"));
Here is codewars result 这是代码战的结果
As answered in a previous reply, i < s.length
is checked in every iteration in a for
loop. 如上一个答复中所述,在
for
循环的每次迭代中都会检查i < s.length
。 Try this : 尝试这个 :
function remove(s) {
let a = s.length;
for (i = 0; i < a; i++) {
let lastChar = s.slice(-1);
if (lastChar === "!") {
s = s.substring(0, s.length - 1);
}
else {
return s;
}
}
return s;
}
@TJ Crowder pointed me in the right direction, but he didn't provide an answer that followed my original logic (in this case I wanted to use a for-loop). @TJ Crowder向我指出了正确的方向,但是他没有提供符合我原始逻辑的答案(在这种情况下,我想使用一个for循环)。
The key takeaway is that s = s.replace("!", "");
关键要点是
s = s.replace("!", "");
will work when i--
and s = s.replace(/!+$/g, '')
will work when i++
. 当
i--
和s = s.replace(/!+$/g, '')
将在i++
时工作。 Because, as far as I understand, the replace() method only replaces the first occurrence of the string, which is why we need i--
to force the loop to iterate backwards through the string, making sure that every occurance of "!"
因为据我所知, replace()方法仅替换字符串的第一次出现,因此我们需要
i--
强制循环向后遍历字符串,以确保每次出现"!"
gets replaced. 被替换。
Ie this will work: 即这将工作:
function remove(s) {
for (i = 0; i < s.length; i--) {
let lastChar = s.slice(-1);
if (lastChar === "!") {
s = s.replace("!", '')
}
else {
return s;
}
}
return s;
}
And this will also work: 这也将起作用:
function remove(s) {
for (i = 0; i < s.length; i++) {
let lastChar = s.slice(-1);
if (lastChar === "!") {
s = s.replace(/!+$/g, '');
}
else {
return s;
}
}
return s;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.