Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Follow along as we translate pseudocode to functioning JavaScript code.
Finished working code
// function calculate_gpa
// pass in student_grades
function calculate_gpa(student_grades) {
// set grade_total to 0
let grade_total = 0;
// for each grade in student_grades
let num_of_grades = student_grades.length;
for (let i = 0; i<num_of_grades; i++) {
let grade = student_grades[i];
// if grade is not a 1, 2, 3, or 4
if (grade < 1 || grade > 4) {
// print "invalid grade" with grade
console.log("invalid grade: " + grade);
// exit function with "can't complete calculation" message
return "Can't complete calculation";
} else {
// else add grade to grade_total
grade_total += student_grades[i];
}
}
// set gpa to grade_total / number of grades
const gpa = grade_total / num_of_grades;
// return gpa
return gpa;
// end function
}
// set reggie_grades to 4, 4, 3, 4
let reggie_grades = [4,4,3,4];
// print the results of calling calculate_gpa with reggie_grades
console.log(calculate_gpa(reggie_grades));
// set dave_grades to 1, 2, 3, 2
let dave_grades = [1,2,3,2]
// print the results of calling calculate_gpa with dave_grades
console.log(calculate_gpa(dave_grades));
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
[MUSIC]
0:00
Hopefully you tried to solve
this challenge yourself,
0:09
based on the previous
step in this workshop.
0:12
If not, you can follow along with me.
0:14
I'll translate this
pseudocode into working code.
0:16
I'll use JavaScript in this example.
0:19
But pseudocode is simply
the outline of a program steps.
0:21
So it can be translated to
any programming language.
0:24
Open the workspace associated with
this video in the gpa.js file.
0:28
This is a JavaScript file, and
0:33
you'll notice that it has the pseudo
code added as code comments.
0:34
First, I'll create the function
structure with opening function name,
0:38
calculated GPA in a parameter
named student_grades.
0:43
A closing brace marks
the end of a function.
0:47
Now I'll create a new variable to
0:53
store the number of grades
passed into the function.
0:56
I'll call it grade_total and
set the initial value to 0.
1:00
This value will change as the loop
indicated here in the pseudocode runs.
1:06
We're using JavaScript here, so the grades
will be kept in an array that are stored
1:11
in the parameter student_grades.
1:16
To get the total number of items in
an array, you use the length property.
1:18
I'll use a for loop here,
1:23
but there are many ways to iterate
through items in an array in JavaScript.
1:25
You may have come up with a totally
different approach and that's fine.
1:29
Now within the loop, we need to
test the values to make sure they
1:32
fall within the range
of allowable numbers.
1:35
I'll create a new variable and
store the current grade from the array.
1:38
Remember, you use a numeric index
to access an item in an array.
1:43
So in this case, the first time
through the loop, the value of i is 0.
1:47
So student_grades 0 would be
the first number in the array.
1:52
I'll start with a simple condition that
looks at the current grade in the loop.
2:03
If the value is below 1 or
above 4, then it's invalid and
2:08
will output some messages to the console.
2:12
We'll print the text invalid
grade plus the grade and
2:18
then we'll return can't complete
calculation and that exits the function.
2:21
If the grade is valid,
then it's added to the total.
2:30
This loop will run once for each grade
in the array passed to the function
2:38
Once the loop is done, We'll calculate
the grade point average, which
2:47
is the total of all of the grades divided
of the number of grades in the array.
2:54
We return that value which
exists the function.
3:00
Now, a function doesn't do
anything until its called.
3:04
So let me create an array of grades.
3:08
Then we can log the results of
calling that function to the console.
3:19
I can see if the program works by
going into the console here and
3:23
using node js to run the program.
3:27
First, let me save this file.
3:30
Go to the Console and type node space and
3:32
then the name of the file gpa.js.
3:37
If you see the number 3.75,
then it's working.
3:41
Let's see what happens if I
change one of the grades to -2.
3:45
You can now see the error messages
because that grade isn't valid.
3:56
You can finish off the requirements
simply by copying and
4:01
pasting this code down here and
making the changes that are necessary.
4:05
I'll let you do that.
4:09
One thing is after I've
written my program,
4:11
I don't need the pseudocode anymore,
4:19
so I can go through and clean it up.
4:26
I'll save this in it one more time
just to make sure it's working
4:45
And there you have it,
from pseudocode to JavaScript.
5:01
You can find my finished working code
listed in the teachers notes below,
5:04
as well as a link to other
pseudocode resources.
5:08
Have fun programming.
5:11
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up