Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

364
Views
Cómo guardar un número aleatorio, solo una vez, en Javascript

Traté de guardar un número aleatorio, solo una vez en J.

 let Machine = { position : 0, num_move : 0, isEmpty : true, memory : [], start : function () { for (let index = 0; index < 9; index++) { this.memory.push({move:undefined,isEmpty: true}) } }, generateMove:function () { let num = Math.floor((Math.random()*9)+1) if (num>=10) { num-=1 } return num }, // Update move move : function () { let move = this.generateMove() let prev = this.num_move if (!this.memory[this.num_move] && this.num_move<=0) { this.memory[this.num_move]=move this.num_move+=1 } }, //Return data values data : function () { return { memory : this.memory, num_move : this.num_move } } }

El problema es que Javascript guarda un nuevo número aleatorio, pero solo quiero guardarlo una vez. como un recuerdo

 Machine.move() //First time console.log(Machine.data()) // {memory : [2], num_move : 1} Machine.move() //Second time console.log(Mahine.data()) // {memory : [9], num_move : 1}

También intenté inicializar el objeto de la máquina con el método de inicio. Y cambie el método de movimiento como este. Para verificar si está vacío, antes de empujar la memoria

 move : function () { let move = this.generateMove() if (this.memory[this.num_move].isEmpty) { this.memory[this.num_move].isEmpty = false this.memory[this.num_move].move=move this.num_move+=1 } }

pero no funciona JS genera un nuevo número aleatorio cada vez que llamo desde la consola.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Lo intento y guardo el objeto y obtuve el mismo resultado en ambos turnos.

 let Machine = { position : 0, num_move : 0, isEmpty : true, memory : [], start : function () { for (let index = 0; index < 9; index++) { this.memory.push({move:undefined,isEmpty: true}) } }, generateMove:function () { let num = Math.floor((Math.random()*9)+1) if (num>=10) { num-=1 } return num }, // Update move move : function () { let move = this.generateMove() let prev = this.num_move if (!this.memory[this.num_move] && this.num_move<=0) { this.memory[this.num_move]=move this.num_move+=1 } }, //Return data values data : function () { return { memory : this.memory, num_move : this.num_move } } } var obj = Object.create(Machine); obj.move(); console.log(obj.data()); // {memory : [2], num_move : 1} obj.move(); console.log(obj.data()); // {memory : [2], num_move : 1}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!