Last active
February 15, 2021 03:39
-
-
Save rvwhitney/c14966893b0d139bcb620bf5a3ad789b to your computer and use it in GitHub Desktop.
function like() for javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fiddle