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

213
Views
Finding specific string is odd or even from an array JavaScript

I am trying to find an odd string from an given array.

Code :

const friendArray = ["agdum", "bagdum", "chagdum", "lagdum", "jagdum", "magdum"];

function oddFriend(friendArray) {
  for (let i = 0; i < friendArray.length; i++) {
    if (friendArray[i].length % 2 != 0) {
      return friendArray[i];
    }
  }
}
const myFriend = oddFriend(friendArray);
console.log(myFriend);

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can apply this:

const friendArray = ["agdum","bagdum","chagdum","lagdum","jagdum","magdum",];

  function oddFriend(friendArray) {
    if (friendArray.length % 2 != 0) {
      return friendArray;
    }
  }
  const myFriend = friendArray.filter(oddFriend);
  console.log(myFriend);
about 4 years ago · Juan Pablo Isaza Report

0

.flatMap() and a ternary callback makes a simple and readable filter. It's not entirely clear if you wanted only odd or odd and even so there's both versions.

Odd length strings

const fArray = ["agdum", "bagdum", "chagdum", "lagdum", "jagdum", "magdum"];

let odd = fArray.flatMap(o => o.length % 2 === 1 ? [o] : []);

console.log(odd);

Odd & even length strings

const fArray = ["agdum", "bagdum", "chagdum", "lagdum", "jagdum", "magdum"];

let oe = [[], []];

fArray.forEach(o => o.length % 2 === 1 ? oe[0].push(o) : oe[1].push(o));

console.log(oe);

about 4 years ago · Juan Pablo Isaza Report

0

You can use Array.filter() method which will create a new array with all odd friends that pass the test implemented by the provided function.

// Input array
const friendArray = ["agdum", "bagdum", "chagdum", "lagdum", "jagdum", "magdum"];

// Filtered result
console.log(friendArray.filter((friend) => friend.length % 2 !== 0));

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!