简体   繁体   English

检查字符串中字符的ASCII码值是否大于或小于前一个字符的ASCII码值-JavaScript

[英]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 . Codewars上尝试此问题。

The function should return true if the characters in the string appear in order. 如果字符串中的字符按顺序出现,则函数应返回true。

For example: 例如:

solve("abc") = True, because it contains a,b,c resolve(“ abc”)= True,因为它包含a,b,c

solve("abd") = False, because a, b, d are not consecutive. resolve(“ abd”)= False,因为a,b,d不连续。

solve("dabc) = True, because it contains a, b, c, d resolve(“ dabc)= True,因为它包含a,b,c,d

solve("abbc") = False, because b does not occur once. resolve(“ abbc”)= False,因为b不会出现一次。

solve("v") = True resolve(“ 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. 我的想法是检查字符串中的下一个字符是否具有大于上一个字符的ASCII码值的ASCII码值。

If this is the case, return true. 如果是这种情况,请返回true。 Otherwise, return false. 否则,返回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. 但是如您所见,我不知道如何与上一个角色进行比较。

You have a few issues in your code. 您的代码中有几个问题。

  1. You aren't comparing the right content in your if clause. 您没有在if子句中比较正确的内容。 You are comparing the same thing. 您正在比较同一件事。 You need to check if the next char code index is greater than your current. 您需要检查下一个字符代码索引是否大于当前索引。

  2. You are immediately returning after the first character because you return true off top without looping through the values. 您将在第一个字符之后立即返回,因为您从顶部返回true而不循环遍历值。

Make sure to pay attention when you are looping, your biggest issue is you are just straight comparing the same char over and over because you stop worrying about the index of the char you are checking. 确保在循环时要注意,最大的问题是您一次又一次地比较相同的char,因为您不再担心要检查的char的索引。

I would say basically in your function if the next char code is less than the current return false, otherwise return true. 我基本上会在您的函数中说,如果下一个char代码小于当前返回的false,否则返回true。 This will only return true after looping through the entire string. 仅在遍历整个字符串后才返回true。

 function solve(s){ s = s.trim().split("").sort().join(""); // remove white spaces, split to sort and then join again for (let i = 0; i < s.length - 1; i++) { if ((s.charCodeAt(i + 1) - s.charCodeAt(i)) !== 1) { return false; } } return true; } console.log(solve('abcdef')); //true console.log(solve('afew')); //false console.log(solve('defghijklmn')); //true console.log(solve('lkjsadf')); //false console.log(solve('123456')); //true console.log(solve('123abc')); //true console.log(solve('abc123')); //false console.log(solve('abd')); //false console.log(solve('abbc')); //false console.log(solve('abc')); //true console.log(solve('dabc')); // true 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM