2

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;
}
6
  • 2
    you're missing a closing parenthesis line 5 Commented Aug 5, 2017 at 20:38
  • 2
    Why can you not use .toLowerCase()? Commented Aug 5, 2017 at 20:39
  • I'm pretty sure char is a reserved word in JS that you are not allowed to use. If char is an array (which it looks like), it should be chars. Commented Aug 5, 2017 at 20:40
  • 1
    Also charCodeAt(97-122) becomes charCodeAt(-25), which makes no sense because String.prototype.charCodeAt() (1) must be called on a string and (2) takes an index, not a code point. Commented Aug 5, 2017 at 20:45
  • 1
    more importantly I believe charCodeAt is a method, not a function! You might want to do char[i].charCodeAt(0) instead of charCodeAt(char[i]) Commented Aug 5, 2017 at 20:48

5 Answers 5

3

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&$*(@&(*&*#@!"
    
Sign up to request clarification or add additional context in comments.

2 Comments

very nice explanation
Thank you for explaining step by step, this really helped me understand a lot better.
1

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'));

Comments

1

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)

Comments

0

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

:%s/.*/\L&/

Comments

0

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);
}
}

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.