by Tiffany White
蒂芙尼·怀特(Tiffany White)
哭泣算法的眼泪 (Crying Algorithm Tears)
“Laughter and tears are both responses to frustration and exhaustion. I myself prefer to laugh, since there is less cleaning to do afterward.” ― Kurt Vonnegut
笑声和眼泪都是对沮丧和疲惫的回应。 我自己更喜欢笑,因为事后清洁工作减少了。” ―库尔特·冯内古特
There comes a point in every new programmers life when they hit a barrier, a wall, a threshold between understanding and not understanding the material at hand.
在每个新程序员的生活中,都有一个障碍,就是在理解和不了解手头的材料之间遇到了障碍,隔离墙,门槛。
I hit that threshold yesterday.
我昨天达到了这个门槛。
And the day before yesterday.
还有前天。
In retrospect, the solution was so simple. I had the right idea several times. I got encouraged, and explained to, and guided, but it was like their words just bounced off of my skull instead of being absorbed into my grey matter.
回想起来,解决方案是如此简单。 我有好几次主意了。 我得到了鼓励,解释和指导,但这就像他们的话从我的头顶跳了起来,而不是被我的灰质所吸收。
The algorithm challenge was:
算法挑战是:
Check if a string (first argument) ends with the given target string (second argument).
检查字符串(第一个参数)是否以给定的目标字符串(第二个参数)结尾。
Remember to use Read-Search-Ask if you get stuck. Write your own code.
如果遇到问题,请记住使用Read-Search-Ask。 编写自己的代码。
Here are some helpful links:
以下是一些有用的链接:
String.substr()
String.substr()
The code Free Code Camp started me off with:
代码免费代码训练营让我开始了:
function end(str, target) {
// “Never give up and good luck will find you.”
// — Falcor
return str;
}
end(“Bastian”, “n”);
搞什么鬼? 子串? (What the Hell? Substrings?)
You’ve done it before and you can do it now. See the positive possibilities. Redirect the substantial energy of your frustration and turn it into positive, effective, unstoppable determination. –Ralph Marston
您之前已经做过,现在可以做。 看到积极的可能性。 转移沮丧的实质能量,并将其转变为积极,有效,不可阻挡的决心。 –拉尔夫·马斯顿
I knew from looking at the failing tests that my algorithm had to handle strings of different lengths. But I kept hardcoding for just one of the test’s strings.
通过查看失败的测试,我知道我的算法必须处理不同长度的字符串。 但是我只对测试的字符串之一进行硬编码。
How do I code this thing for different string lengths? How do I get the length of a string? .length() right? YES. But how. Where do I put the .length()?
如何为不同的字符串长度编写代码? 如何获得字符串的长度? .length()对吗? 是。 但是如何 。 我将.length()放在哪里?
I had this code:
我有以下代码:
function end(str, target) {
//”Never give up and good luck will find you.”
// — Falcor
//’abcdefghijklmn’.substr(0, 3)
// ‘abc’
//”grab 3 characters starting with the character at address number 0"
var isEqual = str.substr(6, 1) === target.substr(0, 1);
return isEqual;
} end(“Bastian”, “n”);
I found out in one of Free Code Camp’s help chat rooms that you can get to the end of a string by using a negative number. No need to keep popping off all those letters before the “n” on Bastian.
我在Free Code Camp的帮助聊天室之一中发现,可以使用负数来到达字符串的末尾。 无需继续在Bastian的“ n”之前弹出所有这些字母。
But I continued to hard code for “Bastian” and “n”.
但是我继续对“ Bastian”和“ n”进行硬编码。
I needed a broader approach.
我需要更广泛的方法。
I tried:
我试过了:
function end(str, target) {
var isEqual = str.substr(-1) === target.substr(-1); return isEqual;
} end(“Bastian”, “n”);
But I wasn’t really making any progress. All but one of the tests were passing, and I still wasn’t really utilizing .length() to address the variance in string length.
但是我并没有真正进步。 除了其中一个测试之外,所有其他测试都通过了,我仍然没有真正利用.length()来解决字符串长度的差异。
So I tried this:
所以我尝试了这个:
function end(str, target) {
var n = target.length; var z = str.length; var isEqual = str.substr(-1) === target.substr(-1); return isEqual;
} end(“Bastian”, “n”);
Same result. I knew I needed to have .length() up there. But where to go after that?
结果相同。 我知道我需要在那儿有.length()。 但是那之后该去哪里呢?
啊哈! (Aha!)
Finally, I had to be guided to the answer. The woman was in Britain and I am pretty sure I was keeping her awake. But together we came up with this solution:
最后,必须指导我答案。 那个女人在英国,我很确定自己正在保持清醒。 但是我们一起想出了以下解决方案:
// You didn't think I'd give it away, did you?
And finally I understood it. It took a while to get there, but when we reached the solution, I felt like a complete idiot. How could I have not understood this earlier?
最后我明白了。 到达那里花了一段时间,但是当我们找到解决方案时,我感觉自己像个白痴。 我怎么会更早不了解呢?
I cried. I literally cried. Part of that was just me already being emotional.
我哭了。 我真的哭了。 部分原因是我已经很情绪化。
The other part was me not wanting to put my fist through my MacBook Pro’s screen.
另一部分是我不想将拳头穿过MacBook Pro的屏幕。
Strings are characters. Not words. And I was totally getting stuck on that.
字符串是字符。 没话说。 我完全陷入了困境。
Algorithm tears indeed.
算法的确的眼泪。
Originally published at Code Newbie in Pittsburgh.
最初发表于匹兹堡的Code Newbie 。
翻译自: https://www.freecodecamp.org/news/bonfire-tears-free-code-camp-edition-d79bbfd3d945/