Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@

<body>
<h1>Section 2: JavaScript Language Basics</h1>
<script src='script.js'></script>
</body>
</html>

</html>
75 changes: 75 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const johnTeamScores = [89, 20, 103];
const mikeTeamScores = [116, 94, 123];
const johnsAvrg = calcAverage(johnTeamScores);
const mikesAvrg = calcAverage(mikeTeamScores);

const higherAverage = {
winner: 'John and Mike',
score: '',
numberOfWinners: 1,

setWinner() {

if (johnsAvrg !== mikesAvrg && johnsAvrg > mikesAvrg) {
this.winner = 'John';
this.score = johnsAvrg;
}
else if (johnsAvrg !== mikesAvrg) {
this.winner = 'Mike';
this.score = mikesAvrg;
}
else {
this.numberOfWinners = 2;
}
}
};

/**
* Determines which team has the highest average score and stores the results
*/
higherAverage.setWinner();

/**
* Calculates the average of the given array of numbers
* @param {integers[]} scores
*/
function calcAverage(scores) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello can we add some documentation for the code here and here are some examples


let sum = 0;

scores.forEach(score => {
sum += score;
});

return sum / scores.length;
}

//Extra

const maryTeamScores = [97, 134, 105];
const maryAvrg = calcAverage(maryTeamScores);

if (maryAvrg > johnsAvrg && maryAvrg > mikesAvrg) {
higherAverage.winner = 'Mary';
higherAverage.score = maryAvrg;
higherAverage.numberOfWinners = 1;
}

else if (maryAvrg > johnsAvrg && maryAvrg == mikesAvrg) {
higherAverage.winner = 'Mary and Mike';
higherAverage.numberOfWinners = 2;
}

else if (maryAvrg > mikesAvrg && maryAvrg == johnsAvrg) {
higherAverage.winner = 'Mary and John';
higherAverage.numberOfWinners = 2;
}

else if (maryAvrg === mikesAvrg && maryAvrg === johnsAvrg) {
higherAverage.winner = 'Mary, John and Mike';
higherAverage.numberOfWinners = 3;
}

const highestScore = higherAverage.numberOfWinners === 1 ? `${higherAverage.winner}\'s team has the highest average score of ${higherAverage.score}` : `${higherAverage.winner} have the same score`;

console.log(highestScore);