Skip to content

Instantly share code, notes, and snippets.

@hassan-maavan
Created September 19, 2020 06:33
Show Gist options
  • Save hassan-maavan/083fa287cca70877a3db5c9a8d6600b4 to your computer and use it in GitHub Desktop.
Save hassan-maavan/083fa287cca70877a3db5c9a8d6600b4 to your computer and use it in GitHub Desktop.
Complete the function/method (depending on the language) to return true/True when its argument is an array that has the same nesting structures and same corresponding length of nested arrays as the first array. https://www.codewars.com/kata/520446778469526ec0000001
Array.prototype.sameStructureAs = function (other) {
Array.prototype.status = Array.prototype.status || true;
if(this.length != other.length){
Array.prototype.status = false;
return false;
}
for(var index = 0; index < this.length; index++)
{
var val = this[index];
if(isArray(val) && isArray(other[index])){
if(val.length != other[index].length){
[].sameStructureAs([1,2,3])
Array.prototype.status = false;
break;
}
else{
Array.prototype.status = true;
val.sameStructureAs(other[index]);
}
}
else if (!isArray(val) && !isArray(other[index])){
Array.prototype.status = true;
}
else{
Array.prototype.status = false;
[].sameStructureAs([1,2,3])
break;
}
}
return Array.prototype.status;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment