
FreeCodeCamp 46步审核失败详解及解决方案
许多 FreeCodeCamp 用户在完成第46步挑战时,遭遇了代码审核失败。本文将分析其原因并提供解决方案。
问题代码示例:
function gostore() {
button1.innertext = "buy 10 health (10 gold)";
button2.innertext = "buy weapon (30 gold)";
button3.innertext = "go to town square";
button1.onclick = buyhealth;
button2.onclick = buyweapon;
button3.onclick = gotown;
}审核失败原因:缺少文本更新
审核失败的核心原因在于代码缺少关键的文本更新语句。程序需要向用户显示进入商店的提示信息。 缺少的代码行如下:
text.innerText = "you enter the store.";
这行代码负责更新游戏文本区域,告知玩家已进入商店。
修正后的完整代码:
为了顺利通过审核,需要添加上述缺失的代码行。修正后的代码如下:
function goStore() {
button1.innerText = "Buy 10 health (10 gold)";
button2.innerText = "Buy weapon (30 gold)";
button3.innerText = "Go to town square";
button1.onclick = buyHealth;
button2.onclick = buyWeapon;
button3.onclick = goTown;
text.innerText = "You enter the store.";
}注意:代码中的变量名 innerText 应为首字母大写 innerText,函数名也应保持一致的大小写(例如 goStore, buyHealth, buyWeapon, goTown)。 添加了这行代码后,即可解决审核失败的问题,顺利完成 FreeCodeCamp 第46步挑战。

