Skip to content

Instantly share code, notes, and snippets.

@eduardolundgren
Created June 21, 2013 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eduardolundgren/5834588 to your computer and use it in GitHub Desktop.
Save eduardolundgren/5834588 to your computer and use it in GitHub Desktop.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
(function() {
var p1 = "bananai",
p2 = "na",
index = -1,
i,
p2len = p2.length;
for (i = 0; i < p1.length; i++) {
if (index < 0) {
if (p1[i] === p2[0]) {
index = i;
}
}
else if ( ( ((i - index) < p2len) && (p1[i] !== p2[i - index]) ) ) {
index = -1;
i--;
}
}
console.log('index:', index);
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment