Skip to content

Instantly share code, notes, and snippets.

@nathanjwtx
Last active June 28, 2017 17:08
Show Gist options
  • Save nathanjwtx/81f922e916e2073fba88c74e25c31019 to your computer and use it in GitHub Desktop.
Save nathanjwtx/81f922e916e2073fba88c74e25c31019 to your computer and use it in GitHub Desktop.
Find the common words in two strings and return an alphabetically sorted string of common words
function commonWords (first, second) {
var a = first.split(',')
var b = second.split(',')
var c = []
for (var i = 0; i < a.length; i++) {
for (var j = 0; j < b.length; j++) {
if (a[i] === b[j] && c.indexOf(a[i]) !== null) {
c.push(a[i])
}
}
}
return c.sort().toString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment