Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

204
Visualizações
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 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

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 4 years ago · Juan Pablo Isaza Relatório

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 4 years ago · Juan Pablo Isaza Relatório

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 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda