0

forgive me if this is a dumb question but im trying to find the difference between these 2 statements. I wrote the first code and got the 2nd code from stack overflow. Of course the stack overflow works but when I try and do it the long way, it won't run. What am I doing wrong? What's the difference in our code that makes mine break??

function compareTriplets(a, b) {
    // Write your code here
    let score = [0,0];
for (let i = 0; i < a.length; i++) {
    if (a[i] > b[i]) {
        return score[0]++;
    } else if (a[i] < b[i]) {
        return score[1]++;
    } else return ""
}
return score;
}

//this is the code from stack overflow that works. I can't find the difference.

function compareTriplets(a, b) {
let score = [0,0]

for (let i = 0; i < a.length; i++)
a[i] > b[i] ? score[0]++ : a[i] < b[i] ? score[1]++ : ""
return score

Here is where I'm getting the question from.

Please let me know if you have any ideas. Thanks for your time.

Shaneeyb
  • 125
  • 2
  • 9
  • 2
    remove the `return`s. `return` immediately exits the function (you'll note the ternary has no returns in it.) Also the final `else` isn't needed in your refactoring, but is in the [ternary](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) because it is always an 'if...else' – pilchard Jul 20 '22 at 21:53
  • Ahhhh gotcha! Thank you so much! This worked and I understand now. I appreciate it! : ) – Shaneeyb Jul 20 '22 at 22:18

0 Answers0