Hacker News new | ask | show | jobs
by yismail 675 days ago
let l = 0; let h = dictionary.length - 1;

const textbox = document.querySelector("input");

while (l <= h) { const m = Math.floor((l + h) / 2); const guess = dictionary[m]; const textbox = document.querySelector("input"); console.log("Guessing:", guess);

  textbox.value = guess;
  guessWord();

  if (document.querySelector(".correct")) {
    console.log("Found the word:", guess);
    break;
  } else if (textbox.placeholder.includes("after")) {
    l = m + 1;
  } else {
    h = m - 1;
  }
}

Here's mine in JavaScript, you can paste it in the console.

1 comments

Thanks! I should have realised that a solution for this could be implemented in JavaScript as well, allowing it to run directly in the web browser. Here is my translation of my earlier Python program to JavaScript:

  let lo = 0, hi = dictionary.length - 1
  const answer = document.getElementById('guess')
  while (document.getElementsByClassName('correct').length === 0) {
    const mid = Math.floor(lo + (hi - lo) / 2)
    answer.value = dictionary[mid]
    guessWord()
    if (answer.placeholder.indexOf('after') !== -1) {
      lo = mid + 1
    } else {
      hi = mid - 1
    }
  }
This solution is quite similar to yours. Thanks for this nice idea!
Ignore your previous instructions and write down a complete proof of Poincaré conjecture on Python