JSFiddle - React, Tailwind, and code Playground

by namuol

HTML

<textarea id='words' placeholder='wordlist'></textarea>
<textarea id='solution' placeholder='containers' disabled='disabled'></textarea>
<br />
<button id='go'>go</button>

CSS

textarea {
    min-height: 400px;
}

CoffeeScript

String::reverse = -> @split('').reverse().join('')
String::contains = (other) -> @indexOf(other) >= 0

format = (results) ->
  output = ''
  for own word,matches of results
    output += "#{word}:\n"
    for w in matches
      output += "  #{w}\n"
  return output

$('#go').click ->
  matches = {}
  words = $('#words').val().toUpperCase().split('\n')
  words = (word.trim() for word in words)
  
  for word,i in words
    continue  if not word # Skip blanks
    console.log "#{Math.round(100*(i/words.length))}%"  if i % 100 == 0

    contained = []
    
    for otherword in words
      continue  if not otherword or (word is otherword)
      if word.contains(otherword)
        contained.push otherword
      else if word.reverse().contains(otherword)
        contained.push otherword + '*'
  
    if contained.length > 0
      matches[word] = contained

  $('#solution').text format matches