node.jsnodesnode-modulesnodejs-streamnodejs-server

Callback and event emitter functionality using NodeJS


Programme language is NodeJS

Steps ToDo: 1. The variable input has the input value. Extract n1 and n2 from the input. 2. Write a function to find the sum of all the multiples of n1 and n2, below and including 1000. This function should log the sum after 2 seconds. 3. Register an event named MyEvent for any instance of an event emitter, and bind a function named logInfo to it, which logs "Multiples of {n1} & {n2}" to the console and emit the event(Don't pass any parameters while emitting the event).

Constraints

Note: Even though you got the exact output, the test cases will fail if you do not use the callback and event concepts as mentioned in the problem statement.

Sample Case 0

Sample Input For Custom Testing 100 1000 Sample Output: Multiples of 100 & 1000 6500

Explanation Multiples of 100 are 100,200,300,......1000 and multiples of 1000 is 1000 in below and including 1000.

Sum = (100+200+............1000) + 1000

Sum = 6500

Sample Case 1

Sample Input For Custom Testing

500 1200

Sample Output:

Multiples of 500 & 1200

1500


I Tried below code :

process.stdin.resume();
process.stdin.setEncoding("ascii");
var input = "";
process.stdin.on("data", function (chunk) {
    input += chunk;
});

process.stdin.on("end", function () {
    
    let _input = input.split (" ");
    let a = parseInt(_input[0]);
    let b = parseInt(_input[1]);
    console.log("Multiples of " + a + " & " + b);
    
    var sum = 0;
    for (var x = 0; x < 1000; x++)
    {
        if (x % a === 0 || x % b === 0)
        {
        sum += x;
        }
    }
    console.log(sum);
    
});

Code Test out:

Case 1

Input (stdin)
4 6

Your Output (stdout)

Multiples of 4 & 6
165834

Expected Output

Multiples of 4 & 6
208666

===============================================

Case 2

Input (stdin)
3 5

Your Output (stdout)

Multiples of 3 & 5
233168

Expected Output

Multiples of 3 & 5
267333


Please help me for this code. My output is not match with this logic.


Solution

  • I got the right solution.

    process.stdin.resume();
    process.stdin.setEncoding("ascii");
    var input = "";
    process.stdin.on("data", function (chunk) {
        input += chunk;
    });
    
    
    process.stdin.on("end", function () {
    
        const EventEmitter = require('events');
        let data = input.split(" ");
        n1 = data[0];
        n2 = data[1];
        // console.log(n1,n2);
    
        let result = 0;
        const sum = () => {
            for (let i = 0; i <= 1000; i++) {
                if (i % n1 == 0) {
                    result += i;
                }
            }
            for (let j = 0; j <= 1000; j++) {
                if (j % n2 == 0) {
                    result += j;
                }
            }
            console.log(result);
        }
        setTimeout(sum, 2000);
    
        const event = new EventEmitter();
    
        event.on("MyEvent", () => {
            console.log(`Multiples of ${n1} & ${n2}`);
        })
        event.emit("MyEvent");
    
    
    
    });