Files
.github
api-server
client
config
curriculum
docs
guide
arabic
chinese
english
3D
accessibility
agile
algorithms
android-development
angular
angularjs
apache
aspnet
bash
blockchain
book-recommendations
bootstrap
bsd-os
bulma
c
canvas
certifications
apis-and-microservices
coding-interview-prep
data-visualization
front-end-libraries
information-security-and-quality-assurance
javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
-iterate-through-the-keys-of-an-object-with-a-for...in-statement
access-an-arrays-contents-using-bracket-notation
access-property-names-with-bracket-notation
add-items-to-an-array-with-push-and-unshift
add-items-using-splice
add-key-value-pairs-to-javascript-objects
check-for-the-presence-of-an-element-with-indexof
check-if-an-object-has-a-property
combine-arrays-with-the-spread-operator
copy-an-array-with-the-spread-operator
copy-array-items-using-slice
create-complex-multi-dimensional-arrays
generate-an-array-of-all-object-keys-with-object.keys
iterate-through-all-an-arrays-items-using-for-loops
index.md
modify-an-array-stored-in-an-object
modify-an-object-nested-within-an-object
remove-items-from-an-array-with-pop-and-shift
remove-items-using-splice
use-an-array-to-store-a-collection-of-data
use-the-delete-keyword-to-remove-object-properties
index.md
basic-javascript
debugging
es6
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
index.md
responsive-web-design
index.md
chef
clojure
cloud-development
computational-genomics
computer-hardware
computer-science
containers
cplusplus
csharp
css
d3
data-science-tools
design-patterns
designer-tools
developer-ethics
developer-tools
devops
docker
documentation
electron
elixir
elm
erlang
game-development
gatsbyjs
git
go
groovy
haskell
hibernate
html
ionic
java
javascript
joomla
jquery
julia
kotlin
laravel
linux
logic
machine-learning
mathematics
meta
miscellaneous
mobile-app-development
mongodb
natural-language-processing
neovim
network-engineering
nginx
nodejs
php
product-design
progressive-web-apps
puppet
python
r
react
react-native
redux
rest-api
robotics
rt-os
ruby
rust
security
semantic-ui
software-engineering
sql
ssh
svg
svn
swift
terminal-commandline
tools
typescript
typography
user-experience-design
user-experience-research
vagrant
vim
virtualbox
visual-design
voice
vue
vue-cli
web-augmented-reality
web-components
web-performance
web-virtual-reality
wordpress
working-in-tech
xml
Blender
portuguese
russian
spanish
news
tools
.editorconfig
.eslintignore
.eslintrc
.gitattributes
.gitignore
.node-inspectorrc
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE.md
README.md
docker-compose-shared.yml
docker-compose.yml
lerna.json
netlify.toml
package-lock.json
package.json
sample.env

41 lines
1.3 KiB
Markdown
Raw Normal View History

2018-10-12 15:37:13 -04:00
---
title: Iterate Through All an Array's Items Using For Loops
---
## Iterate Through All an Array's Items Using For Loops
## Hint 1
- A nested ``for`` loop must be used to search through every element in the array.
```javascript
for (let i = 0; i < arr.length; i++) {
````
## Hint 2
- Every element of the array must then be compared to the `elem` parameter passed through the `filteredArray()` function.
```javascript
if (arr[i].indexOf(elem)==-1){
```
## Hint 3
- If a match is NOT found then `newArr` have that entire subarray added. The `push()` function is very useful here.
```javascript
newArr.push(arr[i]);
```
- Once that entire subarray is added to `newArr` the loop continue with the next element.
## Solution:
```javascript
function filteredArray(arr, elem) {
let newArr = [];
// change code below this line
for (let i = 0; i < arr.length; i++) {
if (arr[i].indexOf(elem)==-1){ //Checks every parameter for the element and if is NOT there continues the code
newArr.push(arr[i]); //Inserts the element of the array in the new filtered array
};
};
// 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));
```