Skip to content

Instantly share code, notes, and snippets.

@QuadFlask
Created April 10, 2016 04:59
Show Gist options
  • Save QuadFlask/892056fde5a9499f141a0688752b6687 to your computer and use it in GitHub Desktop.
Save QuadFlask/892056fde5a9499f141a0688752b6687 to your computer and use it in GitHub Desktop.
[CodeWars] array same structure

이미 isArray 가 존재함...;;

뭔가 너무 어렵게 풀어낸듯?

My Solution

Array.prototype.isArray =()=> toString.call(this) == '[object Array]';
Array.prototype.sameStructureAs = function (other) {
  return this.length == other.length?
    this.map((a,i)=> {
      var b = other[i];
      if (isArray(a) && isArray(b)) {
        if (a.length != b.length) return false;
        else return a.sameStructureAs(b);
      } else if (isArray(a) && !isArray(b)) return false;
      else return true;
    }).every(f=>f==true) : false;
};

역시 심플 이즈 배러

Best practice

Array.prototype.sameStructureAs = function (other) {
    return (this.length === other.length) ? this.every(function(el, i){
      return Array.isArray(el) ? el.sameStructureAs(other[i]) : true;
    }) : false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment