Home > Blockchain >  Reusing the already randomly generated number in another JS file - Node.js (ESM not CJS)
Reusing the already randomly generated number in another JS file - Node.js (ESM not CJS)

Time:01-25

I need help with my project. I am creating CLI game with Node.js using ESM (not CJS).

In my randomHealth.js file, I have a function that generates a random number:

let randomHealth = () => {
  return Math.floor(Math.random() * 300   1)
}

export default randomHealth

In my stats.js file I need to import and display that number. However, I needs to be fixed there, because I need that same number in another file:

import random from "./random/random.js"
import randomHealth from "./random/randomHealth.js"

let stats = () => {
  console.log("STATS")

  let playerHealth = randomHealth()
  console.log("Health - "   playerHealth)
}

export default stats

I stored it in a playerHealth variable because I need to take that one generated number.

Now, I need that variable in another file, fight.js to use it. If I call randomHealth() it's just gonna generate a new number, and I need a number that was already generated in stats.js file. How to get that number from stats.js to use it in fight.js ?

I tried exporting that variable and importing it in fight.js, but once I do console.log() it displays undefined. I tried many different things but none worked. It returns either undefined, either NaN either nothing. I also tried immediatelly storing it in a variable in randomHealth.js and then displaying it, but that didn't work either. If I do console.log in randomHealth.js then I need to do it again when I want to display it in another file and a double console.log displays undefined. And if I don't to console.log in randomHealth.js then it always regenerates a new random number and I need the number that gets displayed in stats.js to use in fight.js.

Any ideas ? This was just my idea, if there is an even better way of doing it that storing it in a varible in stats.js then by all means tell me.

Thanks in advance.

CodePudding user response:

Your problem is that by calling randomHealth() every time, you redefine the health points. In order to patch it, I recommend that you store the values outside the function and export them.

stats.js :

import randomHealth from './randomHealth.js'

export var healthPlayer1 = randomHealth();
export var healthPlayer2 = randomHealth();
export var healthPlayer3 = randomHealth();

export let stats = () => {
    console.log("STATS")
    console.log("Player 1 - "   healthPlayer1)
    console.log("Player 2 - "   healthPlayer2)
    console.log("Player 3 - "   healthPlayer3)
}

main.js

import { stats } from './stats.js';

import { healthPlayer1, healthPlayer2, healthPlayer3 } from './stats.js';

stats(); 
// Player 1 - 211
// Player 2 - 98
// Player 3 - 240


console.log(healthPlayer1);// 211
console.log(healthPlayer2);// 98
console.log(healthPlayer3);// 240

Now you can just use the exported HP, maybe you should create functions that will modify the HP in the same script so you can centralize all related functions in the same script, making it more clean and easy to use.

Hope it helped you out

CodePudding user response:

Try exporting the result of the randomHealth function:

const randomHealth = () => {
  return Math.floor(Math.random() * 300   1)
}

export default randomHealth()
  • Related