Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
indexOf() skipped repeated element javascript

I am trying to print the values of odd positions in the array below but it skipped 7, what could be wrong, please?

const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];

const validateCred = (serialDigits)=>{
  //let sum = 0;
  for (let i = serialDigits.length - 1; i >= 0; i--){
    let currentDigit = serialDigits[i];
    
    if( serialDigits.indexOf(currentDigit) % 2 === 1){
      console.log(currentDigit)
        };
    }
}

console.log(validateCred(valid1));
// 0 6 0 0 6 3 4
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This:

if( serialDigits.indexOf(currentDigit) % 2 === 1){

Is not a good line because indexOf find the first occurrence in the array, therefore, do not fulfil your need. just use the variable i instead:

if (i % 2 === 1) {

Similarly, if you want to print the even indexes, you can use this code:

const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];

const validateCred = (serialDigits) => {
  //let sum = 0;
  for (let i = serialDigits.length - 1; i >= 0; i--) {
    let currentDigit = serialDigits[i];

    if (i % 2 === 0) {
      console.log(currentDigit);
    };
  }
}

validateCred(valid1);
// 0 6 0 0 7 6 3 4

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!