-2
//1. input words beginning and ending with a vowel are preserved in lowercase
//2. input words beginning with vowels and ending in a non vowel are translated to uppercase
//3. input words beginning with a non vowel and ending with a vowel are translated to a capitalized word (only first letter is uppercase)
//4. when printing the words, only two vowels are preserved per word (the first two)

//Output "i dont know Dude i AM not typing ALL OF this Nonsens text"
var str = "i dont know dude i am not typing all of this nonsense text";
console.log(str);
var res = str.split(" ");
console.log(res);

for(let i = 0; i<res.length;i++){
    //checking words after spliting into single words

    if((res[i].split("") && (res[0] ==== 'a'|| 'e' || 'o' || 'u' || 'i' || 'y') && (/* last charachter to check */ ))
}

I am beginner JavaScript Developer and i am having some difficulties with my exercise i have above 4 conditions first i split the array into a words then i hoped to split into single characters so res[0] will be my first item . I dont know if this will work but at least i need to try. Any help will be very appreciated even if it is with regular expressions. Thank you

  • Possible duplicate of [Convert string to title case with JavaScript](https://stackoverflow.com/questions/196972/convert-string-to-title-case-with-javascript) – Tim Grant Mar 28 '18 at 17:40
  • According to 4th condition "nonsense" will be converted to "nonse", right? – yajiv Mar 28 '18 at 18:01

3 Answers3

1

You can use reduce of Array.prototype.

var str = "i dont know dude i am not typing all of this nonsense text";
console.log(str);
var res = str.split(" ");

var y=res.reduce((a,e) => {
    if("aeiou".includes(e[0])){  //condition 1 and 2
        return a + ("aeiou".includes(e[e.length-1]) ? getWord(e).toLowerCase() : getWord(e).toUpperCase()) + " ";
    }
    else{                       //condition 3
        return a + ("aeiou".includes(e[e.length-1]) ? e[0].toUpperCase() + getWord(e.substr(1)).toLowerCase() : e) + " ";
    }
},"");

function getWord(x){            //condition 4
    var count = 0;
    for(var i = 0; i < x.length; i++){
        count += "aeiou".includes(x[i]) ? 1 : 0;
        if(count === 3)
            return x.substr(0,i);
    }
    return x;
}

console.log(y);
yajiv
  • 2,901
  • 2
  • 15
  • 25
0
 res[0] ==== 'a'|| 'e'

That wont work. You need to do:

 res[0] ==== 'a'|| res[0] === 'e'

However as that is quite complicated, might just do:

 "aeiou".includes(res[0])

Oh and res[0] is the first word, not the first char, that would be res[i][0]. And you can get the last one with res[i][res[i].length - 1]. And i still dont get what you are trying to do with res[i].split("") && ... just leave that away.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

Here is a quick function that does fulfills 1 to 3. I'm not sure what you mean in 4.

Snippet:

/* Example. */
var str = "i dont knoooow dudeeeee i am not typing all of this nonsense text";
console.log(str);
console.log(process(str));

/* --------------------------------------------- */

/* The function that checks whether a char is a vowel. */
function isVowel (char) {
  return "aeiou".includes(char);
}

/* The function that truncates a word before the 3rd vowel. */
function truncate (word) {
  /* Create an index and a counter and save the length of the word. */
  var i = 0, c = 0, l = word.length;

  /* Iterate over every char until 3 vowels are found, if found. */
  for (; i < l && c < 3; i++, c += +isVowel(word[i]));

  /* Return the part of the string until before the 3rd vowel. */
  return word.slice(0, i);
};

/* The function that processes the string. */
function process (str) {
  /* Split the sentence into an array of words. */
  var words = str.split(" ");
  
  /* Iterate over every word. */
  words.forEach(function (word, index) {
    /* Strip any more than 2 repeated vowels and anything beyond the 3rd vowel. */
    word = truncate(word.replace(/([aeiou])\1{2,}/i, "$1$1"));        // 4
    
    /* Check whether the first character is a vowel or not. */
    words[index] = isVowel(word[0])
      /* Check whether the last character is a vowel or not. */
      ? isVowel(word[word.length - 1])
        ? word.toLowerCase()                                          // 1
        : word.toUpperCase()                                          // 2
      /* Check whether the last character is a vowel or not. */
      : isVowel(word[word.length - 1])
        /* Capitalise the first char and turn the rest to lowercase. */
        ? word[0].toUpperCase() + word.slice(1).toLowerCase()         // 3
        : word;
  });
  
  /* Join the array into a string and return it. */
  return words.join(" ");
}
Angel Politis
  • 10,955
  • 14
  • 48
  • 66
  • in the forth case i need input "eeeeee teeeeeee" = "ee Tee" output Thank you very much –  Mar 28 '18 at 17:50
  • So essentially, when the same vowel is repeated many times, you want to keep a maximum of two occurrences. Is that correct @EmirMustafoski? – Angel Politis Mar 28 '18 at 17:54
  • Yes @Angel Politis –  Mar 28 '18 at 17:57
  • Check out my update @EmirMustafoski. I hope you don't bother using a small regular expression to do that. It would be fairly complicated otherwise. – Angel Politis Mar 28 '18 at 18:04
  • It does not actually show correct the first test ( i mean the string) This guy above with reduce have all the conditions. Btw before that it worked for first 3 conditions - @Angel Politis –  Mar 28 '18 at 18:10
  • It still works for the first 3 conditions. Regarding condition 4, I asked you whether you want to keep a maximum of two occurrences of a repeated vowel and you said 'yes' @EmirMustafoski. – Angel Politis Mar 28 '18 at 18:13
  • Yes but its does not do that. –  Mar 28 '18 at 18:14
  • @AngelPolitis "nonsense" would be "nonsens"(which is not converted in your code) – yajiv Mar 28 '18 at 18:25
  • So what you want is two vowels per word regardless of being consecutive or not. Right @EmirMustafoski? – Angel Politis Mar 28 '18 at 18:26
  • Yes -@AngelPolitis –  Mar 28 '18 at 18:33
  • Okay @EmirMustafoski. I added that functionality as well. – Angel Politis Mar 28 '18 at 18:43