Created
October 24, 2020 11:32
-
-
Save RaynZayd/5f2a50c135359d4d5ce5a7922d49723c to your computer and use it in GitHub Desktop.
Training days is a service program that sends you a message for the event you signed up for and the days you have left to train. In this project I made Training Days more maintainable and less error-prone by fixing variable scopes.
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
/* | |
Training days is a service program that sends you a message for the event you signed up for and the days you have left to train. | |
In this project I made Training Days more maintainable and less error-prone by fixing variable scopes. | |
*/ | |
// The scope of `random` is too loose | |
//const random = Math.floor(Math.random() * 3);//defined with global scope | |
const getRandEvent = () => { | |
const random = Math.floor(Math.random() * 3); | |
if (random === 0) { | |
return 'Marathon'; | |
} else if (random === 1) { | |
return 'Triathlon'; | |
} else if (random === 2) { | |
return 'Pentathlon'; | |
} | |
}; | |
// The scope of `days` is too tight | |
const getTrainingDays = event => { | |
var days; | |
if (event === 'Marathon') { | |
days = 50; | |
} else if (event === 'Triathlon') { | |
days = 100; | |
} else if (event === 'Pentathlon') { | |
days = 200; | |
} | |
return days; | |
}; | |
// The scope of `name` is too tight | |
name = 'Nala'; | |
const logEvent = (name,event) => { | |
console.log(`${name}'s event is: ${event}`); | |
}; | |
const logTime = (name,days) => { | |
console.log(`${name}'s time to train is: ${days} days`); | |
}; | |
const event = getRandEvent(); | |
const days = getTrainingDays(event); | |
// Define a `name` variable. Use it as an argument after updating logEvent and logTime | |
//Functions for another competitor. | |
logEvent(name,event); | |
logTime(name,days); | |
const event2 = getRandEvent(); | |
const days2 = getTrainingDays(event2); | |
const name2 = 'Warren'; | |
logEvent(name2, event2); | |
logTime(name2, days2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment