Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

237
Vistas
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;
    
}
over 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

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.
    
}
over 4 years ago · Juan Pablo Isaza Denunciar

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

over 4 years ago · Juan Pablo Isaza Denunciar

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.

over 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Asistente virtual