Created
May 17, 2018 17:54
-
-
Save niniyzni/b7fdbf21e102f2182e6ab2cb23205f13 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Employee = function(fName, lName, project) { | |
this.firstName = fName; | |
this.lastName = lName; | |
this.project = project; | |
this.getFullName = function() { | |
return this.lastName + ", " + this.firstName; | |
} | |
}; | |
Employee.prototype.managerId = 123; | |
Employee.prototype.sayHello = function() { | |
console.log("Hello"); | |
}; | |
// --------------------------------------------- | |
function convertToString(Employee) { | |
var data = ""; | |
for (let empdata in Employee) { | |
if (Employee.hasOwnProperty(empdata) && !isFunction(Employee[empdata])) { | |
if (isObject(Employee[empdata])) { | |
// console.log(JSON.stringify(Employee[empdata])); | |
data += convertToString(Employee[empdata]); | |
} else { | |
data += Employee[empdata] + ", "; | |
} | |
} | |
} | |
return data; | |
// console.log("data--->", data); | |
} | |
function isFunction(property) { | |
return property && {}.toString.call(property) === '[object Function]'; | |
} | |
function isObject(property) { | |
return property && {}.toString.call(property) === '[object Object]'; | |
} | |
// --------------------------------------------- | |
/** | |
* Returns true if all tests pass; otherwise, returns false. | |
*/ | |
function doTestsPass(testCases) { | |
// todo: add more test cases | |
return testCases.reduce((result, testCase) => { | |
const answer = convertToString(testCase.inputs); | |
console.info("expected: ", testCase.expected, ", answer: ", answer); | |
return result && (answer === testCase.expected); | |
}, true); | |
} | |
const testCases = [{ | |
expected: "Frank, Sinatra, 1, Trader Platform, ", | |
inputs: new Employee("Frank", "Sinatra", { | |
id: 1, | |
name: "Trader Platform" | |
}) | |
}]; | |
/** | |
* Main execution entry. | |
*/ | |
if (doTestsPass(testCases)) { | |
console.log("All tests pass!"); | |
} else { | |
console.log("There are test failures."); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment