mardi 16 avril 2019

Why is my code skipping the if-statement and only returning the else part? JS

Categorize New Member on CodeWars

To be a senior, a member must be at least 55 years old and have a handicap greater than 7. In this croquet club, handicaps range from -2 to +26; the better the player the lower the handicap.

Input Input will consist of a list of lists containing two items each. Each list contains information for a single potential member. Information consists of an integer for the person's age and an integer for the person's handicap.

My code skips the if part; it only returns "Open" and never "Senior"

I have already looked on stackoverflow for similar problems; however, I can't find one in JavaScript (have seen java, css, html, python, etc.). Additionally, the problems already asked seem to relate more to if statements inside of if statements, or they have the opposite problem where the else statement is skipped. I have tried switching the "Open" and "Senior" checks, but that doesn't work. I have had problems before using int with strings and vice versa, but I don't think that should be a problem in this instance because they're all integers here.

function openOrSenior(data){
  var array = [];
  for(var i = 0; i < data.length; i++) {
    var age = data[i[0]];
    var handicap = data[i[1]];
    if (age >= 55 && handicap > 7) {
      array.push("Senior");
    } else {
      array.push("Open");
    }
  }
  return array;
}


Test Lists

Test.assertSimilar(openOrSenior([[45, 12],[55,21],[19, -2],[104, 20]]),['Open', 'Senior', 'Open', 'Senior']) => returned [['Open', 'Open', 'Open', 'Open'])

Test.assertSimilar(openOrSenior([[59, 12],[55,-1],[12, -2],[12, 12]]),['Senior', 'Open', 'Open', 'Open']) => returned [['Open', 'Open', 'Open', 'Open'])

Aucun commentaire:

Enregistrer un commentaire