简体   繁体   中英

How does ternary operator work in the function?

Can you explain to me the return line in this algorithm?

The function is supposed to take a string and return it's Pig Latin version which is that it takes the first consonant or consonant cluster and puts it to the end of the string adding "ay" at the end.

If the string starts with a vowel it's supposed to just add "way" at the end.

function translatePigLatin(str) {
  function check(obj) {
      return ['a','i','u','e','o'].indexOf(str.charAt(obj)) == -1 ? check(obj + 1) : obj;
  }

  return str.substr(check(0)).concat((check(0) === 0 ? 'w' : str.substr(0, check(0))) + 'ay');
}

// test here
translatePigLatin("consonant"); // should return "onsonantcay"

Thats just hard to grasp because the names are horrible. obj is actually a number used to go to a position in the string, so it should rather be named pos or something. check doesnt check anything, it just traverses forward until the first vowel was found so it should be:

 const firstVowel = (pos = 0) => "aeiou".includes(str.charAt(pos)) ? pos : firstVowel(pos + 1);

Now the last line just takes the part from the first vowel (removes the consonants at the beginning):

 str.substr(/*from*/ firstVowel() /*till end*/)

If the first vowel is directly at the beginning:

 firstVowel() === 0

it just appends

 "way"

otherwise it takes those consonants at the beginning:

 str.substr(0, /*to*/ firstVowel())

and appends an "y".

Hope the below code explains it with the comments.

 function translatePigLatin(str) { function check(obj) { return ['a','i','u','e','o'].indexOf(str.charAt(obj)) == -1 ? check(obj + 1) : obj; } //return str.substr(check(0)).concat((check(0) === 0 ? 'w' : str.substr(0, check(0))) + 'ay'); // The above is explained as equivalent to below: if(check(0) === 0){ return str.substr(check(0)).concat('w'+'ay') } return str.substr(check(0)).concat(str.substr(0, check(0))+'ay') } // test here console.log(translatePigLatin("consonant")); // should return "onsonantcay" 

It is usually helpful to break ternary statements down into if/else blocks in cases where you are unsure what they do.

 function translatePigLatin(str) { function check(index) { // return ['a','i','u','e','o'].indexOf(str.charAt(obj)) == -1 ? check(obj + 1) : obj; // break down ternary into traditional if also changed obj to index to be more descriptive const vowels = ['a','i','u','e','o']; // if the character at the given index exists then check the next character if (vowels.indexOf(str.charAt(index)) === -1) { return check(index + 1) } // otherwide return index (vowel case) return index; } // return str.substr(check(0)).concat((check(0) === 0 ? 'w' : str.substr(0, check(0))) + 'ay'); // set base translated word as the first letter in word that is not a vowel. const indexKey = check(0) // get string after index key let substringed = str.substr(indexKey) // get string from beginning until indexkey let appended = str.substr(0, indexKey); // if the index key is the first letter (word starts with vowel) use 'w' if (indexKey === 0) { appended = 'w'; } // return string return `${substringed}${appended}ay`; } // test here const singleConsonant = translatePigLatin("constant"); const doubleConsonant = translatePigLatin("chain"); const vowel = translatePigLatin("analyze") console.log(singleConsonant, doubleConsonant, vowel); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM