Final Exam Practice Part II - Complete the Code
1) Complete the program segment below. The program asks the user to enter the name and grade point average of three students, then prints out the name and average of the student with the highest grade.
// Below are all the variables you need. DO NOT MAKE ANY NEW VARIABLES!
var name1, name2, name3, g1, g2, g3;
var topName, topGrade;
name1 = prompt("Enter first student's name:","");
g1 = prompt("Enter first student's grade point average:","”);
name2 = prompt("Enter second student's name:","");
g2 = prompt("Enter second student's grade point average:","");
name3 = prompt("Enter third student's name:","");
g3 = prompt("Enter third student's grade point average:","");
// Complete the code below to determine which student has the highest grade.
// You can assume that no students have the same grade.
______
______
______
______
______
______
______
______
______
______
______
______
______
// The top name and grade are printed out.
document.write(topName + " has the highest grade of " + topGrade);
2) This program prompts the user to enter words one at a time until the number of odd words is greater than 3. Odd words are defined as words with an odd number of letters.
For example: "pizza" is an odd word (it has an odd number of letters).
"haas" is not an odd word (it has an even number of letters)
The program then prints out all of the odd words.
Example:
Enter Word à soup
Enter Word à pie
Enter Word à milk
Enter Word à pizza
Enter Word à spam
Enter Word à gum
Enter Word à grape
The computer then prints à The odd words are: pie pizza gum grape
3) Complete the program segment to find the Greatest Common Factor (GCF) of 2 numbers. The GCF is the largest number that divides evenly into both of the numbers. Examples: the GCF of 10 and 15 is 5, the GCF of 20 and 16 is 4. You can assume that the user will enter only positive whole numbers.
// below is some code to get you started
var num1 = Number(prompt("Enter first number",""));
var num2 = Number(prompt("Enter second number",""));
4) Complete the program segment to write a word triangle. A word triangle is when the first letter of a word is written on the first line, the first 2 letters is written on the second line, the first 3 letters is written on the third line, and so on until the entire word is written out.
Example The word is: “pizza!”
The output would be: p
pi
piz
pizz
pizza
pizza!
// complete the code to write a word triangle
var word = prompt("Enter a word", "pizza!")
5) Complete the program segment to replace the blanks in a given sentence with the underscore character. The sentence: “I like pizza!” becomes “I_like_pizza!”.
// complete the code
var sentence = prompt("Enter a sentence", "I like pizza!")
var newSentence = "";
document.write(newSentence);
6) Complete the program below to find the sum of the positive integers from a given number down to 1.
For example if the input is 5 your program should write 5 + 4 + 3 + 2 + 1 = 15
// complete the code
var number = Number(prompt("Enter number",""));
var sum;
var output;
document.write(output + " = " + sum);
ANSWER TO TOP GRADE
if (g1 > g2 & g1 > g3)
{
topName = name1;
topGrade = g1;
}
else if (g2 > g1 & g2 > g3)
{
topName = name2;
topGrade = g2;
}
else
{
topName = name3;
topGrade = g3;
}
ANSWER TO ODD WORD COUNTER
var count = 0;
var oddWords = "";
while(count <= 3)
{
word = prompt("Enter Word","");
if (word.length % 2 != 0)
{
oddWords = oddWords + " " + word;
count++;
}
}
document.write("The odd words are: " + oddWords);
ANSWER TO GCF
var num1 = Number(prompt("Enter first number",""));
var num2 = Number(prompt("Enter second number",""));
var gcf = 1;
for (x = 1; x < num1; x++)
{
if(num1%x==0 & num2%x==0)
{
gcf = x;
}
}
ANSWER TO WORD TRIANGLE
var word = prompt("Enter a word", "pizza!")
for (i = 0; i < word.length; i++)
{
document.write("<br>" + word.substring(0,i+1));
}
ANSWER TO REPLACE BLANKS WITH UNDERSCORES
var sentence = prompt("Enter a sentence", "I like pizza!")
var newSentence = "";
for (x = 0; x < sentence.length; x++)
{
if(sentence.substring(x,x+1) == " ")
{
newSentence = newSentence + "_";
}
else
{
newSentence = newSentence + sentence.substring(x,x+1);
}
}
document.write(newSentence);
ANSWER TO SUM NUMBERS DOWN TO 1
var number = Number(prompt("Enter number",""));
var sum = number;
var output = number;
for (x = number-1; x>0; x--) {
sum = sum + x;
output = output + " + " + x;
}
document.write(output + " = " + sum);