Duke and North Carolina have a famous basketball rivalry dating back to 1920. Th
ID: 3809629 • Letter: D
Question
Duke and North Carolina have a famous basketball rivalry dating back to 1920. The number of points each team has scored in head-to-head competition over the past five years is provided in the duke and nc arrays. Ex: North Carolina won the most recent game 76-72 since duke[0] is 72 and nc[0] is 76.
1.)Write a for loop that examines the dukeScores and ncScores arrays and places "D" in the winningTeam array if Duke won or "N" if North Carolina won, for every game. Ex: winningTeam[0] should be "N" because North Carolina won 76-72, and winningTeam[1]should be "D" because Duke won 74-73.
2.)Display the contents of the winningTeam array using console.log(winningTeam);.
3.)Write a forEach loop that examines the winningTeam array and determines the longest streak of Duke wins. Display the longest streak to the console, which should be 4.
To determine the longest streak, use two variables initialized to 0: streak and longestStreak. Loop through the winningTeam array and increment streak every time a "D" appears in the array. When an "N" is encountered, set longestStreak to streak if streak is larger and reset streak back to 0. When the loop terminates, longestStreak will contain the longest streak.
var dukeScores = [72, 74, 84, 92, 93, 66, 69, 73, 70, 85, 75, 67, 79]; var ncScores = [76, 73, 77, 90, 81, 74, 53, 68, 88, 84, 58, 81, 73]; var winningTeam = [];//Who won the last meeting? if (dukeScores [0] > ncScores[0]) {console.log (Duke won " + dukeScores[0] + "-" + ncScores [0] + ".");} else {console.log ("North Carolina won " + ncScores [0] + "-" + dukeScores [0] + ".");}Explanation / Answer
Answer -> i wrote this javascript program according to the question requirements .
-------------------------------------------------------------------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<body>
<p id="p_id"></p>
<p id="r_id"></p>
<p id="q_id"></p>
<script>
var winningTeam = new Array();
var streak=0;
var longestStreak=0;
var dukeScores = [72,74,84,92,93,66,69,73,70,85,75,67,79]; /*dukeScores array*/
var ncScores = [76,73,77,90,81,74,53,68,88,84,58,81,73]; /*ncScores array*/
for (i = 0; i < dukeScores.length; i++) {
if(dukeScores[i]<ncScores[i])
{
winningTeam[i]= "N";
}
else if(dukeScores[i] > ncScores[i])
{
winningTeam[i]="D";
}
}
console.log(winningTeam);
document.getElementById("p_id").innerHTML = winningTeam;
for(j=0;j< winningTeam.length;j++)
{
if(winningTeam[j] === 'D')
{
streak=streak+1;
}
else if(winningTeam[j] === 'N')
{
if( streak > longestStreak)
{
longestStreak = streak;
streak=0;
}
}
}
document.getElementById("r_id").innerHTML="the longest streak of Duke wins :-";
document.getElementById("q_id").innerHTML = longestStreak;
</script>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.