vendredi 26 avril 2019

Why do I get contradictory statements with respect to similar conditions?

I'm practicing Javascript on FreeCodeCamp and I have to write chained if/else if statements to fulfill the following conditions:

num < 5 - return "Tiny"
num < 10 - return "Small"
num < 15 - return "Medium"
num < 20 - return "Large"
num >= 20 - return "Huge"

I wrote the following code then:

function testSize(num){
  // Only change code below this line
  if (num < 5) {
    return "Tiny";
  } else if (num < 10) {
    return "Small";
  } else if (num < 15) {
    return " Medium";
  } else if (num < 20){
    return "Large";
  } else if (num >= 20) {
    return "Huge";
  } else {
    return "Change Me";
  }

  // Only change code above this line
}

// Change this value to test
testSize(10);

This is what I get in the output field:

// running tests
testSize(10) should return "Medium"
testSize(14) should return "Medium"
// tests completed

If we look at my snippet, in line 5 I specified the condition (num < 10), not (num <= 10). The subsequent condition (line 7) is (num < 15) and when it is satisfied the statement is supposed to be "Medium": why (num = 10) doesn't return "Medium"? The same holds for (num = 14): it is greater than 10 and less than 15: shouldn't be "Medium" as well?

On the other hand, the text run successfully for the following values:

testSize(0) should return "Tiny"
Passed
testSize(4) should return "Tiny"
Passed
testSize(5) should return "Small"
Passed
testSize(8) should return "Small"
Passed
testSize(15) should return "Large"
Passed
testSize(17) should return "Large"
Passed
testSize(20) should return "Huge"
Passed
testSize(25) should return "Huge"
Passed

I wonder why the text passed for the values above but not when num is given 10 or 14.

Why do I get contradictory statements with respect to similar conditions?

Aucun commentaire:

Enregistrer un commentaire