Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capitalize the last letter of each word in a string

I am still rather new to JavaScript and I am having an issue of getting the first character of the string inside the array to become uppercase.

I have gotten to a point where I have gotten all the texted lowercase, reversed the text character by character, and made it into a string. I need to get the first letter in the string to uppercase now.

function yay () {
  var input = "Party like its 2015";

  return input.toLowerCase().split("").reverse().join("").split(" ");
  for(var i = 1 ; i < input.length ; i++){
    input[i] = input[i].charAt(0).toUpperCase() + input[i].substr(1);
  }   
}

console.log(yay());

I need the output to be "partY likE itS 2015"

like image 733
vpope Avatar asked Dec 25 '22 15:12

vpope


2 Answers

Frustrating that you posted your initial question without disclosing the desired result. Lots of turmoil because of that. Now, that the desired result is finally clear - here's an answer.

You can lowercase the whole thing, then split into words, rebuild each word in the array by uppercasing the last character in the word, then rejoin the array:

function endCaseWords(input) {
    return input.toLowerCase().split(" ").map(function(item) {
        return item.slice(0, -1) + item.slice(-1).toUpperCase();
    }).join(" ");
}

document.write(endCaseWords("Party like its 2015"));

Here's a step by step explanation:

  1. Lowercase the whole string
  2. Use .split(" ") to split into an array of words
  3. Use .map() to iterate the array
  4. For each word, create a new word that is the first part of the word added to an uppercased version of the last character in the word
  5. .join(" ") back together into a single string
  6. Return the result

You could also use a regex replace with a custom callback:

function endCaseWords(input) {
    return input.toLowerCase().replace(/.\b/g, function(match) {
      return match.toUpperCase();
    });
}

document.write(endCaseWords("Party like its 2015"));

FYI, there are lots of things wrong with your original code. The biggest mistake is that as soon as you return in a function, no other code in that function is executed so your for loop was never executed.

Then, there's really no reason to need to reverse() the characters because once you split into words, you can just access the last character in each word.

like image 163
jfriend00 Avatar answered Mar 15 '23 08:03

jfriend00


Instead of returning the result splitting and reversing the string, you need to assign it to input. Otherwise, you return from the function before doing the loop that capitalizes the words. Then after the for loop you should return the joined string.

Also, since you've reverse the string before you capitalize, you should be capitalizing the last letter of each word. Then you need to reverse the array before re-joining it, to get the words back in the original order.

function yay () {
  var input = "Party like its 2015";

  input = input.toLowerCase().split("").reverse().join("").split(" ");
  for(var i = 1 ; i < input.length ; i++){
    var len = input[i].length-1;
    input[i] = input[i].substring(0, len) + input[i].substr(len).toUpperCase();
  }
  return input.reverse().join(" ");
}

alert(yay());
like image 33
Barmar Avatar answered Mar 15 '23 10:03

Barmar