freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../basic-data-structures/iterate-through-all-an-arra...

4.2 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d7b7b367417b2b2512b15 Iterate Through All an Array's Items Using For Loops 1 使用for循环遍历所有数组的项目

Description

有时在使用数组时,能够遍历每个项目以查找我们可能需要的一个或多个元素,或者根据哪些数据项符合某组标准来操作数组非常方便。 JavaScript提供了几种内置方法每种方法都以稍微不同的方式迭代数组以实现不同的结果例如every() forEach() map()等),但是这种技术最灵活并且为我们提供了最大的控制量是一个简单的for循环。考虑以下:
function greaterThanTenarr{
让newArr = [];
forlet i = 0; i <arr.length; i ++{
ifarr [i]> 10{
ARR [I]newArr.push;
}
}
返回newArr;
}

greaterThanTen[2,12,8,14,80,0,1];
//返回[12,14,80]
使用for循环,此函数遍历并访问数组的每个元素,并使其经受我们创建的简单测试。通过这种方式,我们可以轻松地以编程方式确定哪些数据项大于10 ,并返回包含这些项的新数组。

Instructions

我们定义了一个函数filteredArray ,它接受arr ,一个嵌套数组和elem作为参数,并返回一个新数组。 elem表示在arr嵌套的一个或多个数组上可能存在或不存在的元素。使用for循环修改函数,以返回已传递数组的过滤版本,以便删除嵌套在包含elem arr中的任何数组。

Tests

tests:
  - text: '<code>filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18)</code>应该返回<code>[ [10, 8, 3], [14, 6, 23] ]</code>'
    testString: 'assert.deepEqual(filteredArray([ [10, 8, 3], [14, 6, 23], [3, 18, 6] ], 18), [[10, 8, 3], [14, 6, 23]], "<code>filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18)</code> should return <code>[ [10, 8, 3], [14, 6, 23] ]</code>");'
  - text: '<code>filteredArray([ [&quot;trumpets&quot;, 2], [&quot;flutes&quot;, 4], [&quot;saxophones&quot;, 2] ], 2)</code>应返回<code>[ [&quot;flutes&quot;, 4] ]</code>'
    testString: 'assert.deepEqual(filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2), [["flutes", 4]], "<code>filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2)</code> should return <code>[ ["flutes", 4] ]</code>");'
  - text: '<code>filteredArray([ [&quot;amy&quot;, &quot;beth&quot;, &quot;sam&quot;], [&quot;dave&quot;, &quot;sean&quot;, &quot;peter&quot;] ], &quot;peter&quot;)</code>应该返回<code>[ [&quot;amy&quot;, &quot;beth&quot;, &quot;sam&quot;] ]</code>'
    testString: 'assert.deepEqual(filteredArray([["amy", "beth", "sam"], ["dave", "sean", "peter"]], "peter"), [["amy", "beth", "sam"]], "<code>filteredArray([ ["amy", "beth", "sam"], ["dave", "sean", "peter"] ], "peter")</code> should return <code>[ ["amy", "beth", "sam"] ]</code>");'
  - text: '<code>filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3)</code>应该返回<code>[ ]</code>'
    testString: 'assert.deepEqual(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3), [], "<code>filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3)</code> should return <code>[ ]</code>");'
  - text: <code>filteredArray</code>函数应该使用<code>for</code>循环
    testString: 'assert.notStrictEqual(filteredArray.toString().search(/for/), -1, "The <code>filteredArray</code> function should utilize a <code>for</code> loop");'

Challenge Seed

function filteredArray(arr, elem) {
  let newArr = [];
  // change code below this line

  // change code above this line
  return newArr;
}

// change code here to test different cases:
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));

Solution

// solution required