We help IT Professionals succeed at work.

Simple Javascript Score Object

Steve Tinsley
on
59 Views
Last Modified: 2019-02-14
I want to keep a simple score using Javascript:

 var score = [
    {name: "spring", score: 0},
    {name: "summer", score: 0},
    {name: "autumn", score: 0},
    {name: "winter", score: 0}
];

Open in new window



I have 4 questions and after each question I want to increase the result...

function questionCalc(number) {
	console.log(number);
	if (number == 1)
		spring ++;
	if (number == 2)
		summer ++;
	if (number == 3)
		autumn ++;
	if (number == 4)
		winter ++;
	console.log("RUNNING RESULTS Spring: "+spring+" Summer: "+summer+" Autumn: "+autumn+" Winter: "+winter);
}

Open in new window


I then want to show the result:

				var scores = [];

				var highestScore = -1, winners = [];
				for (var i = 0; i < scores.length; i++) {
				    if (scores[i] > highestScore) {
				        winners = [i];
				        highestScore = scores[i];
				    } else if (scores[i] == highestScore) {
				        winners.push(i);
				    }
				}

				if (winners.length == 1) {
				    alert("1 winner");
				} else {
				    alert("multiple winners");
				}

Open in new window


Can someone help with the questionCalc function, and perhaps look over my idea.

Thanks

Steve
Comment
Watch Question

Solutions Architect
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
@Michael - bear in mind arrays are 0 indexed so
score[num].score++;

Open in new window

Should probably be
score[num-1].score++;

Open in new window

I see a potential problem with this code
var scores = [];
var highestScore = -1, winners = [];
// SOMETHING NEEDS TO HAPPEN TO scores HERE
//     OR ELSE THIS FOR LOOP IS NEVER GOING TO RUN
for (var i = 0; i < scores.length; i++) {
  if (scores[i] > highestScore) {
    winners = [i];
    highestScore = scores[i];
  } 
  else if (scores[i] == highestScore) {
    winners.push(i);
  }
}

Open in new window

Kelvin McDanielSr. Developer
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions