Created
March 9, 2021 00:57
-
-
Save pasha1110/a9cfb69753c10f531bbfc7b61d24e7f2 to your computer and use it in GitHub Desktop.
How to check array startwith or endwith
This file contains 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
//using Array.prototype | |
Array.prototype.startWith = function (query){ | |
if (query === undefined) { | |
return this[0] | |
}else { | |
if (this === []|| this.length === 0){ | |
return false | |
}else { | |
return (this[0] === query) ? true:false | |
} | |
} | |
} | |
Array.prototype.endWith = function (query) { | |
let end = this.length - 1 | |
if (query === undefined) { | |
return this[end] | |
}else { | |
if (this === [] || this.length === 0) { | |
return false | |
} | |
else { | |
return (this[end] === query) ? true:false | |
} | |
} | |
} | |
//Example usage: | |
Let car = ["Audi","Toyota","Volvo"] | |
console.log (car.startWith()) // expected Audi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment