Skip to content

Instantly share code, notes, and snippets.

@rvwhitney
Last active February 15, 2021 03:39
Show Gist options
  • Save rvwhitney/c14966893b0d139bcb620bf5a3ad789b to your computer and use it in GitHub Desktop.
Save rvwhitney/c14966893b0d139bcb620bf5a3ad789b to your computer and use it in GitHub Desktop.
function like() for javascript
function like(haystack, needle) {
let n = -1;
if (typeof haystack !== 'object') {
let str = new String(haystack);
if (str !== "undefined") {
/// haystack = 'x';
/// needle = ['a','b','c','x'];
if (!Array.isArray(needle)) {
needle = [needle];
}
for (var i = 0; i < needle.length; i++) {
n = str.search(needle[i]); /// searches the whole word
if (n > -1) {
return true;
}
}
}
}
return false;
}
var str = 'apple';
if(like(str, ['ap','pe','ca'] ) === true ) {
// do something
// returns true
console.log(str)
}
if(like(str, ['al','pe','ca']) === false){
// do something
// returns true
console.log(str)
}
@rvwhitney
Copy link
Author

rvwhitney commented Feb 4, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment