-1

so like imagine I have an array like this

names = ["Alex", "David", "Hanna"]

so I want to get the length of the characters like Alex is 4 characters long and If I get an odd length I want to show that as an output. I can't figure out how to do it.

3 Answers3

2

You can use Array.filter method to filter the results


const names = ["Alex", "David", "Hanna"]
names.filter(name => name.length % 2 !== 0) // ['David', 'Hanna']

Santhosh
  • 71
  • 1
  • 4
0

Welcome Santonu to SO, you can do the following:

const names = ["Alex", "David", "Hanna"];

for (const name of names) {
  console.log(name.length);
}

This will iterate every name of the array and log you the length.

Jose A
  • 10,053
  • 11
  • 75
  • 108
0
const array = ["Alex", "David", "Hanna"]
array.forEach(item => {if(item.length%2 == 1)  console.log(item)});

See this for loop in javascript.

navylover
  • 12,383
  • 5
  • 28
  • 41