检查字母顺序

发布于 2025-01-13 09:27:09 字数 1035 浏览 4 评论 0原文

我是一个新手,正在努力掌握 javascript。请帮助我巩固我的基础。

输入将是一串字母。

以下是要求。

如果满足以下条件,函数应返回 true:

  1. 字母按字母顺序排列。 (不区分大小写)

  2. 仅传递一个字母作为输入。示例:

isAlphabet('abc') === true

isAlphabet ('aBc') === true

isAlphabet ('a') === true

isAlphabet ('mnoprqst') === false 

isAlphabet ('') === false

isAlphabet ('tt') === false
function isAlphabet(letters) {
    
    const string = letters.toLowerCase();
    
    for (let i = 0; i < string.length; i++) {
        
        const diff = string.charCodeAt(i + 1) - string.charCodeAt(i);
        
        if (diff === 1) {
            
            continue;
            
        } else if (string === '') {
            
            return false;
            
        } else if (string.length === 1) {
            
            return true;
            
        } else {
            
            return false;
            
        }
        
    }
    
    return true;
    
}

I am a newbie who is trying hard to have a grip on javascript. please help me to consolidate my fundamentals.

input will be a string of letters.

following are the requirements.

function should return true if following conditions satisfy:

  1. letters are in alphabetical order. (case insensitive)

  2. only one letter is passed as input. example :

isAlphabet ('abc') === true

isAlphabet ('aBc') === true

isAlphabet ('a') === true

isAlphabet ('mnoprqst') === false 

isAlphabet ('') === false

isAlphabet ('tt') === false
function isAlphabet(letters) {
    
    const string = letters.toLowerCase();
    
    for (let i = 0; i < string.length; i++) {
        
        const diff = string.charCodeAt(i + 1) - string.charCodeAt(i);
        
        if (diff === 1) {
            
            continue;
            
        } else if (string === '') {
            
            return false;
            
        } else if (string.length === 1) {
            
            return true;
            
        } else {
            
            return false;
            
        }
        
    }
    
    return true;
    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〗斷ホ乔殘χμё〖 2025-01-20 09:27:10

一般来说,更好的做法是从处理边缘情况开始你的函数,而不是把它们放在中间的某个地方。这样,函数就会尽快返回 - 并且它比 if..else 语句的瀑布更容易阅读。

function isAlphabet(letters) {
    if ("" == letters) { 
        return false;
    }
    if (1 == letters.length) {
        return true;
    }

    const string = letters.toLowerCase();
    // carry on with your loop here.
    
}

It's generally a better practice to start your function off with dealing with the edge-cases rather than putting them somewhere in the middle. That way, the function returns as soon as it can - and it's a lot easier to read than a waterfall of if..else statements.

function isAlphabet(letters) {
    if ("" == letters) { 
        return false;
    }
    if (1 == letters.length) {
        return true;
    }

    const string = letters.toLowerCase();
    // carry on with your loop here.
    
}
冷夜 2025-01-20 09:27:10

您的想法是正确的,但它可以简化为在特定错误条件下失败,即当较小的字符跟随较大的字符时:

function isAlphabet(letters) {
    const string = letters.toLowerCase();
    let lastChar;
    
    for (let i = 0; i < string.length; i++) {
        // Grab a character
        let thisChar = string.charCodeAt(i);
        
        // Check for the failure case, when a lower character follows a higher one
        if (i && (thisChar < lastChar)) {
            return false;
        }
        
        // Store this character to check the next one
        lastChar = thisChar;
    }
    
    // If it got this far then input is valid
    return true;
}

console.log(isAlphabet("abc"));
console.log(isAlphabet("aBc"));
console.log(isAlphabet("acb"));

You've got the right idea, but it can be simplified to just fail on a particular error condition, i.e when a smaller character follows a larger one:

function isAlphabet(letters) {
    const string = letters.toLowerCase();
    let lastChar;
    
    for (let i = 0; i < string.length; i++) {
        // Grab a character
        let thisChar = string.charCodeAt(i);
        
        // Check for the failure case, when a lower character follows a higher one
        if (i && (thisChar < lastChar)) {
            return false;
        }
        
        // Store this character to check the next one
        lastChar = thisChar;
    }
    
    // If it got this far then input is valid
    return true;
}

console.log(isAlphabet("abc"));
console.log(isAlphabet("aBc"));
console.log(isAlphabet("acb"));

几味少女 2025-01-20 09:27:10

您可以使用简单的方法来实现与下面相同的效果

function isAlphabet(inputString)
{
  var sortedString = inputString.toLowerCase().split("").sort().join("");
  return sortedString == inputString.toLowerCase();
}

console.log("abc = " + isAlphabet("abc"));
console.log("aBc = " + isAlphabet("aBc"));
console.log("acb = " + isAlphabet("acb"));
console.log("mnoprqst = " + isAlphabet("mnoprqst"));

注意:标记答案可以解决您的问题。

You can use the simple way to achieve the same as below

function isAlphabet(inputString)
{
  var sortedString = inputString.toLowerCase().split("").sort().join("");
  return sortedString == inputString.toLowerCase();
}

console.log("abc = " + isAlphabet("abc"));
console.log("aBc = " + isAlphabet("aBc"));
console.log("acb = " + isAlphabet("acb"));
console.log("mnoprqst = " + isAlphabet("mnoprqst"));

Note: Mark the answer is resolves your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文