Compare the Triplets (interesting algorithm) [JavaScript]

Hi there, I’ve been solving this nice algorithm https://www.hackerrank.com/challenges/compare-the-triplets

Can anyone please explain me what I should correct so that it works?

<Below this line, add a link to the EXACT exercise that you are stuck at.>
The initial and the last parts of the code can be found here https://www.hackerrank.com/challenges/compare-the-triplets

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>
Also, it gives me a SyntaxError: Unexpected token { after line 10.

Really appreciate your help!

function solve(a0, a1, a2, b0, b1, b2){
    // Complete this function
var a = 0
var b = 0
var a0 = 0
var b0 = 0
var a1 = 0
var b1 = 0
var a2 = 0
var b2 = 0
    if(a0 > b0) { 
    a++; 
}   else(b0 > a0) { b++; 
} 
    else if(a0 === b0) { a == 0, b == 0; 
            }
    if(a1 > b1) { 
    a++; 
}   else(b1 > a1) { b++;
} 
         else if(a0 === b0)  { a == 0, b == 0; 
                  }
if(a2 > b2) { 
    a++; 
}   else(b2 > a2) { b++;
} 
        else if(a0 === b0)  { a == 0, b == 0; 
                 }
}
  return(a,b);

Those lines are wiping out the argument values of the parameters. all six should be removed.

else never comes before else if. Suggest tidy up your code so it has consistent layout and it will be easier to examine and debug.

ES6 may allow this sort of return, but ES5 will only return a, and not b. To return both values they need to be in an array.

return [a, b];

or a string,

return [a.toString(), b.toString()].join(' ');

Here is a sort of brute force approach

Code
// https://discuss.codecademy.com/t/compare-the-triplets-interesting-algorithm-javascript/86035/2
// compare triplets (a0, a1, a2) and (b0, b1, b2)

function bound_error (args) {
    if (args.length !== 6) return true;
    for (var i = 0; i < args.length; i++) {
        if (isNaN(args[i])) return true;
        if (args[i] < 0 || args[i] > 100) return true;
        args[i] = parseInt(Math.round(parseFloat(args[i])), 10);
    }
    return false;
}

function solve(a0, a1, a2, b0, b1, b2) {
    if (bound_error(arguments)) return "Input Error";
    var a = 0, b = 0;
    if (a0 > b0) a++;
    if (a1 > b1) a++;
    if (a2 > b2) a++;
    if (b0 > a0) b++;
    if (b1 > a1) b++;
    if (b2 > a2) b++;
    return [a.toString(), b.toString()].join(' ');
}

console.log(solve(5, 6.5, 7, 3, 6.4,10));
function bound_error (args) {
    if (args.length !== 6) return true;
    for (var i = 0; i < args.length; i++) {
        if (isNaN(args[i])) return true;
        if (args[i] < 0 || args[i] > 100) return true;
    }
    return false;
}

function solve(a0, a1, a2, b0, b1, b2) {
    if (bound_error(arguments)) return "Input Error";
    var a = 0, b = 0;
    if (a0 > b0) a++;
    if (a1 > b1) a++;
    if (a2 > b2) a++;
    if (b0 > a0) b++;
    if (b1 > a1) b++;
    if (b2 > a2) b++;
    console.log(a, b);
    return 0;
}

Output

 > solve(5,6,7,3,6,-10)
=> 'Input Error'
 > solve(5,6,7,3,6,10)
 1 1
=> 0
 > solve('1','2','3','4','5','6')
 0 3
=> 0
 > solve('1','2','3','4','5','a')
=> 'Input Error'
 > solve(1,2,3)
=> 'Input Error'

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.