lundi 3 juin 2019

Check if ASCII Code Value Of Character In String Is Greater Than or Less Than ASCII Code Value of Previous Character - JavaScript

Attempting this problem on codewars.

The function should return true if the characters in the string appear in order.

For example:

solve("abc") = True, because it contains a,b,c

solve("abd") = False, because a, b, d are not consecutive.

solve("dabc) = True, because it contains a, b, c, d

solve("abbc") = False, because b does not occur once.

solve("v") = True

My thought is to check if the next character in the string has an ASCII code value that is greater than the ASCII code value of the previous character.

If this is the case, return true. Otherwise, return false.

I have:

function solve(s){
  for (let i = 0; i < s.length; i++) {
    let character = s[i];
    //if character ASCII value is > than the ASCII value of character before it
    if (character.charCodeAt(0) > /*previous character.charCodeAt(0));*/ ) {
      return true
    }
    else {
      return false;
    }
  }
}

But as you can see, I don't know how to make a comparison to the previous character.

Aucun commentaire:

Enregistrer un commentaire