Files
AP-Computer-Principles-Archive/problem2.js
2025-10-27 11:49:47 -05:00

102 lines
3.6 KiB
JavaScript

//Function displayList is complete
//Function displayList returns a string (a list of) of all test scores
//stored in the parameter array named scores
function displayList(scores)
{
let s = ""
for(let loc=0; loc<scores.length; loc++)
{
if(loc<scores.length-1)
s = s+ scores[loc] +", "
else
s = s+ scores[loc]
}
return s;
}
// function hasImproved() takes as parameter an array of scores named scores.
// This function returns TRUE if all scores are improving, returns FALSE otherwise.
// scores are said to be improving if all scores in the list are greater than the
// previous score. For example [50, 80, 90, 100] is improving since any given score
// in the list is greater than the previous. On the otherhand, scores [90, 95, 85, 100]
// are not improving because 85<95
function hasImproved(scores)
{
//DO NOT WRITE ANY CODE HERE
for(let i=0; i<scores.length-1; i++)//5 6 8 9 4
{
for(let j=i+1; j<scores.length-2; j++)
{
if(scores[j]<scores[i])
return false
}
}
return true
}
// function getAverage is complete.
// This function takes an array of test scores and two integers, start and end.
// getAverage returns the average of all scores starting at start and end
// at to inclusive. For example. if scores is [80, 85, 75, 90, 100, 50] and
// start=2 and end=4, getAverage will return the average of 75, 90 and 100
function getAverage(scores, start, end)
{
//DO NOT WRITE ANY CODE HERE
let sum=0;
for(let i=start; i<=end; i++)
{
sum = sum + scores[i]
}
return sum/(end-start+1)
}
// ******************************** TASK 1 ***********************************
// Function computeGrade() takes as parameters an array of student test scores
// named scores.
// This function needs to return the average of this student. The average
// needs to be computed according to the following rules:
// 1. If the student has improve, the average is computed from the scores on the
// second half of the list ONLY. If the list has odd number of scores, the extra score
// is part of the second half. For example, if the student test scores list is
// [50, 80, 85, 90, 95] the average should be calculated with scores [85, 90, 95] use LabArrays for this, and use improve function for boolean :3
// 2. If the student has not improved, the average score is calculated including
// all the test scores
function computeGrade(scores)
{
if (hasImproved(scores)) {
let temparray = scores.slice(Math.ceil(scores.length / 2), scores.length)
return getAverage(temparray, 0, temparray.length-1)
} else {
// console.log("nuh uh")
return getAverage(scores, 0, scores.length-1);
}
}
// ******************************** TASK 2 ***********************************
// Write the code to calculate the average of the following test scores and
// display the data as shown in the sample run of the program shown in the
// WORD DOCUMENT.
// [95, 100, 80, 80, 100, 80, 60, 80, 90]
// [95, 60, 80, 80, 50, 80, 60, 80, 90]
// [95, 100, 75, 80, 80, 80, 60, 80, 75]
// You MUST use the other given functions when completing this TASK
let array = [85, 90, 96, 97, 98, 100]
let array2 = [95, 60, 100, 80, 80, 85, 87, 90]
let array3 = [95, 100, 75, 80, 80, 80, 60, 80, 75]
console.log(`Test Scores: ${displayList(array)}`)
console.log(`Average = ${computeGrade(array)}`)
console.log()
console.log(`Test Scores: ${displayList(array2)}`)
console.log(`Average = ${computeGrade(array2)}`)
console.log()
console.log(`Test Scores: ${displayList(array3)}`)
console.log(`Average = ${computeGrade(array3)}`)