Skip to content

Instantly share code, notes, and snippets.

@pasha1110
Created March 9, 2021 00:57
Show Gist options
  • Save pasha1110/a9cfb69753c10f531bbfc7b61d24e7f2 to your computer and use it in GitHub Desktop.
Save pasha1110/a9cfb69753c10f531bbfc7b61d24e7f2 to your computer and use it in GitHub Desktop.
How to check array startwith or endwith
//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