【问题标题】:Using a for loop with a function in Javascript [closed]在Javascript中使用带有函数的for循环[关闭]
【发布时间】:2021-02-16 20:40:15
【问题描述】:

我无法弄清楚如何解决以下挑战:

挑战:机器人

完成接受字符串数组并使用 FOR 循环遍历数组的函数 droids。将变量结果更新为“找到机器人!”如果数组包含字符串“Droids”。否则将变量结果更新为“这些不是您要寻找的机器人”。返回您的更新结果。

这是目前编写的代码:

function droids(arr) {
  let result = '';
  // ADD CODE HERE
  return result;
}

// Uncomment these to check your work! 
const starWars = ["Luke", "Finn", "Rey", "Kylo", "Droids"] 
 const thrones = ["Jon", "Danny", "Tyrion", "The Mountain", "Cersei"] 
 console.log(droids(starWars)) // should log: "Found Droids!"
 console.log(droids(thrones)) // should log: "These are not the droids you're looking for."

有人可以帮忙找出我需要添加什么来使用javascript解决这个问题吗?非常感谢

【问题讨论】:

  • 您是否查看过任何有关 JavaScript 循环的教程或文档?
  • 只需在您的函数中执行return arr.includes('Droids')。你甚至不需要 for 循环。
  • 这里是如何使用 for statement - 提示:数组索引从 0 到它们的长度 - 1 .... 所以 for (i = 0; i < arr.length; i++) 应该让你开始
  • @Terry ... 使用 FOR 循环 ...所以,虽然可行,但它并没有回答他的任务

标签: javascript arrays function loops for-loop


【解决方案1】:

您可以遍历arr 并在找到它后立即返回Found Droid,或者如果找不到则返回not found

简单的方法如下。

function droids(arr) {
  for(var str of arr) {
      if (str  === 'Droids') {
       return 'Found Droid';
    }
  }
 return `These are not the droids you're looking for`;
}

// Uncomment these to check your work! 
const starWars = ["Luke", "Finn", "Rey", "Kylo", "Droids"]
const thrones = ["Jon", "Danny", "Tyrion", "The Mountain", "Cersei"]
console.log(droids(starWars)) // should log: "Found Droids!"
console.log(droids(thrones)) // should log: "These are not the droi

【讨论】:

    【解决方案2】:

    只需遍历数组并进行比较

    function droids(arr) {
      let result = 'These are not the droids you\'re looking for';
      for(let i=0; i<arr.length;i++) {
          if (arr[i] === 'Droids') {
          result = 'Found Droid';
        }
      }
      return result;
    }
    
    // Uncomment these to check your work! 
    const starWars = ["Luke", "Finn", "Rey", "Kylo", "Droids"]
    const thrones = ["Jon", "Danny", "Tyrion", "The Mountain", "Cersei"]
    console.log(droids(starWars)) // should log: "Found Droids!"
    console.log(droids(thrones)) // should log: "These are not the droi
    
    
    
    
    
    //A simpler approach 
    
    console.log(starWars.includes('Droids') ? 'Droid Found' : 'These are not the droids you\'re looking for');
    console.log(thrones.includes('Droids') ? 'Droid Found' : 'These are not the droids you\'re looking for');

    【讨论】:

    • 这看起来不像 for 循环... "完成函数 droids 接受字符串数组并使用 FOR 循环遍历数组"
    • @Nick 更新了答案,我的错
    • 该问题还专门要求提供字符串Droids,您的代码将匹配Android
    • 具有讽刺意味的是,并不是我对这篇文章投了反对票,但既然它现在确实有效,我至少会平衡它。
    • 不用担心 - 只是想提高网站的整体质量......
    【解决方案3】:
    function droids(arr) {
        result = "These are not the droids you're looking for." 
        
        for (str of arr) {
            if (str == 'Droids')
                result = "Found Droids!"
        }
        
        return result;
    }
    

    【讨论】:

    • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助、质量更好,并且更有可能吸引投票。
    【解决方案4】:

    关注这个希望你解决这个问题。

    const starWars = ["Luke", "Finn", "Rey", "Kylo", "Droids"];
    for (i = 0; i < starWars.length; i++) {
      console.log(starWars[i]);
    } 
    

    【讨论】:

      猜你喜欢
      • 2014-02-12
      • 1970-01-01
      • 1970-01-01
      • 2016-08-26
      • 2021-12-21
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多