Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

285
Vistas
How can I fix this Javascript solution

Problem: https://open.kattis.com/problems/abc

So I am trying to do this code problem in Kattis with Javascript and I am unable to understand why my solution is wrong. If anyone could tell me why they think it might be wrong or highlight how I could test the Kattis cases and see the outputs that would be excellent. I didn't just code this up, and instantly assumed it was right but I have created several test beds to simulate standard input and they seem to produce the same answers as the sample input. Note, there's probably a much better way to approach the solution, I am very unfamiliar with node.JS still.

Here is my code:

const readline = require('readline');


const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});


rl.on('line', (line) => {

    var array = (line.split(' '));
    var test = parseInt(array[0]);
    
    if (!isNaN(test)) { // its a numba 
        
        var a = parseInt(array[0]);
        var b = parseInt(array[1]);
        var c = parseInt(array[2]);
        var arr2 = [a, b, c];
        var arr = arr2.sort(function (a, b) { return a - b });
        a = arr[0];
        b = arr[1];
        c = arr[2];
    }
    // z y x
    else if (isNaN(test)) {
        var g = line.split('');
        var z = g[0];
        var y = g[1];
        var x = g[2];


        if (z == 'A' && y == 'B') {
            var answer = [a, b, c];
        }

        else if (z == 'A' && x == 'B') {
            answer = [a, c, b];
        }

        else if (y == 'A' && x == 'B') {
            answer = [c, a, b];
        }

        else if (y == 'A' && z == 'B') {
            answer = [b, a, c];
        }

        else if (x == 'A' && z == 'B') {
            answer = [b, c, a];
        }

        else if (x == 'A' && y == 'B') {

            answer = [c, b, a];
        }
        else{
            console.log(b + " " + a + " " + c);
        }

    

        console.log(answer[0] + " " + answer[1] + " " + answer[2]);
    }
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

As Bravo said in the comments: Your variables a,b,c get erased when the line callback function returns. Just make them global.

Your else clause that tests for all those conditions can be greatly simplified. Imagine this:

    else {
        var g = line.split('');
        let outputLine = [];

        g.forEach(item => {
            if (item == 'A') {
                outputLine.push(a);
            }
            else if (item == 'B') {
                outputLine.push(b);
            }
            else if (item == 'C') {
                outputLine.push(c);
            }
        });

        console.log(outputLine[0], outputLine[1], outputLine[2]);
    }

Or even this:

    else {
        var g = line.split('');
        let table = {"A":a, "B":b, "C":c}
        console.log(table[g[0]], table[g[1]], table[g[2]]);
    }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda