-
Notifications
You must be signed in to change notification settings - Fork 341
Lesson Review Golf Code
Created by Rafase282 based on FCC Wiki version.
Github | FreeCodeCamp | CodePen | LinkedIn | Website | E-Mail
We will now use our knowledge about else if statements and comparison with equality, less and greater operators.
In the game of golf each hole has a par for the average number of strokes needed to sink the ball. Depending on how far above or below par your strokes are, there is a different nickname.
Your function will be passed a par and strokes. Return strings according to this table (based on order of priority - top (highest) to bottom (lowest)):
Strokes | Return |
---|---|
1 | "Hole-in-one!" |
<= par - 2 | "Eagle" |
par - 1 | "Birdie" |
par | "Par" |
par + 1 | "Bogey" |
par + 2 | "Double Bogey" |
= par + 3 | "Go Home!"
par and strokes will always be numeric and positive.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
- Waypoint: Chaining If Else Statements
- Waypoint: Comparison with the Greater Than Equal To Operator
- Waypoint: Comparison with the Less Than Equal To Operator
- Change the code below
// Only change code below this line
and above// Only change code above this line
- Take note that you are editing the inside of the
golfScore
function. - You will have to make the function return exactly the same string as shown shown on table, depending on the value of the parameters
par
andstrokes
that are passed in to your function.
-
+number -number
can be used to increase or decrease a parameter in your condition.
- You can chain else if statements to return different values in different scenarios.
- Control the flow of your function based on the tables order of priority - top (highest) to bottom (lowest) to return matching string values.
Solution ahead!
function golfScore(par, strokes) {
// Only change code below this line
if (strokes === 1){
return "Hole-in-one!";
} else if (strokes <= par - 2){
return "Eagle";
} else if (strokes === par - 1) {
return "Birdie";
} else if (strokes === par) {
return "Par";
} else if (strokes === par + 1) {
return "Bogey";
} else if (strokes === par + 2) {
return "Double Bogey";
} else if (strokes >= par + 3) {
return "Go Home!";
}
// Only change code above this line
}
// Change these values to test
golfScore(5, 4);
- Comparing the
parameters
stroke and par value to return appropriated string value. - Using else if statement for flow control
- else will return string "Go home!" to every condition where strokes are equal to par +3 or higher
Thanks for visiting, if you like this please feel free to star my repo, follow me or even contact me about contributing as it will be a lot of work and having help would be cool.
- HTML5 and CSS
- Responsive Design with Bootstrap
- Gear up for Success
- jQuery
- Basic JavaScript
- Object Oriented and Functional Programming
- Basic Algorithm Scripting
- Basic Front End Development Projects
- Intermediate Algorithm Scripting
- JSON APIs and Ajax
- Intermediate Front End Development Projects
- Claim Your Front End Development Certificate
- Upper Intermediate Algorithm Scripting
- Automated Testing and Debugging
- Advanced Algorithm Scripting
- AngularJS (Legacy Material)
- Git
- Node.js and Express.js
- MongoDB
- API Projects
- Dynamic Web Applications
- Claim Your Back End Development Certificate
- Greefield Nonprofit Project 1
- Greefield Nonprofit Project 2
- Legacy Nonprofit Project 1
- Legacy Nonprofit Project 2
- Claim your Full Stack Development Certification
- Whiteboard Coding Interview Training
- Critical Thinking Interview Training
- Mock Interview 1
- Mock Interview 2
- Mock Interview 3