PencilCrate
PencilCrate

Reputation: 211

Using .toUpperCase to check for upper-caseness

My program replaces word 'before' with word 'after' in a string. I think that I might be using .toUpperCase and .toLowerCase in the wrong way. Can I use .toUpperCase to check for upper-caseness, or can I only use it to assign upper-caseness? I have posted my short program. It is thoroughly commented with how I expect it to work, and then with the expected result and actual result.

// Params are string, word to be removed, new word to replace removed word.
function myReplace(str, before, after) {
  var afterCap;
  var newString;

  // Uppercase first letter of after, add rest of word.
  // afterCap is then capitalized after.
  afterCap = after[0].toUpperCase() + after.slice(1);

  // If before is capitalized,
  if (before[0].toUpperCase()) {
    // Replace with capitalized after.
    newString = str.replace(before, afterCap);
  }

  // If before not-capitalized,
  else if (before[0].toLowerCase()) {
    // Replace with lowercase after.
    newString = str.replace(before, after);
  }

  console.log(newString);
}

myReplace("Let us go to the store", "store", "mall");

// Should return "Let us go to the mall"
// Is in fact returning "Let us go to the Mall"

Why is the lower-case word 'store' being replaced with the upper case 'Mall'?

Upvotes: 2

Views: 264

Answers (2)

konkked
konkked

Reputation: 3231

toUppercase returns a string, not a boolean

Your code isn't actually checking if the first letter is upper case or not, just translating them to uppercase and checking if truthy. if the string is non-empty then the result will be truthy, and the first block will be executed, if it is empty the result will be an empty string, which is falsy, and nothing will be executed

Truthy values in javascript are all values except for false, 0, null, undefined and NaN see the MDN article here for more information

Change this

// If before is capitalized,
if (before[0].toUpperCase()) {
// Replace with capitalized after.
    newString = str.replace(before, afterCap);
}

to this

// If before is capitalized,
if (before[0].toUpperCase() === before[0]) {
// Replace with capitalized after.
    newString = str.replace(before, afterCap);
}

The second else-if statement can be translated to just else, turning your code into this:

// If before is capitalized,
if (before[0].toUpperCase() === before[0]) {
    // Replace with capitalized after.
    newString = str.replace(before, afterCap);
} else {
    // If before not-capitalized,
    // Replace with lowercase after.
    newString = str.replace(before, after);
}

you could reduce it even more by using the ternary conditional assignment operator

 newString = str.replace(before, 
                         before[0].toUppercase() === before[0] ? afterCap : after 
             );

Upvotes: 2

Ice Jovanoski
Ice Jovanoski

Reputation: 199

This is how you check if a given string (or char) is uppercase

function isUpperCase(str) {
    return str === str.toUpperCase();
}

Upvotes: 1

Related Questions