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

99
Views
sea battle game / prevent typing the same thing twice

such a problem is that if I enter the same number 3 times and it counts for me as a win, but I need to prohibit it so that I cannot enter the same number a second time, can you tell me how to do this?

let randomLocation = Math.floor(Math.random() * 6)
let location1 = randomLocation
let location2 = location1 + 1
let location3 = location2 + 1
let guess
let hits = 0
let guesses = 0
let isSunk = false

while (isSunk == false) {
  guess = prompt('Enter the coordonation: ')
  let secondTime = guess
  if (guess < 0 || guess > 6) {
    alert('Your number is incorrect')
  } else {
    guesses++
    if (guess == location1 || guess == location2 || guess == location3) {
      alert('SHOT')
      hits++


      if (hits == 3) {
        isSunk = true
        alert('The ship was wrecked')
      }
    } else {
      alert('you missed')
    }
  }
}
let stats = 'You wrecked from ' + guesses + ' attempts ' + ' which means you have statistics ' + (3 / guesses)
console.log(stats)

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

0

Here's one way to check to see if the user has entered the same thing before. Use a Set.

let answers = new Set();
while(true) {
  let input = prompt('Enter something')
  if (input == null) break;
  if (answers.has(input)) {
    alert('You already entered that.');
  } else {
    alert('That is new!')
    answers.add(input);  // Remember this input for later.
  }
}

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!