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

264
Views
problem with a simple program (check a number is an even number or odd number using javascript)

I am writing a very simple program in JavaScript to check a number is an odd number or even number. and here is my code.

  function run(number) {
    var result;
    if(number % 2 == 0) {
      result == 'even';
    }
    else {
      result == 'odd';
    }
    return result;
  }

As you can see, very easy to understand, right? I have the variable number and check it. I thought that my algorithm is correct. But the program still does not run.

Could you please show me why the program has the problem ? Thank you very much for your time.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Two things here

function run(number) {
    var result;
    if (number % 2 == 0) 
        result = 'even';
    else 
        result = 'odd';
    
    return result;
}

const val = run(11);
console.log(val);

You should be using = (assignment operator) as you're assigning value to a variable and not comparing them (== or === are called comparison operators).

And make sure you actually call the function

about 4 years ago · Juan Pablo Isaza Report

0

As the other answers & comments already point out, == is not storing the result but instead, an operator to compare two variables. One equal sign would do the job. Additionally, I'm suggesting the shorthand version (recommended once you understood what went wrong in the first place):

function run(number) {
  return (number % 2 == 0) ? 'even' : 'odd';
}
console.log(run(5))
console.log(run(6))

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!