I have array like this.
const cntry = ['Albania', 'Bolivia', 'Canada', 'Denmark', 'Ethiopia', 'Finland', 'Germany', 'Hungary', 'Turkey', 'Iceland', 'Ireland'];
I want to delete elements that contain the word "land" from this array but since I am studying "for loop" i want to remove it using only the "for" method.
I tried this code but it didn't work. "Ireland" is still in the array. where did I go wrong?
for (let i = 0; i < cntry.length; i ) {
if (cntry[i].includes('land')) {
cntry.splice(i, 1);
}
}
Output:
(9) ['Albania', 'Bolivia', 'Canada', 'Denmark', 'Ethiopia', 'Germany', 'Hungary', 'Turkey', 'Ireland']
CodePudding user response:
You're mutating cntry
as you for
loop it. This breaks the increment/index relationship of all the remaining elements as soon as your remove an element from the array. So instead, reverse the loop—start from the end of the array and work toward the start:
const cntry = ['Albania', 'Bolivia', 'Canada', 'Denmark', 'Ethiopia', 'Finland', 'Germany', 'Hungary', 'Turkey', 'Iceland', 'Ireland'];
// start with the length of array minus 1
// decrement i by 1 after each loop
// leave loop when i is less than 0
for (let i = cntry.length - 1; i >= 0; i--) {
if (cntry[i].includes('land')) {
cntry.splice(i, 1);
}
}
console.log(cntry);
Now when you remove an element, the preceding indexes still correspond to your current increment value.
Elaborating on: for (let i = cntry.length - 1; i >= 0; i--)
In your for loop, set increment variable i
to the length of the array minus 1. Since arrays are zero indexed, meaning the first element is indexed 0
, you'll need to start your increment at the length of the array minus 1. There are 11 elements in your array so the last element is indexed 10
.
let i = cntry.length - 1;
Then allow loop while i
is greater to or equal to >=
0. In other words, leave the loop when i
is less than zero:
i >= 0;
And finally, decrement
i
. The for loop increment change happens after the loop block has been evaluated and before the next iteration.
i--
In the "real world" you'd use a loop construct that is well suited to the block of code you wish to evaluate. Since you are mutating the array it would be more suitable to forEach
loop since it does not depend on maintaining an increment/index relationship. Maybe that's the purpose of your homework assignment, to show you when a for loop is not appropriate.
CodePudding user response:
You can filter
your list because when you use slice
you are overwriting the original array:
const countryList = ['Albania', 'Bolivia', 'Canada', 'Denmark', 'Ethiopia', 'Finland', 'Germany', 'Hungary', 'Turkey', 'Iceland', 'Ireland']
const filter = 'land'
const filteredList = []
for (const country of countryList) {
if (!country.includes(filter)) {
filteredList.push(country)
}
}
CodePudding user response:
There is already a better answer that utilizes the existing array but here is an alternative solution.
const cntry = ['Albania', 'Bolivia', 'Canada', 'Denmark', 'Ethiopia', 'Finland', 'Germany', 'Hungary', 'Turkey', 'Iceland', 'Ireland'];
const newArray = []; // new target array for countries that don't have 'land' in the name
// looping through the cntry array
for (let i = 0; i < cntry.length; i ) {
// checking to see if the current country in the loop iteration doesn't include 'land' - if false then push that country to the array => newArray
if (!cntry[i].includes('land')) {
newArray.push(cntry[i]);
}
}
console.log(newArray);