JSFiddle - React, Tailwind, and code Playground

by Twiggeh99

JavaScript

const strFlipCase = function(){
	// create a temporary variable to then return after the loop.
	const result = [];
  // get an array with each letter
	const strArr = this.split('');
	// loop over the newly created array
  for(let character of strArr){
  	// check whether the character is uppercase
  	if(character.toUpperCase() === character){
			// character is uppercase so push the lowercase character
			// into the temporary array
    	result.push(character.toLowerCase())
    } else {
      // character is lowercase so push the uppercase character
			// into the temporary array
      result.push(character.toUpperCase())
    }
  }
	// temporary array has been filled, return the temporary variable
  // as a string
  return result.join('')
}

String.prototype.strFlipCase = strFlipCase;

const myStr = "Hello";
// prints "hELLO"
console.log(myStr.strFlipCase());