46 lines
1.6 KiB
TypeScript
46 lines
1.6 KiB
TypeScript
|
|
|
|
// 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:number[], from: number, to:number): number {
|
|
//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: number[], from: number, to:number): string {
|
|
//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 ================
|
|
|
|
console.log(`Scores: ${getRange([95, 100,80, 80, 100, 80, 60, 80, 90], 2, 5)}`)
|
|
console.log(`Average:${computeScores([95, 100,80, 80, 100, 80, 60, 80, 90], 2, 5)}`)
|
|
console.log()
|
|
console.log(`Scores: ${getRange( [95, 60, 80, 80, 50, 80, 60, 80, 90], 4, 8)}`)
|
|
console.log(`Average:${computeScores( [95, 60, 80, 80, 50, 80, 60, 80, 90], 4, 8)}`)
|
|
console.log()
|
|
console.log(`Scores: ${getRange([95, 100, 75, 80, 80, 80, 60, 80, 75], 0, 8)}`)
|
|
console.log(`Average:${computeScores([95, 100, 75, 80, 80, 80, 60, 80, 75], 0, 8)}`) |