Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!