javascript - Returning a string with only vowels capitalized - Stack Overflow

I'd like to return the variable newString with only vowels capitalized.Not sure how to proceed.

I'd like to return the variable newString with only vowels capitalized. Not sure how to proceed. Tried using an if/else block but my logic wasn't correct.

function LetterChanges(str) {
var newArray = [];
for (var i = 0; i < str.length; i++) {
    var strCode = str.charCodeAt(i) + 1;
    var strLetter = String.fromCharCode(strCode);
    newArray.push(strLetter);
    var newString = newArray.join("");
    }
  return newString;
}

LetterChanges("hello");

I'd like to return the variable newString with only vowels capitalized. Not sure how to proceed. Tried using an if/else block but my logic wasn't correct.

function LetterChanges(str) {
var newArray = [];
for (var i = 0; i < str.length; i++) {
    var strCode = str.charCodeAt(i) + 1;
    var strLetter = String.fromCharCode(strCode);
    newArray.push(strLetter);
    var newString = newArray.join("");
    }
  return newString;
}

LetterChanges("hello");
Share Improve this question asked Jan 19, 2015 at 23:34 user4471957user4471957
Add a ment  | 

4 Answers 4

Reset to default 8

This is different from your approach, but you can do this:

function LetterChanges(str) {
  return str.toLowerCase().replace(/[aeiou]/g, function(l) {
    return l.toUpperCase();
  });
}

console.log(LetterChanges("The Quick Brown Fox Jumped Over The Lazy Dog"));


Here's an approach that's closer to your attempt and uses somewhat simpler concepts:

function LetterChanges(str) {
    var newArray = [];
    for (var i = 0; i < str.length; i++) {
        var ch = str.charAt(i);
        if ('aeiouAEIOU'.indexOf(ch) !== -1) {
           newArray.push(ch.toUpperCase());
        } else {
           newArray.push(ch.toLowerCase());
        }
    }

    return newArray.join("");
}

console.log(LetterChanges("The Quick Brown Fox Jumped Over The Lazy Dog"));

Split, map, join.

var vowels = 'aeiou';
var text = 'my random text with inevitable vowels';

var res = text.split('').map(function(c){
    return (vowels.indexOf(c) > -1) ? c.toUpperCase() : c;
});

See the fiddle: http://jsfiddle/zo6j89wv/1/

To do this without regex, you can set the string to lower case, then iterate once over, calling toUpperCase() on each vowel.

function letterChanges(string){
  var vowels = 'aeiou';
  var lowerString = string.toLowerCase();
  var result = '';
  for( var i=0; i<lowerString.length; i++){
    if( vowels.indexOf( lowerString[i] ) >= 0 ){ //if lowerString[i] is a vowel
      result += lowerString[i].toUpperCase();
    } else {
      result += lowerString[i]
    }
  }
  return result;
}
const vowelSound = string => {
    let res = string.split("").filter(item => item === 'a' || item === 'i' || item === 'e' || item === 'o' || item === 'u')
    return res.join("")
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742280180a4414342.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信