'Suppose You have an array that contains some strings as name of something. Now,you have to find one string that contains odd letter by java script
const array=['abul','babu','rony','babul','suny'];
function{enter code here}
Solution 1:[1]
I think you would want to use the remainder operator to determine if the string length is odd and Array.prototype.find() to find the value in the list
The remainder operator (%) returns the remainder left over when one operand is divided by a second operand.
The find() method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.
const names = ['abul','babu','rony','babul','suny']
// function that takes a string and returns true if the string has odd length
// determines whether a string's length is odd using modulo operator and checking remainder
const hasOddLength = str => str.length % 2 !== 0
// uses hasOddLength as callback for find()
const findStringWithOddLength = (list) => (
list.find(str => hasOddLength(str))
)
console.log(findStringWithOddLength(names)) // "babul"
Note: if there are multiple names with an odd length, find() will only return the first one.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ItsGeorge |
