标签导航:

freecodecamp步骤46审核失败:为什么我的商店代码需要添加进入商店提示?

FreeCodeCamp JavaScript 挑战步骤 46 审核失败原因及解决方案

在 FreeCodeCamp 的 JavaScript 挑战中,部分代码未能通过审核,问题在于缺少进入商店的提示信息。

原始代码 (审核失败):

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;
}

问题分析: 审核失败的原因是代码缺少对玩家进入商店行为的反馈。 FreeCodeCamp 的测试可能检查了是否正确显示了“You enter the store.” 这样的提示信息。

修改后的代码 (审核通过):

function goStore() {
  text.innerText = 'You enter the store.'; // 添加进入商店提示
  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;
}

关键修改: 在 goStore 函数的开头添加了 text.innerText = 'You enter the store.'; 这行代码,向玩家显示进入商店的提示信息。 注意大小写变化,innerText 应该使用大写 I。 函数名也建议使用驼峰命名法 goStore。 确保 text 变量已正确定义并指向用于显示提示信息的元素。

通过添加这行代码,解决了审核失败的问题,程序能够正确地显示进入商店的提示信息,从而通过 FreeCodeCamp 的测试。