PurplePanda
PurplePanda

Reputation: 33

Convert uppercase string to lowercase string in JS manually

Trying to write a function which returns the calling string value converted to lowercase. The trick is I can't use toLocaleLowerCase().

Below is what I have so far.

function charChange(char){
    for (var i=0; i<char.length; i++){
        var char2=charCodeAt(char[i])+32;
        var char3=String.fromCharCode(char2);
        if (char3 !== charCodeAt(97-122){
            alert("Please enter only letters in the function")
        }
    }
    return char;
}

Upvotes: 2

Views: 4277

Answers (5)

Dileep yadav
Dileep yadav

Reputation: 1

const lowerCase = ()=>{

    let welcome = prompt("Enter your string");
let lower = '';
for(let i=0; i<welcome.length;i++){
    code =  welcome.charCodeAt(i);

   if(code >= 65 && code <= 90){
    let co = code+32;
    console.log(co);
    lower += String.fromCharCode(co);
   }
   else
   {
    lower +=String.fromCharCode(code) ;
   }
   console.log(lower);
}
}

Upvotes: 0

MultiplyByZer0
MultiplyByZer0

Reputation: 7129

To convert the uppercase letters of a string to lowercase manually (if you're not doing it manually, you should just use String.prototype.toLowerCase()), you must:

  1. Write the boilerplate function stuff:

    function strLowerCase(str) {
        let newStr = "";
        // TODO
        return newStr;
    }
    
  2. Loop over each character of the original string, and get its code point:

    function strLowerCase(str) {
        let newStr = "";
        for(let i = 0; i < str.length; i++) {
            let code = str.charCodeAt(i);
            // TODO
        } return newStr;
    }
    
  3. Check if the character is an uppercase letter. In ASCII, characters with code points between 65 and 90 (inclusive) are uppercase letters.

    function strLowerCase(str) {
        let newStr = "";
        for(let i = 0; i < str.length; i++) {
            let code = str.charCodeAt(i);
            if(code >= 65 && code <= 90) {
                // TODO
            } // TODO
        } return newStr;
    }
    
  4. If the character is uppercase, add 32 to its code point to make it lowercase (yes, this was a deliberate design decision by the creators of ASCII). Regardless, append the new character to the new string.

    function strLowerCase(str) {
        let newStr = "";
        for(let i = 0; i < str.length; i++) {
            let code = str.charCodeAt(i);
            if(code >= 65 && code <= 90) {
                code += 32;
            } newStr += String.fromCharCode(code);
        } return newStr;
    }
    
  5. Test your new function:

    strLowerCase("AAAAAAABBBBBBBCCCCCZZZZZZZZZaaaaaaaaaaa&$*(@&(*&*#@!");
    // "aaaaaaabbbbbbbccccczzzzzzzzzaaaaaaaaaaa&$*(@&(*&*#@!"
    

Upvotes: 3

SVSchmidt
SVSchmidt

Reputation: 6567

These are the two most elegant solutions I can think of (at the moment). See comments within the code and don't hesitate to ask if anything is unclear.

function charChange1 (str) {
  let result = '';
  const len = str.length;
  
  // go over each char in input string...
  for (let i = 0; i < len; i++) {
    const c = str[i];
    const charCode = c.charCodeAt(0);

    if (charCode < 65 || charCode > 90) {
      // if it is not a uppercase letter,
      // just append it to the output
      // (also catches special characters and numbers)
      result += c;
    } else {
      // else, transform to lowercase first
      result += String.fromCharCode(charCode - 65 + 97);
    }
  }

  return result;
}

function charChange2 (str) {
    // Array.prototype.slice.call(str) converts
    // an array-like (the string) into an actual array
    // then, map each entry using the same logic as above
    return Array.prototype.slice.call(str)
      .map(function (c) {
        const charCode = c.charCodeAt(0);

        if (charCode < 65 || charCode > 90) return c;

        return String.fromCharCode(charCode - 65 + 97);
      })
      // finally, join the array to a string
      .join('');
}

console.log(charChange1("AAAAsfasSGSGSGSG'jlj89345"));
console.log(charChange2("AAAAsfasSGSGSGSG'jlj89345"));


(On a side node, it would of course be possible to replace the magic numbers by constants declared as calls to 'A'.charCodeAt(0))

(A second side node: Don't use char since it is a reserved word in JavaScript; I prefer c)

Upvotes: 1

Ivan
Ivan

Reputation: 40768

charCodeAt() is a method called on a String, it's not a function. So you apply the method on a string and give the position of the character you want to convert as the parameter. Also as MultiplyByZer0 mentioned the word char is reserved: look at the list of reserved words.

The following code fixes the problem:

function charChange(str) {
  var result = "";

  for (var i = 0; i < str.length; i++) {

    var code = str[i].charCodeAt(0);
    
    if(code >= 65 && code <= 90) {
    
      var letter = String.fromCharCode(code+32);
    
      result += letter // append the modified character

    
    } else {

      result += str[i]  // append the original character
      
    }
    
  }

   return result;
}

console.log(charChange('j#aMIE'));

Upvotes: 1

Graham
Graham

Reputation: 7802

You can use regex to convert the string to lower case:

:%s/.*/\L&/

Upvotes: 0

Related Questions