Home > Software design >  I have to write a javascript function that takes in a string and returns true if "the string st
I have to write a javascript function that takes in a string and returns true if "the string st

Time:04-01

title basically. The question says create a function that takes in a string and returns true when the string starts and ends with a vowel. I must have read every possibly helpful/similar thread on here and still stuck. Please help

Tried too many things to even list. Its been literally hours I've been bashing my head against this problem.

CodePudding user response:

function test( word ){

  word = word.toUpperCase();
  
  let last = word[ word.length - 1 ];
    
  let vowels = [ 'A', 'E', 'I', 'O', 'U' ]

  if( vowels.includes( word[0]) && vowels.includes( last ) ){
    return true
  }else{
    return false
  }
}

CodePudding user response:

:-)

You know how to create and execute a function?

You understand what startsWith and endsWith with does?

You know the && (and) and || (or) operators?

Try something like this:

function startsEndsWithAVowel(aString) {
  return (aString.startsWith('a') || aString.startsWith('e') || aString.startsWith('i') || aString.startsWith('o') || aString.startsWith('u')) && (aString.endsWith('a') || aString.endsWith('e') || aString.endsWith('i') || aString.endsWith('o') || aString.endsWith('u'));
}

Add uppercase vowels too.

This solution uses only a few constructs. If You learn more JavaScript you come up much better solutions.

  • Related