Eloquent JavaScript 字符串连接语法
Eloquent JavaScript string concatenation syntax
所以这是来自 Eloquent Javascript。我想弄清楚为什么 return 找到的是“(” + history + “ + 5)” 而不是 “(history + 5)”???
这是代码片段:
function findSolution(target) {
function find(start, history) {
if (start == target)
return history;
else if (start > target)
return null;
else
return find(start + 5, "(" + history + " + 5)") ||
find(start * 3, "(" + history + " * 3)");
}
return find(1, "1");
}
结果:
console.log(findSolution(24));// → (((1 * 3) + 5) * 3)
所以“(” + history + “ + 5)”和“(history + 5)”之间的区别是
"(" + 历史 + " + 5)"
"(" --> String
history -->Number
"+5)" -->String
现在,如果您使用 + 组合这些变量,它将像串联一样工作
所以 "(" + history + "+5)"
会给你 (5+5) 作为字符串
但是"(history + 5)" -->String
就像 (history + 5) 因为 history 现在不是变量,它是字符串文字
希望对您有所帮助
所以这是来自 Eloquent Javascript。我想弄清楚为什么 return 找到的是“(” + history + “ + 5)” 而不是 “(history + 5)”???
这是代码片段:
function findSolution(target) {
function find(start, history) {
if (start == target)
return history;
else if (start > target)
return null;
else
return find(start + 5, "(" + history + " + 5)") ||
find(start * 3, "(" + history + " * 3)");
}
return find(1, "1");
}
结果:
console.log(findSolution(24));// → (((1 * 3) + 5) * 3)
所以“(” + history + “ + 5)”和“(history + 5)”之间的区别是
"(" + 历史 + " + 5)"
"(" --> String
history -->Number
"+5)" -->String
现在,如果您使用 + 组合这些变量,它将像串联一样工作
所以 "(" + history + "+5)"
会给你 (5+5) 作为字符串
但是"(history + 5)" -->String
就像 (history + 5) 因为 history 现在不是变量,它是字符串文字
希望对您有所帮助