2

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

  • Input: input, a string separated by space
  • Output: strings separated by newline

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.

Coder-Neel
  • 51
  • 1
  • 6

5 Answers5

3

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");



});
Coder-Neel
  • 51
  • 1
  • 6
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 06 '22 at 21:17
  • How did It work for you if you didn't bind any logInfo function to 'MyEvent' ? – Diego Ramos Feb 20 '22 at 04:10
  • working fine just copy paste this code – Md Rehan Apr 28 '23 at 12:37
2
process.stdin.resume();
process.stdin.setEncoding("ascii");
var input = "";
process.stdin.on("data", function (chunk) {
    input += chunk;
});



process.stdin.on("end", function () {
    const myArr = input.split(" ");
    const EventEmitter = require('events');
    var eventEmitter = new EventEmitter();
    let sum=0;
    let a = myArr[0];
    let b = myArr[1];
    for(let i=3; i<=1000; i++){
        if(i%a==0){
            sum += i
        }
        if(i%b==0){
            sum+=i
        }
    }
    
    eventEmitter.on('MyEvent', function(){
        setTimeout(function(){
            console.log("Multiples of " + a + " & "+ b);
            console.log(sum);},5000);
    });
    
    eventEmitter.emit('MyEvent'); 
});
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 28 '21 at 07:09
0

I hope below code will help you and will give expected output. Thanks! :)

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

process.stdin.on("end", function () {
    const myArr = input.split(" ");
    const EventEmitter = require('events');
    var eventEmitter = new EventEmitter();
    let sum=0;
    let a = myArr[0];
    let b = myArr[1];
    for(let i=3; i<=1000; i++){
        if(i%a==0){
            sum += i
        }
        if(i%b==0){
            sum+=i
        }
    }

    eventEmitter.on('MyEvent', logInfo);

    function logInfo(input) {
        console.log("Multiples of " + a + " & "+ b);
        console.log(sum);
    }

    eventEmitter.emit('MyEvent', ""); 
});
Ravi
  • 26
  • 4
0

Check below solution

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

process.stdin.on("end", function () {
    const myArr = input.split(" ");
    const EventEmitter = require('events');
    var eventEmitter = new EventEmitter();
    let sum=0;
    let a = myArr[0];
    let b = myArr[1];
    for(let i=3; i<=1000; i++){
        if(i%a==0){
            sum += i
        }
        if(i%b==0){
            sum+=i
        }
    }

    eventEmitter.on('MyEvent', logInfo);

    function logInfo(input) {
        console.log("Multiples of " + a + " & "+ b);
        console.log(sum);
    }

    eventEmitter.emit('MyEvent', ""); 
});
0

I have only call console as async function kindly avoid my mistake.

process.stdin.on("end", function () {

    // Enter your code here
      const myArr = input.split(" ");
    const EventEmitter = require('events');
    var eventEmitter = new EventEmitter();
    let sum=0;
    let a = myArr[0];
    let b = myArr[1];
    for(let i=3; i<=1000; i++){
        if(i%a==0){
            sum += i
        }
        if(i%b==0){
            sum+=i
        }
    }
    setTimeout(function(){
        console.log(sum);
    },2000)

    eventEmitter.on('MyEvent', logInfo);

    function logInfo() {
        console.log("Multiples of " + a + " & "+ b);
        
    }

    eventEmitter.emit('MyEvent'); 

});
cigien
  • 57,834
  • 11
  • 73
  • 112