skip to Main Content

I’m working on a challenge called Compare the Triplets in Hacker Rank where the function takes in two arrays as parameters and you are meant to compare the indexes and award a point to the group with a higher number and return the score for each array.

my code is as follows:

let aTemp = readLine().split(' ');
    let a0 = parseInt(aTemp[0]);
    let a1 = parseInt(aTemp[1]);
    let a2 = parseInt(aTemp[2]);
    let bTemp = readLine().split(' ');
    let b0 = parseInt(bTemp[0]);
    let b1 = parseInt(bTemp[1]);
    let b2 = parseInt(bTemp[2]);
    
    let aScore = 0;
    let bScore = 0;
    
    if(a0 > b0){
        a++;
    };
    if (a0 < b0){
        b++;
    };
    if(a1 > b1){
        a++;
    };
    if (a1 < b1){
        b++;
    };
    if(a2 > b2){
        a++;
    };
    if (a2 < b2){
        b++;
    };
    console.log(aScore + ' ' + bScore);

I am getting the error:

TypeError: Cannot read properties of undefined (reading ‘split’)

I have tried searching this error to get more information but do not seem to see anything relevant to my issue.

2

Answers


  1. You’re missing one argument in the readLine function. You can just use readLine('Write something divided by spaces').

    Also you can refactor your code to be a bit more simple. Here’s my take.

    const aTemp = readLine('Write something divided by spaces').split(' ');
    const bTemp = readLine('Write something divided by spaces').split(' ');
    
    let aScore = 0;
    let bScore = 0;
    
    if (aTemp.length !== bTemp.length){
        throw Error('The array lengths must match.');
    }
    
    aTemp.forEach((a, index) => {
        const b = bTemp[index];
        if (a > b) aScore++;
        if (a < b) bScore++;
    });
    
    console.log({aScore, bScore});
        
    
    Login or Signup to reply.
  2. The undefined you get from readLine means you are reading more lines from the input than is given as input. This typically happens when the input was already read, and you are trying to read it again.

    The template that HackerRank offers before you start entering your code, is this:

    'use strict';
    
    const fs = require('fs');
    
    process.stdin.resume();
    process.stdin.setEncoding('utf-8');
    
    let inputString = '';
    let currentLine = 0;
    
    process.stdin.on('data', function(inputStdin) {
        inputString += inputStdin;
    });
    
    process.stdin.on('end', function() {
        inputString = inputString.split('n');
        main();
    });
    
    function readLine() {
        return inputString[currentLine++];
    }
    
    /*
     * Complete the 'compareTriplets' function below.
     *
     * The function is expected to return an INTEGER_ARRAY.
     * The function accepts following parameters:
     *  1. INTEGER_ARRAY a
     *  2. INTEGER_ARRAY b
     */
    
    function compareTriplets(a, b) {
    
    }
    
    function main() {
        const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
        const a = readLine().replace(/s+$/g, '').split(' ').map(aTemp => parseInt(aTemp, 10));
        const b = readLine().replace(/s+$/g, '').split(' ').map(bTemp => parseInt(bTemp, 10));
        const result = compareTriplets(a, b);
        ws.write(result.join(' ') + 'n');
        ws.end();
    }
    

    Note how the code in main already reads the necessary input, calling readLine, so you shouldn’t do that in your implementation. Instead you should put your code inside the compareTriplets function. That function is called from main (which you shouldn’t touch!), and gets the two arrays as arguments. It should not perform console.log, but return the result as a 2-element array. It is main that takes care of outputting that result in the required format.

    Not your question, but you can avoid the repetition of similar code you have for a0, a1 and a2, by making use of arrays.

    Here is how it could be done:

    function compareTriplets(a, b) {
        const scores = [0, 0];
        for (let i = 0; i < a.length; i++) {
            const diff = a[i] - b[i];
            if (diff) {
                scores[+(diff < 0)]++;
            }
        };
        return scores; // Return an array. The caller takes care of outputting it
    }
    

    Don’t touch the rest of the template code you get to start with. Only define the body of the compareTriplets function.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search