JSFiddle - React, Tailwind, and code Playground
by John Doe
HTML
<script src="https://raw.githubusercontent.com/arstgit/high-frequency-vocabulary/refs/heads/master/10k.txt"></script>
<div id="question">
Do you know following word?<br/>
<span id="word"></span>
<br/>
<button id="yes">
yes
</button>
<button id="no">
no
</button>
</div>
<div>
Vocabulary Size Estimate: <span id="estimate"></span>
</div>
<div id="output" style="background:#444;">
</div>
JavaScript
var wordlist = []
var currentWordIndex = 0
var responses = []
let wordElement = document.getElementById('word')
let estimateElement = document.getElementById('estimate')
let yesButton = document.getElementById('yes')
let noButton = document.getElementById('no')
let output = document.getElementById('output')
function init(){
yesButton.onclick = function(){
yesnoclick(true)
}
noButton.onclick = function(){
yesnoclick(false)
}
currentWordIndex = sampleIndex([])
wordElement.innerHTML = wordlist[currentWordIndex]
}
function yesnoclick(yes){
output.innerHTML = output.innerHTML + `<span style="color:${yes?'green':'red'}">${wordlist[currentWordIndex]}</span> `
responses.push({word:wordlist[currentWordIndex], wordIndex:currentWordIndex, response:yes})
currentWordIndex = sampleIndex(responses)
wordElement.innerHTML = wordlist[currentWordIndex]
estimateElement.innerHTML = updateEstimate(responses)
}
/* estimate number of words based on responses */
function updateEstimate(responses){
// for each response, get inteval length
var sortedResponses= responses.toSorted((a, b) => a.wordIndex - b.wordIndex)
console.log(sortedResponses)
let sortedIndices = sortedResponses.map(x=>x.wordIndex)
var intervalBoundaries = [0, ...sortedIndices.slice(1).map((element, index) => Math.floor((element + sortedIndices[index])/2)), wordlist.length-1]
let gaps = intervalBoundaries.slice(1).map((element, index) => element - intervalBoundaries[index])
//multiply length with response value and sum it up
console.log('gaps', gaps)
console.log('responses', sortedResponses.map(x=>x.response))
let estimate = gaps.reduce((sum, gap, index)=>sum + sortedResponses[index].response*gap, 0)
console.log('estimate', estimate)
return estimate
}
/* produce index of next word based on previous responses */
function sampleIndex(responses){
// simplest case: return Math.floor(Math.random()*wordlist.length)
//return 0
/* the following implementation...