Home > database >  how would you iterate through an array, incrementing a variable in the global scope, when the loop h
how would you iterate through an array, incrementing a variable in the global scope, when the loop h

Time:11-14

I have a variable set to a string of about 30 characters. I used .split() to turn the string into an array. Now the part I am struggling with is looping through the array I just made, incrementing a counter I have set to a variable, and the variable incrementing anything the array iterates past the last 5 numbers in the array?

Thanks in advance to anyone that can help!

I tried this inside my function...

let numsArr = [0, 1, 2, 3, ...... ,30]
let numOfInvalidElement = 0;


function() {
  for(let i = 0; i < numsArr.length; i  ) {
    if (numsArr >= 20) {
       return numofInvalidElement  
    }
   }
  }

CodePudding user response:

You need to keep a check to know when the loop is approaching the last 5 values in the array. And also when you return from a for loop inside a function it ends the function execution.

let numsArr = [0, 1, 2, 3, ...... ,30]
let numOfInvalidElement = 0;
let isLastFiveNumbers = numsArr.length 


function() {
  const numsArrLength = numsArr.length
  for(let i = 0; i < numsArrLength; i  ) {
    if (numsArrLength - i <= 5) {
       numOfInvalidElement  
    }
   }
  }

CodePudding user response:

1st solution:

const numsArr = []
// create the numsArr array with a for loop.
for (let i=0; i<30; i  ) numsArr.push(i   1);

// us a for...of loop to iterate numsArr
let numOfInvalidElement = 0;
for(const num of numsArr) {
  if (num >= 20) numOfInvalidElement  
 }

// log the result
console.log(numOfInvalidElement)

// expected output: 11

some other solutions:

const numsArr = []
// create the numsArr array with a for loop.
for (let i=0; i<30; i  ) numsArr.push(i   1);


// use a for loop to iterate numsArr:
let result1 = 0;
for (let i=0; i<numsArr.length; i  ) if (numsArr[i] >= 20) result1  ;

console.log(`for loop result: ${result1}`);


// use .forEach method to itrate numsArr:
let result2 = 0;
numsArr.forEach(num => (num >= 20) && result2  )

console.log(`.forEach method result: ${result2}`)


// use .filter method to itrate numsArr:
const result3 = numsArr.filter(num => num >= 20).length;

console.log(`.filter method result: ${result3}`)

// using .filter method with {} to itrate numsArr:
// when using array methods like .filter, .map, .some, etc. with {}, you do need to return a value.
const result4 = numsArr.filter(num => {
    return num >= 20
}).length;

console.log(`.filter method result: ${result4}`)

CodePudding user response:

Your function is terminating early because of of the return statement. As the docs note:

The return statement ends function execution and specifies a value to be returned to the function caller.

To your requirement:

incrementing a counter I have set to a variable, and the variable incrementing anything the array iterates past the last 5 numbers in the array?

Not entirely sure what this means, nor what your condition numsArr >= 20 aims to achieve since numsArr is an array. Sounds like you want to only look at the last 5 elements and to only do something if the value exceeds a value e.g. 20:

for(let i = 0; i < numsArr.length; i  ) {
  // Only check last 5 and increment is exceed threshold value
  if (i >= numsArray.length - 5 && numArr[i] >= 20) {
    numofInvalidElement  ; // Note the removed return
  }
}

If that's what you want, then a functional approach may be more concise:

numOfInvalidElements = numsArr.slice(-5).filter(x => x >= 20).length;

See documentation on slice and filter.

  • Related