【问题标题】:Can you highlight where I've gone wrong你能强调我哪里出错了吗
【发布时间】:2022-01-15 23:45:39
【问题描述】:

我已经为此工作了超过 12 个小时。

我真的很喜欢编码,但我很不擅长。

能否请您用红色突出显示我在此代码中出错的地方?

(function (window) {
var names = {"Yaakov", "John", "Jen", "Jason", "Paul", "Frank", "Larry", "Paula", "Laura", "Jim"};
for (let i = 0; i < names.length; i++) {
if (firstLetter === 'j')  || 'J'{
    byeSpeaker.speak(names[i]);
  } else {
    helloSpeaker.speak(names[i]);
  } 
}

})(window);

(function (window) {

var speakWord = "Hello";

var helloSpeaker = speakWord;

helloSpeaker.speak(name) = fuction () 
    
(speakWord + " " + name);

window.helloSpeaker = helloSpeaker;
console.log(window);})

(function (window) {

var speakWord = "Good Bye";

var byeSpeaker = speakWord;

byeSpeaker.speak(name) = function () {
    
(speakWord + " " + name);}

window.byeSpeaker = byeSpeaker;

console.log(window);})
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Module 4 Solution Starter</title>
  <script src="SpeakHello.js"></script>
  <script src="SpeakGoodBye.js"></script>
  <script src="script.js"></script>
</head>
<body>
  <h1>Module 4 Solution Starter</h1>
</body>
</html>

它应该循环输入你好名字但再见以 J 开头的名字。

如果您能提供更多反馈,我将不胜感激。

提前致谢。

娜塔莎

【问题讨论】:

  • 浏览器控制台会向您显示错误所在。您正在尝试使用 {} 来定义数组而不是 []
  • 数组应该用@David 提到的[] 声明,而不是{}。您的 if 语句是错误的,正如下面提到的@DerEchteKroate。您的 firstLetter 变量未在任何地方声明。除此之外,您下面的函数声明也是错误的。

标签: javascript for-loop syntax javascript-objects curly-braces


【解决方案1】:

您的第一个 if 语句是错误的。应该是这样的:

if (firstLetter === 'j' || firstLetter === 'J') {

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多