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

53 lines
1.4 KiB
JavaScript

// Function computeAverage() is complete. This function
// takes an array of test named scores and two integers, from and to.
// computeScores returns the average of all the scores starting at
// from and ending at to, inclusive
function computeScores(scores, from, to)
{
//DO NOT WRITE ANY CODE HERE
let sum = 0;
let average =0;
for(let i=from; i<=to; i++)
{
sum = sum + scores[i]
}
average = sum/(to-from+1)
return average
}
// Function getRange() is complete. This function
// takes an array of test scores and two integers, from and to.
// getRange returns a string consisting of all the scores from scores
// starting at from and ending at to, inclusive separated by a space
function getRange(scores, from, to)
{
//DO NOT WRITE ANY CODE HERE
let s="";
for(let i=from; i<=to; i++)
{
s = s + scores[i] +" "
}
return s
}
// ======= Your task is specified in the WORD DOCUMENT ================
let scores = [95, 100,80, 80, 100, 80, 60, 80, 90]
let scores2 = [95, 60, 80, 80, 50, 80, 60, 80, 90]
let scores3 = [95, 100, 75, 80, 80, 80, 60, 80, 75]
console.log(`Scores: ${getRange(scores, 2, 5)}`)
console.log(`Average:${computeScores(scores, 2, 5)}`)
console.log()
console.log(`Scores: ${getRange(scores2, 4, 8)}`)
console.log(`Average:${computeScores(scores2, 4, 8)}`)
console.log()
console.log(`Scores: ${getRange(scores3, 0, 8)}`)
console.log(`Average:${computeScores(scores3, 0, 8)}`)