Search code examples
javascriptblockly

Javascript does not execute correctly


Good evening, I am using Blockly to learn to programm. In the exercise part of the code is not executed correctly. If "maandloon" is above > 2000, then there is a reduction of 25% on 'Kindergeld'. However if the result after the reduction is below 25 euro per child, then there is no reduction. The problem is that calculation keeps using the 25% when maandloon >2000, even if the result afer reduction is below 25 euro per child.

This is my code:

var aantalKinderen, maandloon, kindergeld, kindergeldBasis, toeslag3ekind, toeslag5ekind, i;
do {
aantalKinderen=parseInt((parseFloat((output = window.prompt('Hoeveel kinderen?')) ? output : "")));
var blocktype_number = true;
if (output == null) {
    window.alert("No empty input allowed");
    break;
    }
} while(isNaN(aantalKinderen));
do {
maandloon=(parseFloat((output = window.prompt('Wat is het maandloon?')) ? output : ""));
var blocktype_number = true;
if (output == null) {
    window.alert("No empty input allowed");
    break;
    }
} while(isNaN(maandloon));
kindergeldBasis = 25;
toeslag3ekind = 12.5;
toeslag5ekind = 7.5;
kindergeld = kindergeldBasis * aantalKinderen;
if (aantalKinderen > 2) {
  kindergeld = kindergeldBasis * aantalKinderen + (aantalKinderen - 2) * toeslag3ekind;
}
if (aantalKinderen > 4) {
  kindergeld = kindergeldBasis * aantalKinderen + (aantalKinderen - 2) * toeslag3ekind + (aantalKinderen - 4) * toeslag5ekind;
}
if (maandloon <= 500) {
  kindergeld = kindergeld * 1.25;
}
if (maandloon > 2000) {
  kindergeld = kindergeld * 0.75;
} else {
  if ((kindergeld * 0.75) / aantalKinderen < 25) {
      kindergeld = kindergeld;
  }
}
window.alert(String('Het kindergeld bedraagt ') + String(kindergeld)+'\n');

Can someone help me?

Thank you.


Solution

  • From what it sounds like you want to move your extra if check into the > 2000 statement. Otherwise it won't fire. Your if else statement is linear. It doesn't hit your else after > 2000 unless the conditional hasn't been matched by any of the previous if statements and the value is less than 2000. It does not run regardless or when the value was changed by a previous if condition.

    var aantalKinderen, maandloon, kindergeld, kindergeldBasis, toeslag3ekind, toeslag5ekind, i;
    do {
    aantalKinderen=parseInt((parseFloat((output = window.prompt('Hoeveel kinderen?')) ? output : "")));
    var blocktype_number = true;
    if (output == null) {
        window.alert("No empty input allowed");
        break;
        }
    } while(isNaN(aantalKinderen));
    do {
    maandloon=(parseFloat((output = window.prompt('Wat is het maandloon?')) ? output : ""));
    var blocktype_number = true;
    if (output == null) {
        window.alert("No empty input allowed");
        break;
        }
    } while(isNaN(maandloon));
    kindergeldBasis = 25;
    toeslag3ekind = 12.5;
    toeslag5ekind = 7.5;
    kindergeld = kindergeldBasis * aantalKinderen;
    if (aantalKinderen > 2) {
      kindergeld = kindergeldBasis * aantalKinderen + (aantalKinderen - 2) * toeslag3ekind;
    }
    if (aantalKinderen > 4) {
      kindergeld = kindergeldBasis * aantalKinderen + (aantalKinderen - 2) * toeslag3ekind + (aantalKinderen - 4) * toeslag5ekind;
    }
    if (maandloon <= 500) {
      kindergeld = kindergeld * 1.25;
    }
    if (maandloon > 2000) {
      kindergeld = kindergeld * 0.75;
       if (kindergeld / aantalKinderen < 25) {
          kindergeld = 25;
      }
    } 
    window.alert(String('Het kindergeld bedraagt ') + String(kindergeld)+'\n');