nodejs input output

Hello ! I wanted to solve the factorial (easy) test in nodejs, but i get the “wrong answer” alert every time.
The solutions itself is right, but i probably make mistakes with the input / output.

Because the input in nodejs is an event, its not as straight as in other languages to implement. Can i get some help here ?
Many thanks in advance…

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


process.stdin.on('data', function(chunk) { start(chunk) });

function start(chunk){
		
		var chunks = chunk.toString().split('\n');
		maxcount = chunks.shift();
		chunks = chunks.filter(function(e){return e}); 
		
		chunks.forEach(function(val,index){ 
				if(maxcount>0){
				solveProblem(val);
				
				}
			
			if(maxcount-1>0){process.stdin.destroy();}
			maxcount--;
			});
		
}

function solveProblem(numba){
var solution = checkfactorial(numba);
//solution = solution.toString();
console.log(solution);


}


function checkfactorial(number){

var temp = 1;
var sum = 0;
while(temp <= number){

	temp = temp*5;
	sum = sum + Math.floor(number/temp);
	
}

return sum;
}
/**
This issue was I/O 

Always process input .on('data') and output .on('end'). Note that
.on('end') does not receive any parameter in the callback.

Please test this, let me know if you could get around the problem 
**/

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

// declare global variables
var input_stdin = "";
var chunks = "";
var input_currentline = 0;

// standard input is stored into input_stdin
process.stdin.on('data', function (data) {
    input_stdin += data;
});

// standard input is done and stored into an array
process.stdin.on('end', function () {
    chunks = input_stdin.split("\n");
    start();    
});

function start(){
	maxcount = chunks.shift();
	chunks = chunks.filter(function(e){return e});
	
	chunks.forEach(function(val,index) { 
		if(maxcount-- >0){
			solveProblem(val);
		}
	});
}

function solveProblem(numba) {
	var solution = checkfactorial(numba);
	console.log(solution);
}

function checkfactorial(number) {
	var temp = 1;
	var sum = 0;
	
	while(temp <= number) {
		temp = temp*5;
		sum = sum + Math.floor(number/temp);
	}

	return sum;
}
3 Likes

Some Awesome resource:
Learn NodeJS from PROs
https://freelectureslinks.blogspot.com/2019/09/node-js-playlist.html

2 Likes

Great Resources bro Thanks for this

1 Like