Test

by David Santiago

HTML

<script src="https://unpkg.com/[email protected]/dist/core.js"></script>
<script src="https://unpkg.com/[email protected]/dist/enzyme.js"></script>
<script src="https://unpkg.com/[email protected]/dist/prettify.js"></script>

JavaScript

const {
  core: { describe, it, expect, run },
  prettify
} = window.jestLite;

// TODO: Get the longest word from any given sentence
function getLongestWord(sentence) {
  let longestWord = "";
  let currentWord = "";
  
  for (const letter of sentence) {
    if(/^[a-zA-Z]$/.test(letter)){
      currentWord += letter;      
    } else {
      if(currentWord.length > longestWord.length){
         longestWord = currentWord;
      }
      currentWord = "";
    }
    
  }
  
  if(currentWord.length > longestWord.length){
    longestWord = currentWord;
  }
  
  return longestWord;
}


describe('get longest word in string', () => {
  it('should work on string input', () => {
    const inputs = {
      'Hello I am Totoro': 'Totoro',
      'Hello I am Chihiro': 'Chihiro',
      'Hello, my name is Oliver. I work as a coder here.': 'Oliver',
    }
    
    Object.keys(inputs).forEach(key => {
      const input = key
      const expectedResult = inputs[key]
      expect(getLongestWord(input)).toBe(expectedResult)     
    })
  })
})

prettify.toHTML(run(), document.body);