code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

709 To Lower Case

/**
 * 
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/* 
 * Implement function ToLowerCase() that has a string parameter str,
 * and returns the same string in lowercase.
 * 
 * Example 1:
 * Input: "Hello"
 * Output: "hello" 
 */

function toLowerCase(value) {
  let result = []
  for (let index = 0; index < value.length; index++) {
    if(value.charCodeAt(index) >= 65 && value.charCodeAt(index) < 90){
      result.push(String.fromCharCode(value.charCodeAt(index) + 32))
    } else {
      result.push(value[index])
    }    
  }
  return result.join('')
}

test('to Lower Case', () => {
  expect(toLowerCase('Ashish')).toEqual('ashish')
});

Created 2020-04-20T21:12:22+00:00 · Edit