Last active
January 6, 2017 17:28
-
-
Save newmanbrad/64fd9f049b14c9d475fc15c61b8750dc to your computer and use it in GitHub Desktop.
Imperative Code Sample #1
This file contains hidden or 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
/** | |
Imperative style example of a program that will find the average grade for classes with | |
more than one student enrolled. | |
**/ | |
let courses = [ | |
{ name: 'functional fundamentals', enrolled: 2, grade: 100, students: 30 }, | |
{ name: 'category theory', enrolled: 2, grade: 80, students: 25 }, | |
{ name: 'basket weaving', enrolled: 1, grade: 89, students: 43 } | |
]; | |
let totalGrades = 0; | |
let totalClasses = 0; | |
let average = 0; | |
for(let i = 0; i < courses.length; i++){ | |
let course = courses[i]; | |
if (course !== null) { | |
if(course.enrolled > 1){ | |
totalGrades += course.grade; | |
totalClasses += 1; | |
} | |
} | |
} | |
averageGrade = totalGrades / totalClasses; | |
console.info('The average grade for courses with more than one person enrolled: ', averageGrade); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment