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

184
Views
Else not working in if-else if structure in javascript

Else if structure gives wrong result. I replaced the previous else if and it worked, but this time the below code gave an error.

const gelir_vergisi = {
  gelir: function() {

    matrah = ((document.getElementById("a_sayisi").value) - sgk.vtoplam());
    if (matrah <= (document.getElementById("gelir1").value)) {
      gv1 = (matrah * 15) / 100;
      return gv1;
    } else if ((document.getElementById("gelir1").value) < matrah <= (document.getElementById("gelir2").value)) {
      bir = ((document.getElementById("gelir1").value) * 15) / 100;
      iki = ((matrah - (document.getElementById("gelir1").value)) * 20) / 100;
      gv2 = bir + iki;
      return gv2;
    } else if ((document.getElementById("gelir2").value) < matrah <= (document.getElementById("gelir3").value)) {
      ilk_dilim = ((document.getElementById("gelir1").value) * 15) / 100;
      ikinci_dilim = (((document.getElementById("gelir2").value) - (document.getElementById("gelir1").value)) * 20) / 100;
      ucuncu_dilim = ((matrah - (document.getElementById("gelir2").value)) * 27) / 100;
      gv = ilk_dilim + ikinci_dilim;
      gv3 = gv + ucuncu_dilim;
      return gv3;
    }
  }
}

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

0

You can use the unary plus to cast to number, assuming the fields all contain numbers, otherwise use isNaN to test

Then you can make the whole thing MUCH more readable

I may have entered some brackets wrong, but here is an attempt

NOTE: No need to have else after a return

const gelir_vergisi = {
  gelir: function() {
    let a_sayisi = +document.getElementById("a_sayisi").value;
    let gelir1 = +document.getElementById("gelir1").value;
    let gelir2 = +document.getElementById("gelir2").value;
    let gelir3 = +document.getElementById("gelir3").value;
    const matrah = a_sayisi - sgk.vtoplam();
    if (matrah <= gelir1) return (matrah * 15) / 100;
    if (gelir1 < matrah && matrah <= gelir2) {
      let bir = (gelir1 * 15) / 100;
      let iki = ((matrah - gelir1) * 20) / 100;
      return bir + iki;
    }
    if (gelir2 < matrah && matrah <= gelir3) {
      let ilk_dilim = (gelir1 * 15) / 100;
      let ikinci_dilim = ((gelir2 - gelir1) * 20) / 100;
      let ucuncu_dilim = ((matrah - gelir2) * 27) / 100;
      let gv = ilk_dilim + ikinci_dilim;
      return gv + ucuncu_dilim;
    }
    return 0; // or something else that makes sense
  }
}

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!