Why the value I pass to a function behaves like a pointer?

I have this function : static calculatePrice(offer) { if (typeof (offer.TakerGets || offer.taker_gets) == "string") offer.quality = 1 / offer.quality; return offer.quality / 1000000; } I don’t understand why the function modifies the value that I pass to it (this.book[currency][side][this.j[currency][side]]). It does not modify a copy but the original as if I had passed it… Read More Why the value I pass to a function behaves like a pointer?

parseInt output wrong

console.log(parseInt("110000111010000011100101010001010101000100000000000000000001111", 2)); The output result was wrong, even it is in google browser console. The correct output is 7048259459772055567 I think the number is too big so nodejs cannot solve it? >Solution : Indeed, that number is much larger than Number.MAX_SAFE_INTEGER, so it cannot be accurately represented. You can use BigInts to work with numbers… Read More parseInt output wrong

Using promises stops variable being set

The following code sets the row.already_messaged_clients absolutely fine and is accessible outside of the loop. businesses.forEach(async function(row) { // let already_messaged_clients_arr = await getAlreadyMessagedClientsForPlaceID(row.place_id); row.already_messaged_clients = 3; }); When I use an await like this: businesses.forEach(async function(row) { let already_messaged_clients_arr = await getAlreadyMessagedClientsForPlaceID(row.place_id); row.already_messaged_clients = 3; }); row.already_messaged_clients does not get set. Why is this?… Read More Using promises stops variable being set

Display rounded number in a span in JS

A tool that I am using is displaying a room temperature on a smart mirror. This line of code is creating the temperature value: var temperatureTextWrapper = document.createTextNode( zone.state.sensorDataPoints.insideTemperature.celsius + "°" ); After this the var is just being appended to an existing span. By default, the value contains two decimal places eg. 25.76°C. However… Read More Display rounded number in a span in JS

The try/catch block fails to catch socket connection exception

I am trying to connect to a socket that DOES NOT exist for testing purposes. In such case, an exception is thrown and I expect it to be caught in the following try/catch block. createConnection is imported from the net package. try { createConnection(8000, "127.0.0.1") } catch(exception) { console.log("FAILED") } However, the try/catch fails and… Read More The try/catch block fails to catch socket connection exception

global variable in Node.js: weird behaviour

I have below couple of files of javascript: const { testFunc2 } = require("./TestFunction"); function home(val){ global.context={}; global.context.val = val; } home(3); testFunc2(); //homeFunction.js Below is TestFunction.js: exports.testFunc2=()=>{ console.log(context.val); } This code prints 3 as the output, even though I have not put global before context in testFunc2. How does this actually works? My guess… Read More global variable in Node.js: weird behaviour

Javascript – single line reduce statement not working becoz of push statement

// only have numbers in array till n times function deleteNth(arr,n){ // … function count(arr1, num){ console.log(‘arr1’, arr1) return arr1.reduce((acc, val) => (val === num) ? ++acc : acc, 0); } // return arr.reduce((acc1, val1) => { // if (count(acc1, val1) < n){ // acc1.push(val1); // } // else{ // acc1; // } // return… Read More Javascript – single line reduce statement not working becoz of push statement

How to return final result using recursion in nodejs

I’m working on nodejs. Currently I’m doing the recursive part of my work. In prodHelper.js product: (data, totalUser) => { return new Promise(async (resolve, reject) => { // data = {total: 5, order: 6, status: active} -> As a dynamic data let number = 1 let res = await prodHelper.groupProduct(data, totalUser, number) console.log(‘RESULT’, res) //… Read More How to return final result using recursion in nodejs