• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

170
Views
check the alphabetical order

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;
    
}
about 3 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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.
    
}
about 3 years ago · Juan Pablo Isaza Report

0

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"));

about 3 years ago · Juan Pablo Isaza Report

0

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.

about 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error