[英]js objects and if statements. why this code works without an else and using an else or else if statement doesn't?
I am a very beginner JS "developer" (student) and I have run into a question I haven't been able to solve: why does the value of the repeated letters in my 'helperHash' increase when I am not using an else statement and why this same value DOESN't increase if I use an else statement? 我是一个非常入门的JS“开发人员”(学生),我遇到一个我无法解决的问题:当我不使用else语句时,为什么“ helperHash”中重复字母的值会增加为什么使用else语句时相同的值不会增加? My code runs as expected but I am having problems understanding the logic behind this issue... 我的代码按预期运行,但是我在理解此问题背后的逻辑时遇到了问题。
The code is supposed to return an array with the letters that have 1 or more repetitions in the given str. 该代码应返回一个数组,该数组的字母在给定的str中具有1个或多个重复。
function nonUniqueLetters(str){
var strToChars = str.split('');
var finalArr = [];
var helperHash = {};
for (let i = 0; i < strToChars.length; i += 1){
let char = str[i];
if (!helperHash[char]){
helperHash[char] = 0;
}
helperHash[char] += 1; //HERE! why doesn't this work if inside an else?
}
for (var key in helperHash){
if (helperHash[key] > 1){
finalArr.push(key);
}
}
return finalArr;
}
For helperHash[char]
… 对于helperHash[char]
…
The initial value is undefined
and !undefined
is true
so it sets the value to 0
. 初始值为undefined
, !undefined
为true
因此将其设置为0
。
The next time char
has the same value, helperHash[char]
is 0
and !0
is also true
so it sets the value to 0
(which it already is, so it makes no difference). 下次char
具有相同的值时, helperHash[char]
为0
且!0
也为 true
因此它将值设置为0
(该值已经是,因此没有区别)。
Instead of testing if the value is a false value, test if it is undefined, or if it exists at all. 代替测试该值是否为假值,可以测试它是否未定义或是否存在。
if (typeof helperHash[char] === "undefined")
or 要么
if (char in helperHash)
The reason is this if (!helperHash[char]){
and how integers are converted to booleans in Javascript. 原因是这样的if (!helperHash[char]){
以及如何在Javascript中将整数转换为布尔值。
You initialize every member of the hash to 0 which equals a boolean false, so the else is never hit because helperHash[char] === 0 === false
and thus !helperHash[char]
is true for all values initialized with 0 您将哈希的每个成员都初始化为0,这等于布尔值false,因此绝不会命中else,因为helperHash[char] === 0 === false
,因此!helperHash[char]
对于所有以0初始化的值都是true
Logic error. 逻辑错误。
if (!helperHash[char]){
// Enters here only when helperHash[char] is not set (or 0, but it is never 0)
helperHash[char] = 0;
}
// Always increment
helperHash[char] += 1;
// There is no 0 in helperHash at this point
helperHash[char] += 1
in an else
branch doesn't work: 为什么将helperHash[char] += 1
放在else
分支中不起作用: if (!helperHash[char]){
// Enters here only when helperHash[char] is not set or 0
helperHash[char] = 0;
// Still 0, will take this branch again on the next occurrence of char
} else {
// Increment only if it was already 1 or more (this never happens)
helperHash[char] += 1;
}
// Here helperHash contains only 0 for all keys
if (!helperHash[char]){
// This is the first occurrence of char, let's count it
helperHash[char] = 1;
} else {
// This is a subsequent occurrence of char, let's count it too
helperHash[char] += 1;
}
// There is no 0 in helperHash at this point
Your if
condition: 您的if
条件:
!helperHash[char]
is always evaluating to true
( helperHash
never has characters in it that are "falsy"). 总是评估为true
( helperHash
永远不会包含“虚假”字符)。 So, an else
branch of that if
would never be hit. 因此, if
永远不会被击中, if
该分支的else
分支。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.