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

130
Views
From the code below as per 2nd line condition - if dice === 6 code must stop ! what's the need of reassigning dice variable at line 4 ? (I'm a begnr)
let dice = Math.trunc(Math.random() * 6) + 1;
while (dice !== 6) {
   console.log(`you rolled ${dice}`);
   dice = Math.trunc(Math.random() * 6) + 1;
   if (dice === 6) {
      console.log("Game over at 6");
   }
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

At the first line dice is given a value between 1-6, if it is not 6 it goes inside the loop and tells you what you rolled. If we don't reassign the dice variable it results with dice being the same value at the first line and the code is stuck inside an infinite loop since the dice was not 6 and will never be 6. We need to reassign it so that the dice can become 6 if it was not at the first line and end the game.

PS. if the dice is 6 at the first line it wont print Game over.

about 4 years ago · Juan Pablo Isaza Report

0

On line 1 you give dice a random value between 1 and 6. On line 2 you state that while the value is not 6 you should do what's in the while loop. On line 4 you give dice a new random value between 1 and 6. Then you check if the code is 6.

Also I hope this code is very poorly written with bad understanding of programing. Don't expect to learn to code properly from the teacher that gave it to you.

Here is a better way to do it.

while true {
  let dice;
  dice = Math.trunc(Math.random() * 6) + 1;
  console.log(`you rolled ${dice}`);
  if (dice === 6) {
    console.log("Game over at 6");
    break; // this will end the while loop
  }
}
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!