Listings 14.02 and 14.03 - A function to build a user model from user data
JS Bin
HTML:
<!-- The User constructor -->
<script src="http://output.jsbin.com/suzala.js"></script>
JS:
var buildUser = function (userData) {
var user = new User(userData.name);
userData.sessions.forEach(function (sesh) {
user.addSession(sesh.sessionDate, sesh.duration);
});
return user;
};
var userData = {
"name" : "Mahesha",
"sessions" : [
{ "sessionDate" : "2017-02-05", "duration" : 120 },
{ "sessionDate" : "2017-02-06", "duration" : 35 },
{ "sessionDate" : "2017-02-06", "duration" : 45 }
]
};
var user = buildUser(userData);
console.log(user.addSession("2017-02-15", 40));