Shattered wrote: ↑Tue Nov 01, 2022 5:30 pm
don't understand how either of these works but that gave the results I wanted!
Yeah, I know, but I didn't want to write walls of text. It was also pretty late when I wrote that.
Here are a few pointers, in case you want to learn more about it:
What I called "lists": The technical term is "array". But... They're lists... and one of the things they can do, is sort themselves. Usually just alphabetically:
boom- done, but if that's not what you want, you can provide a different criterion (using a
sort compare function)
You've probably been wondering about "window". That's a special variable that - among loads of other things - contains all global variables as a property. This means, the following three instructions do (pretty much) the same:
Code: Select all
var test = 4;
window.test=4;
window["test"]=4;
The interesting one is the last one, because with that you can access variables that you don't know the name of at coding-time. All you need is a string containing the name of the variable, which can be determined at runtime.
The last one is a classic "for-loop". Definitely should check that out if you haven't yet. It's as common as a screwdriver.
Code: Select all
window[stringContainingMyUnknownVariableName]
This is what allows the variable-names-list-sorting-magic.
Shattered wrote: ↑Tue Nov 01, 2022 5:30 pm
For the finding the lowest score part, I've tried to interpret what I can from this and tried a few different things but couldn't get it to work. As far as I understood from the above, I added
...
Then I used 4 if statments to find the one that's zero, then set what I wanted accordingly... Think that works?
Then set the 4 values of the temps back to the original scores once i've got my if's done.
Sorry, I didn't see the second part. Sure that works. After sorting the final scores, you know that the first one in the list is the name of the lowest variable. You can read out that name using:
"0" as in zero, because the first element has the index 0, the second has the index 1 and so on...
So, you don't need the for-loop in this case, I think.
Shattered wrote: ↑Tue Nov 01, 2022 5:30 pm
P.S. What does this code do if two scores are tied - it looks like it randomly chooses a winner? Maybe too complicated to say if they are the same, it triggers a tiebreaker round or something?
I would assume that they are secondarily "sorted" by their original order. In any case it shouldn't cause "problems".
A tiebreaker you say? You could use a for-loop to check how many of the first three elements are equal to the last element:
Code: Select all
var userAlisascore=5;
var userMilascore=90;
var userKatoscore=90;
var userBrookescore=90;
var ranking=["userAlisascore","userMilascore","userKatoscore","userBrookescore"];
ranking.sort(function(a,b){
return window[a]-window[b];
});
var sameCount = 1;
for(i=0; i<3; i=i+1){
if (window[ranking[i]] == window[ranking[3]]) {
sameCount++;
}
}
Then, "sameCount" contains how many scores are the same (and highest). Could be more than 2. Naturally, if it's sameCount>1, you could branch to a tiebreaker page.
Edit: right some wrongs