Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why am i getting ~ no response on stdout ~ in hacker rank nodejs

Tags:

node.js

Please who know how to run a function with stdin and stdout on hackerrank for example

function getPrimes(n){

nextPrime:
for (let i = 2; i <= n; i++) { // for each i...

  for (let j = 2; j < i; j++) { // look for a divisor..
    if (i % j == 0) continue nextPrime; // not a prime, go next i
  }

 console.log(i) ; // a prime
}
}

then use process. stdin and stdout dunction to display result

like image 553
Samuel Avatar asked Nov 28 '22 21:11

Samuel


2 Answers

Hackerrank documentations haven't mentioned this.

This is how it worked for me.

// It will work on Javscript(nodejs), not sure about others
fs.createWriteStream(process.env.OUTPUT_PATH).write("Your Output");
like image 200
Muneeb Avatar answered Dec 01 '22 11:12

Muneeb


Look at how the function you're writing is being used. For example, with getPrimes, look for other uses of getPrimes in the HackerRank built-in code. For example, from the sample test, there is a question asking me to complete a findNumber function:

'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 findNumber function below.
function findNumber(arr, k) {

}

function main() {
    const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const arrCount = parseInt(readLine().trim(), 10);

    let arr = [];

    for (let i = 0; i < arrCount; i++) {
        const arrItem = parseInt(readLine().trim(), 10);
        arr.push(arrItem);
    }

    const k = parseInt(readLine().trim(), 10);

    const res = findNumber(arr, k);

    ws.write(res + '\n');

    ws.end();
}

The function findNumber is being called in the line

const res = findNumber(arr, k);

with its return value assigned to res. If the called function's return value is used, you should probably always return a value. Otherwise, if it's not being used, eg if the line was just

findNumber(arr, k);

then the return value is ignored, so your output will probably be determined by what you call console.log with.

In your case, you should probably return a value from getPrimes so that HR can parse it. Not sure what the challenge is asking for, but maybe something like

function getPrimes(n) {
  const arrOfPrimes = [];
  nextPrime: for (let i = 2; i <= n; i++) { // for each i...

    for (let j = 2; j < i; j++) { // look for a divisor..
      if (i % j == 0) continue nextPrime; // not a prime, go next i
    }

    arrOfPrimes.push(i);
  }
  return arrOfPrimes;
}

or something along those lines - just make sure something is being returned at the end, if the result of the getPrimes call is being used somewhere.

like image 35
CertainPerformance Avatar answered Dec 01 '22 10:12

CertainPerformance