Skip to content

Instantly share code, notes, and snippets.

@niniyzni
Created May 17, 2018 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niniyzni/b7fdbf21e102f2182e6ab2cb23205f13 to your computer and use it in GitHub Desktop.
Save niniyzni/b7fdbf21e102f2182e6ab2cb23205f13 to your computer and use it in GitHub Desktop.
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