Skip to content
Advertisement

Output is coming out to be Greater than sign(>)

This is the question:

This is the question image, the question is itself pretty simple

I’ve written my code solution in JavaScript and as it’s on CODECHEF, I gotta run it on NodeJs environment. But on submission, it shows the time limit exceeded, and on custom input, it outputs the Greater than sign(>).I strongly believe that the problem is with my input, I think its not right, please help. The following is my code:

process.stdin.resume();
process.stdin.setEncoding('utf8');

var readline = require('readline');
 
var rl = readline.createInterface(
        process.stdin, process.stdout);
 
//rl.setPrompt(`What is your age? `);
rl.prompt();


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

  var reverseNum;

    
  reverseNum = function(num) {
      for(let i=0;i<num;i++) {
        let r = num.toString().split('').reverse().join('');
        //let final =  Math.sign(num) * parseInt(r);
        console.log( Math.sign(num) * parseInt(r));
      }
  }
  rl.close();
});

   

 

Advertisement

Answer

Always process input .on('data') and output .on('end').

AC Solution Below (Question)

enter image description here

process.stdin.resume();
process.stdin.setEncoding("utf8");

var input = "";
var arr;

process.stdin.on("data", function (data) {
  input += data;
});

process.stdin.on("end", function () {
  arr = input.split("n");
  arr.slice(1).forEach((a) => {
    console.log(a.split("").reverse().join("").replace(/^0*/, ""));
  });
});

Reversing Logic

const 
  str = "2300",
  reversed = str.split("").reverse().join("").replace(/^0*/, "");

console.log(reversed);
Advertisement