No results found for the specified position. Return the string in lowercase. Javascript Beginner Level Strings Mock Interview

MockQuestions

Javascript Beginner Level Strings Mock Interview

Question 2 of 11 for our Javascript Beginner Level Strings Mock Interview

Get More Information About Our Javascript Beginner Level Strings Interview Questions

Question 2 of 11

Return the string in lowercase.

This interview question tests the developer's skills with JavaScript strings and loops.

You are given a string. The string contains the English alphabets. You need to implement a function toLowerCase which returns the same string in lowercase letters. You can not use the inbuilt function String.prototype.toLowerCase.

/*Example*/

given string- “abcAGiHkj”
expected output- “abcagihkj”

given string- “qwert”
expected output- “qwert”

Solution:

We will iterate over each character of the given string and convert it into lowercase. Then we will push the resulting character into a result array. After iterating the string, we will join the result array and return it. Now the problem remaining is to convert a character to lowercase.

The lowercase letters have character codes from 97 to 122 (both inclusive) and uppercase have from 65 to 90 (both inclusive). We will check the code of the letter. If it is between 97 and 122, we need not change the character. If it is between 65 and 90, we will add 32 (= 97 - 65) to convert to lowercase. So if-
the letter is “j”
-> its code is 106
-> between 97 and 122
-> already a lowercase letter
the letter is “B”
-> its code is 66
-> between 65 and 90
-> add 31
-> 66 + 32 = 98
-> 98 is “b”

/**
 * @param {string} sentence
 * @return {string}
 */
function toLowerCase(str) {
    const result = []

    for (const char of str) {
        let charCode = char.charCodeAt(0)

        // if it uppercase
        if (charCode >= 65 && charCode <= 90) {
            charCode += 32
        }

        const lowerChar = String.fromCharCode(charCode)
        result.push(lowerChar)
    }

    return result.join(“”)
}

Time complexity- O(n)
Space complexity- O(n)

Written by on May 21st, 2021

Next Question

How to Answer: Return the string in lowercase.

Advice and answer examples written specifically for a Javascript Beginner Level Strings job interview.

  • 2. Return the string in lowercase.

      This interview question tests the developer's skills with JavaScript strings and loops.

      You are given a string. The string contains the English alphabets. You need to implement a function toLowerCase which returns the same string in lowercase letters. You can not use the inbuilt function String.prototype.toLowerCase.

      /*Example*/
      
      given string- “abcAGiHkj”
      expected output- “abcagihkj”
      
      given string- “qwert”
      expected output- “qwert”

      Solution:

      We will iterate over each character of the given string and convert it into lowercase. Then we will push the resulting character into a result array. After iterating the string, we will join the result array and return it. Now the problem remaining is to convert a character to lowercase.

      The lowercase letters have character codes from 97 to 122 (both inclusive) and uppercase have from 65 to 90 (both inclusive). We will check the code of the letter. If it is between 97 and 122, we need not change the character. If it is between 65 and 90, we will add 32 (= 97 - 65) to convert to lowercase. So if-
      the letter is “j”
      -> its code is 106
      -> between 97 and 122
      -> already a lowercase letter
      the letter is “B”
      -> its code is 66
      -> between 65 and 90
      -> add 31
      -> 66 + 32 = 98
      -> 98 is “b”

      /**
       * @param {string} sentence
       * @return {string}
       */
      function toLowerCase(str) {
          const result = []
      
          for (const char of str) {
              let charCode = char.charCodeAt(0)
      
              // if it uppercase
              if (charCode >= 65 && charCode <= 90) {
                  charCode += 32
              }
      
              const lowerChar = String.fromCharCode(charCode)
              result.push(lowerChar)
          }
      
          return result.join(“”)
      }

      Time complexity- O(n)
      Space complexity- O(n)

      Written by S. Kumar on May 21st, 2021