使用while循环遍历数组

时间:2019-03-22 15:21:58

标签: javascript arrays while-loop

我正在通过训练营实验室,并在循环实验室中面临一些挑战。练习要求创建一个接受一个参数(一组事实)的函数。我必须使用while循环遍历事实数组,在每个事实的末尾添加!!!,并返回带有感叹号的字符串数组。

const facts = [
  "He was the last Beatle to learn to drive",
  "He was never a vegetarian",
  "He was a choir boy and boy scout",
  "He hated the sound of his own voice"
];

function johnLennonFacts() {
  let i = 0;
  while (i <= facts.lenght) {
    i++;
    console.log(facts[i] + '!!!');
  }
  return facts
}

4 个答案:

答案 0 :(得分:0)

这是您要实现的目标:

const facts = [
  "He was the last Beatle to learn to drive",
  "He was never a vegetarian",
  "He was a choir boy and boy scout",
  "He hated the sound of his own voice"
];

// With one argument (array of facts) it's better :p
function johnLennonFacts(facts) {
  let i = 0;
  const factsModified = [];
  while (i <= facts.length) {
    factsModified.push(facts[i] + '!!');
    i++;
  }

  return factsModified;
}

答案 1 :(得分:0)

如果您不必使用const facts = [ "He was the last Beatle to learn to drive", "He was never a vegetarian", "He was a choir boy and boy scout", "He hated the sound of his own voice" ]; function append(arr) { newArr = arr.map(el => el + ' !!!'); return newArr; } console.log(append(facts));循环,map会更干净。

{{1}}

答案 2 :(得分:0)

查看代码中的注释:

const data = [
  "He was the last Beatle to learn to drive",
  "He was never a vegetarian",
  "He was a choir boy and boy scout",
  "He hated the sound of his own voice"
];

function johnLennonFacts(facts) {
//                       ^^^^^
// your function has to accept the argument here

  var data = facts.slice(); // make a copy of the passed array
  
  var i = 0;

  while (i < data.length) { // fix misspelled length

    data[i] += '!!!'; // use += to add the !!!
    
    i++; // increment after adding the !!!
  }

  return data
}

var result = johnLennonFacts(data); // call the function 
console.log(result);

答案 3 :(得分:0)

通常,我更喜欢从一开始就知道迭代次数的情况下使用for loop,并且在这种情况下,map可能最有意义,如{{3 }}(但我知道这是一项任务)。您需要修复一些问题(注释和其他答案中已经提到了一些问题,但在此处进行合并以供参考)。

1)您的作业表明您的@kemotoe需要接受一个参数function。因此,您需要在函数定义的括号内包含一个参数(或参数),然后在调用该函数时将输入数组传递给该函数。像这样:

// function definition
function johnLennonFacts(arr) {
  // your code
}

// pass your "facts" array to the function when you call it
johnLennonFacts(facts);

2)您当前的array语句将尝试记录数组的最后3个元素,因为在记录数组的值之前要递增i,并且1是第二个元素的索引因为数组是零索引的(因此,数组中的第一个元素的索引为0)。然后,当i递增到4但索引{4的facts中没有元素时,您的循环将遇到无法解决的问题。其他答案为您提供了一些有关如何解决问题的见解。 while循环。

3)您需要为计划从函数返回的数组中的索引分配facts[i] + '!!!'。 JavaScript不知道您是否希望基于此行更改facts[i],它只是看到您正在将其与其他文本组合在一起,但没有将其放置在任何地方。其他答案为如何将新组合的字符串分配给从函数返回的数组中的索引提供了一些见识。

只是为了好玩,这是其他答案的替代方法,可以使您对函数,循环和数组的工作方式有更多了解。

const facts = [
  "He was the last Beatle to learn to drive",
  "He was never a vegetarian",
  "He was a choir boy and boy scout",
  "He hated the sound of his own voice"
];

const exclamation = (arr) => {
  let i = arr.length;
  while (i) {
    i--;
    arr[i] += '!!!';
  }
  return arr;
}

let loudfacts = exclamation(facts);
console.log(loudfacts);
/*
[
  "He was the last Beatle to learn to drive!!!",
  "He was never a vegetarian!!!",
  "He was a choir boy and boy scout!!!",
  "He hated the sound of his own voice!!!"
]
*/

  • const exclamation = (arr) => {...}是定义函数(称为while)的另一种方式
  • 首先分配i = arr.length,以便我们可以在数组中向后循环
  • 使用i递减i--,然后再将新字符串分配给任何数组索引,因为我们知道数组的最高索引将比其长度小一,因为数组的索引为零。 / li>
  • 使用arr[i] += '!!!'为每个数组索引分配一个新的字符串值,这是说arr[i] = arr[i] + '!!!'的快捷方式
  • 因为零是一个arrow function值,所以在i递减为零的迭代之后退出while循环
  • 然后我们从函数返回修改后的arr
  • 我们调用函数并将facts数组传递给它,并使用let loudfacts = exclamation(facts);将返回值分配给变量