benedictify
9/13/2017 - 3:24 AM

Spidergap question 1

Spidergap question 1

// Spidergap Coding questions
// Benedict Schurwanz
// github.com/benedictify

// Question 1: deepClone

// The function "deepClone" takes an object as input and returns a copy of that object.
// It assumes that parameters are objects.

// This function takes as input an object, creates a blank object, and then 
// iterates through each element, checking if that element is itself a nested
// object. If so, the function calls itself recursively, and then returns the
// result to be added as the next element in the clone. If a given element
// is not an object, it copies that element over to the clone. Then it returns
// the completed clone object. In the inner function call, that call would 
// return the clone of the inner object, to be inserted in the outer object. 

function deepClone(original) { 
  var clone = {};

  for (var property in original) {
    if (typeof property === 'Object') {
      var next = deepClone(property);
    } else {
      var next = property; 
    }

    clone[property] = original[next];
  }

  return clone;
}