Javascript String toAlternatingCase()

Description

Javascript String toAlternatingCase()


String.prototype.toAlternatingCase = function() {
  return [...this].map(
    (c) => (c === c.toUpperCase() ? c.toLowerCase() : c.toUpperCase()),
  ).join``;//w  w  w.j  a v  a2s .  c  o  m
};

Javascript String toAlternatingCase()

String.prototype.toAlternatingCase = function () {
  return this.split("").map(function(a){
  return From:git source
===a.toUpperCase() ? a.toLowerCase():a.toUpperCase()}).join('')
}

Javascript String toAlternatingCase()

String.prototype.toAlternatingCase = function () {
  var _str = ""; 
  for(var _c = 0; _c < this.length; _c++){
    _str += ((this[_c] === this[_c].toUpperCase())? this[_c].toLowerCase() : this[_c].toUpperCase());
  }//from   w  ww  .  j a v a  2 s . c o  m
  return _str;
}

Javascript String toAlternatingCase()

String.prototype.toAlternatingCase = function () {
  var newStr = this.split('');
  var result = '';
  newStr.forEach(function(el) {
    if(el === el.toUpperCase()) {
      result += el.toLowerCase();/*from  w  w  w.j a v a2  s . c  o m*/
    } else {
      result += el.toUpperCase();
    }
  });
  return result;
};

console.log('hElLo'.toAlternatingCase()); // 'HeLlO'

Javascript String toAlternatingCase()

String.prototype.toAlternatingCase = function () {
  return this.toString().split('')
                        .map(l => l.match(/[a-z]/)  ? l.toUpperCase() : l.toLowerCase())
                        .join('');
}
//without toString possible

Javascript String toAlternatingCase()

// https://www.codewars.com/kata/alternating-case-%3C-equals-%3E-alternating-case/train/javascript
String.prototype.toAlternatingCase = function () {
    return this.split('').map(function (char) {
        return char === char.toUpperCase()
            ? char.toLowerCase()/*from  w  w  w.j  a v a2  s  .  com*/
            : char.toUpperCase();
    }).join('');
}

Javascript String toAlternatingCase()

String.prototype.toAlternatingCase = function () {
  arr = [];/*www.  ja  v a  2  s .  c om*/
  for (let  i = 0 ; i<(this.length);i++) {
    let char2 = this[i];
    if (char2 == char2.toUpperCase()) 
      char2 = char2.toLowerCase();
     else if (char2 == char2.toLowerCase()) 
      char2 = char2.toUpperCase(); 
   arr.push(char2);
  }
  return arr.join("");
}

Javascript String toAlternatingCase()

String.prototype.toAlternatingCase = function () {
    var str = this, newStr="";

    for (var i=0; i<str.length; i++) {
        var char = str.charCodeAt(i);
    /*from   ww  w  .j  a va 2  s. c  o  m*/
        if ( char<91 && char>64) {
              newStr += String.fromCharCode(char+32);
          } else if 
           ( char>96 && char<123) {
              newStr += String.fromCharCode(char-32);
          } else {
          newStr += str[i];
          }
       }
    return newStr;
 }

Javascript String toAlternatingCase()

String.prototype.toAlternatingCase = function () {
    var a = new Array();
    for (i = 0; i < this.length; i++) {
        if (this[i].charCodeAt() >= 97 && this[i].charCodeAt() <= 122) {
            a[i] = this[i].toUpperCase();
        } else if (this[i].charCodeAt() >= 65 && this[i].charCodeAt() <= 90) {
            a[i] = this[i].toLowerCase();
        } else {// w  ww  .j a v  a  2s.c om
            a[i] = this[i];
        }
    }
    return a.toString().replace(/,/g, "").trim();
};
console.log('Demo User'.toAlternatingCase());

Javascript String toAlternatingCase()

String.prototype.toAlternatingCase = function() {
  var strArr = this.split('');
  for (var i = 0; i < strArr.length; i++) {

    if (strArr[i] === strArr[i].toUpperCase()) {
      strArr[i] = strArr[i].toLowerCase();
    }/* w  w w . j a  v a2 s . c  om*/
    else {
      strArr[i] = strArr[i].toUpperCase();
    }
  }
    return strArr.join('');
}


toAlternatingCase("Hello");

Javascript String toAlternatingCase()

// Define String.prototype.toAlternatingCase (StringUtils.toAlternatingCase(String) 
//  in Java) such that each lowercase letter becomes uppercase and each uppercase letter 
// becomes lowercase. 

String.prototype.toAlternatingCase = function () {
  var result = [];
  var letters = this.split("");
  letters.forEach(function(char) {
    if ((char.charCodeAt(0) >= 97) && (char.charCodeAt(0) <= 122)) {
      result.push(char.toUpperCase());//from w ww .j  ava2s . com
    } else if ((char.charCodeAt(0) >= 65) && (char.charCodeAt(0) <= 90)){
      result.push(char.toLowerCase());
    } else {
      result.push(char);
    }
  });
  return result.join("");
}

Javascript String toAlternatingCase()

String.prototype.toAlternatingCase = function () {
  // Define your method here :)
  var newStr = "";
  for (var i = 0; i < this.length; i++){
    if (this.charAt(i).toUpperCase() === this.charAt(i)){
       newStr += this.charAt(i).toLowerCase();
    } else {//w w w.ja  v  a 2s  . co  m
       newStr += this.charAt(i).toUpperCase();  
    }
  }
  return newStr;
}



PreviousNext

Related