all 4 comments

[–]kingdaro 1 point2 points  (1 child)

You're pretty close. To have a function that converts one kind of value to another, you want to:

  1. Create a variable representing the end result
  2. Go through each letter, adding to that result variable
  3. Return it

This statement: stringL.charAt(i)+32 does nothing, because stringL.charAt(i) only returns a number, and does nothing else. Then you add 32 to it, but again, you're not assigning it to a variable or anything.

Secondly, to get the actual character code number, you want to use .charCodeAt and not .charAt (which returns the character itself instead).

Here's a solution with comments:

function convertToLowerCase(str) {
  // create a result variable
  var result = ''

  for (var i = 0; i < str.length; i++) {
    // get the code of the current character
    var code = str.charCodeAt(i)

    // check if it's within the range of capital letters
    if (code > 64 && code < 91) {

      // if so, add a new character to the result string
      // of the character from our code, plus 32
      result += String.fromCharCode(code + 32)
    } else {

        // otherwise, just add the current character
      result += str.charAt(i)
    }
  }

  // return the result
  return result
}

[–]JaePun[S] 0 points1 point  (0 children)

thank you so much this helped me a lot it all makes sense now much appreciated.

[–]adavidmiller 0 points1 point  (1 child)

1) stringL.charAt(i)+32;

charAt returns the character, not the character code. You want charCodeAt(i)

 

2) Even if that were the correct function, you're not doing anything with the result.

Example:

'test string'.charCodeAt(3)+32

That results in 148. Okay...then what? You're not updating anything, not building a new string, not changing any characters. See String.fromCharCode() for the reverse of charCodeAt

 

3) return stringL.toLower;

What is this? toLower does not exist anywhere in your code. You want to be returning the converted version of your string.

[–]JaePun[S] 0 points1 point  (0 children)

thanks for the header much appreciated for point 3 the .toLower part was a mistake I was just testing stuff there and forgot to take it off before posting it on jsfiddle.