Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

89
Views
this code will return true if item does exist in array but it should be true when item was duplicated

const arr = [12, 12, 10, 11];
function repeatedItem(i) {
  if (arr.indexOf(i) !== -1) return true;
  else return false
}
console.log(repeatedItem(10));
this code will return true if item does exist in array but it should be true when item was duplicated

what should do to return true if item was duplicated

7 months ago · Santiago Trujillo
1 answers
Answer question

0

const arr = [12, 12, 10, 11];

function repeatedItem(item) {
    let count = 0;

    for(let i=0;i<arr.length;i++){
        if(arr[i] === item){
            count++;
            if(count > 1){
                return true;
            }
        }
    }

    return false;
}
console.log(repeatedItem(10));

7 months ago · Santiago Trujillo Report
Answer question
Find remote jobs