Skip to content

Instantly share code, notes, and snippets.

@railsstudent
Created December 23, 2016 16:20
Show Gist options
  • Save railsstudent/230b76b48628be47639adaa29b5a8c92 to your computer and use it in GitHub Desktop.
Save railsstudent/230b76b48628be47639adaa29b5a8c92 to your computer and use it in GitHub Desktop.
Array.prototype.sameStructureAs = function (other) {
// Return 'true' if and only if 'other' has the same
// nesting structure as 'this'.
// Note: You are given a function isArray(o) that returns
// whether its argument is an array.
if (this.length !== other.length) {
return false;
}
for (var i = 0; i < this.length; i++) {
if (isArray(this[i]) && isArray(other[i])) {
if (!this[i].sameStructureAs(other[i])) { return false; }
} else if (!isArray(this[i]) && isArray(other[i])) {
return false;
} else if (isArray(this[i]) && !isArray(other[i])) {
return false;
}
}
return true
};
@badouine
Copy link

badouine commented Sep 2, 2021

great !

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