Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
Capitalizing vowels without replace/regex/includes

I've been trying to make that thing work but the output is probably not too far from the expected result - yet still ,not the correct output.

flipCase();
function flipCase() {
  var string = prompt("Please enter a string:");
  var vowels = "aeiou";
  var newString = "";

  for (var i = 0; i < string.length; i++) {
    for (var j = 0; j < vowels.length; j++) {
      if (string.charAt(i) === vowels.charAt(j)) {
        newString += string.charAt(i).toUpperCase();
      } else {
        newString += string.charAt(i).toLowerCase();
      }
    }
  }

  console.log(newString);
}

The input is : open.

The output is : oooOopppppeEeeennnnn.

The expected output is : OpEn.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

If you found a vowel, you need to leave the inner loop and omit adding the character to the result string.

function flipCase(string) {
    string = string || prompt("Please enter a string:");
    const vowels = "aeiou";
    let newString = "";

    for (let i = 0; i < string.length; i++) {
        let character = string[i].toLowerCase();
        for (let j = 0; j < vowels.length; j++) {
            if (character === vowels[j]) {
                character = character.toUpperCase();
                break;
            }
        }
        newString += character;
    }
    return newString;
}

console.log(flipCase('open'));

about 4 years ago · Juan Pablo Isaza Report

0

Check the index of each character in the string against against the vowels string using indexOf. This way there's no nested loop.

function flipCase(str) {

  const vowels = 'aeiou';

  let newString = '';

  // Iterate through the string - if the
  // (lowercase) character in the current iteration
  // is in the vowels string, make it uppercase, and
  // add it to `newString`, otherwise just add it
  for (var i = 0; i < str.length; i++) {
    const char = str[i].toLowerCase();
    if (vowels.indexOf(char) > -1) {
      newString += char.toUpperCase();
    } else {
      newString += char;
    }
  }

  return newString;

}

console.log(flipCase('open'));
console.log(flipCase('OPEN'));
console.log(flipCase('OPeN'));
console.log(flipCase('Gah! Homework :)'));

about 4 years ago · Juan Pablo Isaza Report

0

This looks homework-like enough that I'm not going to just give you the full answer, but I'll get you halfway there:

function flipCase() {
  var string = "here is some sample input"; // prompt doesn't work in SO snippets in some browsers
  var vowels = "aeiou";
  var newString = "";

  for (var i = 0; i < string.length; i++) {
    for (var j = 0; j < vowels.length; j++) {
      if (string.charAt(i) === vowels.charAt(j)) {
        newString += string.charAt(i).toUpperCase();
      }
      // the "else" can't go here, or it'll emit once for each
      // vowel whether it matches the current character or not.
    }
    // you need to output consonants outside the 'vowel' loop,
    // but this will also emit if the current character is a
    // vowel:
    newString += string.charAt(i).toLowerCase();
    // ...so you'll need to wrap the above in some other
    // condition to tell it when not to output. Possibilities
    // include checking specifically for not-a-vowel, or
    // setting a temporary variable whenever you output a vowel
    // that can be read here to prevent outputting it again
  }

  console.log(newString);
}

flipCase();

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!