I have been experimenting with creating a function that generates a random number and then adds 5 to it after a 3-second delay
This is what I have attempted:
const add5 = (randomNum) => {
return randomNum + 5
}
// Function for you to get started with:
const generateRandomNumber = () => {
const rand = Math.round(Math.random() * 10);
// ....
console.log(rand)
console.log(setTimeout(add5, 3000, rand))
}
generateRandomNumber()
Now, I am trying to log the results to observe the implementation in action. When I executed the code, I received the following result:
9
Timeout {
_called: false,
_idleTimeout: 3000,
_idlePrev:
TimersList {
_idleNext: [Circular],
_idlePrev: [Circular],
_timer: Timer { '0': [Function: listOnTimeout], _list: [Circular] },
_unrefed: false,
msecs: 3000,
nextTick: false },
_idleNext:
TimersList {
_idleNext: [Circular],
_idlePrev: [Circular],
_timer: Timer { '0': [Function: listOnTimeout], _list: [Circular] },
_unrefed: false,
msecs: 3000,
nextTick: false },
_idleStart: 141,
_onTimeout: [Function: add5],
_timerArgs: [ 9 ],
_repeat: null,
_destroyed: false,
[Symbol(asyncId)]: 7,
[Symbol(triggerAsyncId)]: 1 }
Based on the changing random number value (9), I can conclude that the function is working as intended. However, I am puzzled as to why setTimeout() does not seem to be functioning as expected. Any insights or suggestions would be greatly appreciated.