Naive String Search with and without Two Loops in Ruby and JavaScript

def naiveStringSearch(string, frag)  count = 0  s = string.length - 1  f = frag.length - 1  for i in 0..s do    for j in 0..f do      if frag[j] != string[i+j]        break      elsif j==f        count += 1      end     end  end  return countend
function naiveStringSearchFaster(string, frag) {     let count = 0   for(let i=0; i < string.length - frag.length + 1; i++) {       let check = string.slice(i, i + frag.length)       if (check===frag) {          count++       }    }   return count}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Anthony Lepore

Anthony Lepore

136 Followers

Composer, playwright, designer for theater, jazz musician, philosopher, software engineer and technical writer for a FinTech firm in NYC. More: ViolaCello93.com