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

193
Vistas
Javascript getting a function to execute based on and/or

My code:

let buttonScore = document.querySelector('.button-score');
let paraScore = document.querySelector('.para-score');

buttonScore.addEventListener('click', scoreEntry);

function scoreEntry() {
    let scoreOne = Number(prompt('Enter first score'));
    let scoreTwo = Number(prompt('Enter second score'));
    
    if (scoreOne > scoreTwo) {
        scoreCheck();
    }
}

function scoreCheck() {
    if (scoreOne > 9) {
        paraScore.textContent = 'Excellent!';
    } else if (scoreOne < 7) {
        paraScore.textContent = 'Not enough';
    } else {
        paraScore.textContent = 'Good';
    }
}

Upon the user entering the two scores, I want the script to select the bigger score of the two and run scoreCheck() on it. I assume my problem has to do with non-global variables. In case this can also be done with one function instead of two, I'd also like to know please.

Would very much appreciate clarification, thanks in advance.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You should make the variable a parameter to the second function.

Instead of testing whether one score is higher than the other, use Math.max() to select the higher score, and pass that as the argument.

function scoreEntry() {
    let scoreOne = Number(prompt('Enter first score'));
    let scoreTwo = Number(prompt('Enter second score'));
    
    scoreCheck(Math.max(scoreOne, scoreTwo));
}

function scoreCheck(score) {
    if (score > 9) {
        paraScore.textContent = 'Excellent!';
    } else if (score < 7) {
        paraScore.textContent = 'Not enough';
    } else {
        paraScore.textContent = 'Good';
    }
}
about 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
Recomiéndame algunas ofertas
Necesito ayuda