0

I have came across an odd problem with getting JSON data like the following.

[
   {
      "type":"ripe",
      "red":137,
      "green":68,
      "blue":40,
      "strftime(\"%H:%M:%S\", time)":"18:46:37"
   },
]

I was not able to compare this data by type using JavaScript, they both successfully went through my if statement for some odd reason. The total count for both variables is equal to 2.

            let counterLoop = function() {
                for (let i = 0; i < data.length; i++) {
                    let fruitType = JSON.stringify(data[i].type);
                    sortFruit(fruitType.toLowerCase());
                }
            }
            let sortFruit = function(fruitType) {
                if (fruitType.localeCompare('ripe') === 0){} {
                    totalRipeFruit++;
                    $("#totalRipeFruit").text(totalRipeFruit);
                }
                if (fruitType.localeCompare('unripe') === 0){} {
                    totalUnripeFruit++;
                    $("#totalUnripeFruit").text(totalUnripeFruit);
                }
            }

Any idea why this could be the case?

Thank you very much!

6
  • if (fruitType.localeCompare('ripe') === 0){} { <= what is this syntax? Commented May 15, 2020 at 16:38
  • Both of your if statements are being given an empty {} block, so they do nothing. Then they are followed by a block that is not dependent upon the if statement, that always happen. Seems like a typo, but want to make sure you're not thinking this should work another way, and does not Commented May 15, 2020 at 16:40
  • @Taplar that sounds like an answer, especially if you can get a working snippet to go with it. Commented May 15, 2020 at 16:45
  • @GarrettMotzner If the issue is a typo, then it is not an answer. If it is a typo, the question is closable as being a typo question, as it holds little value for future readers. Commented May 15, 2020 at 16:46
  • @Taplar, can't believe I was scratching my head over this for this long.. but any idea why it's not displaying any sort of number after this fix? Commented May 15, 2020 at 16:47

1 Answer 1

1

You have two problems here; First of all there is no need for the JSON.stringifyon the type, just leave it out as it will return the string containing useless quotes. Secondly, your if statements are messed up: You have a second pair of brackets behind each one, so simply change

        if (fruitType.localeCompare('ripe') === 0){} {
            totalRipeFruit++;
            $("#totalRipeFruit").text(totalRipeFruit);
        }
        if (fruitType.localeCompare('raw') === 0){} {
            totalRawFruit++;
            $("#totalRawFruit").text(totalRawFruit);
        }

To:

if (fruitType.localeCompare('ripe') === 0) {
    totalRipeFruit++;
}
if (fruitType.localeCompare('raw') === 0) {
    totalRawFruit++;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, it was both issues that were causing me this problem...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.