Question
Add a nested switch statement to the change option that allows the user to change ANY of the current trackerService properties ( allow them to
Add a nested switch statement to the change option that allows the user to change ANY of the current trackerService properties ( allow them to change exercise, weight, distance, or time) and recalculates the speed and/or calories depending on which needs to be updated. Only recalculate what needs to be recalculated.
The tracker module includes setters for each propery, and each setter already emits events that signal the change. Note that events for time and distance also emit parameters (but you don't need them for this activity).
In the main activityTracker.js file, follow the same strategy that was used for changing the exercise (lines 55-58)( i have specified the lines) for changing the other tracker properties.
To do this, you'll need to add handlers for each event (using either the .on or the .addListener functions) and you'll need to be able to write and pass
some callback functions as the handlers.
activityTracker.js
var response = function(){
reader.question('What actvity did you perform? (Walking/Running/Swimming)', act => {
reader.question('For how long? (in minutes)', time =>{
reader.question('How far? (in miles)', distance => {
reader.question('What is your weight today? (in pounds)', weight =>
{
//Notice that current scope is still able to
//capture data provided in previous callbacks.
console.log(act);
var current = new tracker(act.trim(),weight, distance, time);
console.log(`Calories Burned: ${current.calculate()}`);
//"every time you catch the exerciseChanged event, run this function
current.on('exerciseChanged', () => { //line 55
console.log("Exercise Changed!"); //line 56
console.log(`Calories Burned: ${current.calculate()}`); //line57
}); //line 58
reader.question('Do you want to (s)tart over, (c)hange your current entry, or exit?', answer=>{
var letter = answer.toLowerCase()[0];
switch (letter){
case "s":
response();//recall this function and start over again
//Note that I call the function inside the function,
//similar to recursion
break;
case "c":
//change the current activity
//Your job: add some code that will allow a user to change any member of the
//tracker. Add some cases too. You'll need to add listeners that are listening
//for the custom tracker events (provided for you).
reader.question('What is your new activity (Swimming/Walking/Running)', rep=>{
current.setExercise(rep); //setExercise emits the event 'exerciseChanged'
process.exit(0); // see the Exercise module for more info
});
break; //Without break, this falls through and exits BEFORE setExercise completes
default:
console.log("Bye!");
process.exit(0);
}
})
})
})
})
})
}
TrackerService Properties:
var tracker = function(exercise, weight, distance, time) {
try{
this.exercise=new Exercise(exercise);
this.weight = Number(weight);
this.distance = Number(distance);
this.time = Number(time);
events.EventEmitter.call(this);
} catch (err){
console.log("Error recieved during service creation");
throw err;
}
//updated to accomodate swimming
this.calculate = function() {
return this.exercise.calculate(this.weight, this.distance, this.time);
};
//speed is consistently calculated for all exercise times (distance/time)
this.calcSpeed = function(){
return this.distance/(this.time/60);//miles per hour
};
this.setExercise = function(exercise){
this.exercise=new Exercise(exercise);
this.emit('exerciseChanged');
};
this.setWeight =function(weight){
this.weight=weight;
this.emit('weightChanged');
};
this.setTime = function(time){
this.time=time;
this.emit('timeChanged', time);
};
this.setDistance = function(distance){
this.distance=distance;
this.emit('distanceChanged', distance);
};
};
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started