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

36
Views
I need to check if first number of array equals to the last

I have an array

    const array = [[1, 2], [3, 3]]; arr.forEach(el => {  
      if (el[0] === el[1]) {return true}
    })
7 months ago · Juan Pablo Isaza
3 answers
Answer question

0

There is no point in return true in a forEach

I assume you mean this:

const testArr = arr => arr[0] === arr[arr.length-1]; // test first and last

const array = [[1, 2], [3, 3]]; 

const testResults = array.map(el => testArr(el)); // map the results
console.log(testResults)

// filter: 

const same = array.filter(el => testArr(el)); // FIND the array that has same first and last 

console.log(same.flat())

7 months ago · Juan Pablo Isaza Report

0

You should should every function

const arr = [[1, 2], [3, 3]]; 
const b = arr.every(el => el[0] === el[1]);
console.log(b); // false
7 months ago · Juan Pablo Isaza Report

0

This should get the desired result. Just change console.log(true) to whatever you would like to do if they equal each other. This will work on any length of array.

const array = [
  [1, 2],
  [3, 3],
];

array.forEach((el) => {
  if (el[0] === el[el.length - 1]) {
    console.log(true);
  }
});
7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs