Callback and event emitter functionality using NodeJS - node.js

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.

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');
});

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

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', "");
});

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', "");
});

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');
});

Related

How can i reduce time complexity limit to 1s

I was doing practice problems on codechef and i was in the flow that I solved 1 question then another and so on.While solving all the runtime,compiletime and logical error.
Then suddenly 1 error occurred which is to hard for me to solve and need some guidence over it
The Node Js code which i want to reduce time complexity limit for is as follow:-
process.stdin.resume();
process.stdin.setEncoding('utf8');
// your code goes here
let inputString = '';
let currentLine = 0;
process.stdin.on('data',(input) => {
inputString += input;
});
process.stdin.on('end', () => {
inputString = inputString.split('\n');
main();
});
const readline = () => {
return inputString[currentLine++];
}
const main = () => {
let t = parseInt(readline());
while(t--){
let temp = readline().split('');
let count = 0;
for(let i = 0; i < temp.length; i++){
if(temp[i] === '4'){
count++;
}
}
console.log(count);
}
}
Please help me with it ...

Node.js for loop output only last item from JSON

The code below only output the last result, I don't get it. I check if the updateDate item contains 2020-05 both items does and I get only the last one. The loop is not looping :)
const briefing = [
{
"updateDate": "2020-05-05T00:00:00.0Z",
},
{
"updateDate": "2020-05-06T00:00:00.0Z",
},
{
"updateDate": "2020-05-13T00:00:00.0Z",
}
];
let date = new Date();
var formattedYearMonth = date.getFullYear() + '-' + ('0' + (date.getMonth()+1)).slice(-2) + '-';
for (var i = 0; i < briefing.length; i++) {
var jsonDate = briefing[i].updateDate;
if (jsonDate.includes(formattedYearMonth)) {
var response = JSON.stringify(briefing[i]);
}
}return response;
}
for (var i = 0; i < briefing.length; i++) {
var jsonDate = briefing[i].updateDate;
if (jsonDate.includes(formattedYearMonth)) {
var response = JSON.stringify(briefing[i]); // <==== THIS IS WHERE YOUR PROBLEM LIES
}
}return response;
The loop is actually looping :). But for every run of the loop, you are resetting the value of response.
--EDITED--
For the response to be an array, you need to modify your code as
let response = [];
for (var i = 0; i < briefing.length; i++) {
var jsonDate = briefing[i].updateDate;
if (jsonDate.includes(formattedYearMonth)) {
response.push(JSON.stringify(briefing[i]));
}
}
return response;

Batch 500 writes into firestore loop from json file

Using some inspiration I got from this thread and reply I tried to get my loop working which is to write into firestore in batches. But somehow I only can only update 1 document even if I can see I iterate through different values from my array. I load data into an array and work from there.
const db = admin.firestore();
const jsonStream = StreamArray.withParser();
let arr = []
jsonStream.on('data', ({ key, value }) => {
arr.push(value);
});
jsonStream.on('end', () => {
var counter = 0;
var commitCounter = 0;
var batches = [];
arr.forEach((a, ind) => {
batches[commitCounter] = db.batch();
if (counter <= 498) {
var thisRef = db.collection('Testing').doc(a.id);
console.log("id")
console.log(a.id);
batches[commitCounter].set(thisRef, { ...a });
counter = counter + 1;
} else {
counter = 0;
commitCounter = commitCounter + 1;
batches[commitCounter] = db.batch();
}
})
for (var i = 0; i < batches.length; i++) {
if(i==0)
{
console.log(batches[0])
}
batches[i].commit().then(function () {
console.count('wrote batch');
});
}
});
const filename = path.join(__dirname, 'mydata.json');
fs.createReadStream(filename).pipe(jsonStream.input);
Following line gets executed on each iteration, which essentially "resets" your batch on each round:
batches[commitCounter] = db.batch();
So at the end each of your batches will only contain one document write.

Problem with findOne() in sequelize node.js

I have a problem with node.js and sequelize findOne(). I want to find new students, that I want to add to the DB (var novi), and the ones that already exist, I just want to update their field (var stari). Everything works as expected, only when I want to return JSON with how many new students I added to the DB, and how many are updated, values of stari and novi, go back to 0, but the counting is good, I checked. I know the problem is with asynchronous call, but I don't know how to fix.
app.post('/student', function(req,res) {
var imeGodine = req.body['godina'];
//POMOĆNE SKRIPTE BitBucket.js i citanjeGodina.js
var broj = 0;
var stari = 0;
var novi = 0;
db.godina.findOne({where:{nazivGod:req.body.godina}}).then(god => {
var studenti = req.body.studenti;
db.student.count().then (ranijeStudenata => {
for(var i = 0; i<studenti.length; i++) {
var ime = studenti[i].imePrezime;
var ind = studenti[i].index;
db.student.findOne({where:{index :studenti[i].index}}).then(stud => {
if (stud == null) {
novi++;
db.student.create({imePrezime:ime, index : ind}).then(noviStudent => {
god.addStudenti(noviStudent);
});
}
else if (stud != null) {
stari++;
god.addStudenti(stud);
}
});
broj++;
}
var brojNovih = broj - ranijeStudenata; //ne koristi se, ali možda hoće
res.set("Content-Type", "application/json");
res.status(200).send(JSON.stringify({message: "Dodano je " + novi + " novih studenata i upisano " + stari + " na godinu " + imeGodine}));
});
});
});
Picture of code
You can use async/await to do counting in a synchronous way.
'use strict';
app.post('/student', async function (req, res) {
var imeGodine = req.body['godina'];
var {studenti} = req.body;
var broj = 0;
var stari = 0;
var novi = 0;
let god = await db.godina.findOne({where: {nazivGod: req.body.godina}});
let ranijeStudenata = await db.student.count(); // ranijeStudenata not used?
for (var i = 0; i < studenti.length; i++) {
var ime = studenti[i].imePrezime;
var ind = studenti[i].index;
let stud = await db.student.findOne({where: {index: studenti[i].index}});
if (stud === null) {
novi++;
let noviStudent = await db.student.create({imePrezime: ime, index: ind});
god.addStudenti(noviStudent);
} else if (stud !== null) {
stari++;
god.addStudenti(stud);
}
broj++;
}
return res.status(200).send({
message: "Dodano je " + novi + " novih studenata i upisano " + stari + " na godinu " + imeGodine
});
});

Async await in node js with readline inside for loop

So I'm unable to force a for loop to wait for the stream to close before performing the next iteration.
It seems streaming data and async/await don't seem to get along (especially in for loops). Possibly one way around it may be to put everything in an infinite while loop (instead of a for loop) and use a POSIX mutex type method (where a flag is set, blocking opening a stream until the flag is reset at when the stream closes, and counters update). But there must be some kind of way to accomplish it with async/await.
Below is some test code:
async function bulk_search(an_array)
{
var name = "";
var found = false;
for(let i = 0; i < 10; i++)
{
for(let j = 0; j < 10; j++)
{
for(let k = 0; k < 10; k++)
{
console.log(i + ", " + j + ", " + k);
file_stream = fs.createReadStream(__dirname + '/a_directory/a_file.csv');
file_stream.on('error', function (err) {
return an_array;
});
csv_file = readline.createInterface({
input: file_stream
});
name = an_array[i][j][k];
found = false;
csv_file.on('line', function (line) {
if(name == line)
{
found = true;
csv_file.close();
}
});
await csv_file.on('close', function() {
console.log("Closing Stream");
if(found == false)
{
an_array[i][j][k] = "";
}
if((i == 9) && (j == 9) && (k == 9))
{
return an_array;
}
});
}
}
}
}

Resources