Trouble calling back the result of a function


#1

Please forgive me as this is my first post.

https://www.codecademy.com/en/courses/javascript-beginner-en-ASGIv/2/4?curriculum_id=506324b3a7dffd00020bf661#

So I'm currently on the final exercise of the For, While, and Do/While loops. This is a question that is somewhat unrelated to the exercise itself, and more of a question about the functions I'm using.

Essentially, I was attempting to make a simulation in which there were two fighters, ie; boxing match or something like that. I've created two seperate functions that assign a random score between 1 and 5 for both fighters;
.....................
var judgeScoreT = function (list) {
return list[Math.floor((Math.random()*list.length))];
// call this function with ---> judgeScoreT([1, 2, 3, 4, 5])
}
.....................
and
.....................
var judgeScoreC = function (list) {
return list[Math.floor((Math.random()*list.length))];
// call this function with ---> judgeScoreC([1, 2, 3, 4, 5])
}
.....................
And I've called the function like this inside of the simulation;
.......................
var fight = true;

while(fight) {
console.log("The judges award" + " " + judgeScoreT([1, 2, 3, 4, 5]) + " points to TypeNirvash!");

console.log("The judges award" + " " + judgeScoreC([1, 2, 3, 4, 5]) + " points to Computer!");

}
..................
Which serves the purpose I was looking for initially. But then I thought; why stop there? Why not also have it compare scores and list who happened to win?

So I then coded;
..................
if (judgeScoreT > judgeScoreC) {
console.log("TypeNirvash wins!")
}
else {
console.log("Computer wins!")
}
var fight = false;
}
.............

Just below the original code.

Here's the thing, though. I know this is wrong, and I know this will return a SEPERATE result for the score. So if the console logs that three points are awarded to TypeNirvash and four points are awarded to the computer, it could still log "TypeNirvash wins!" below.

Is there any way that I would be able to use the result of the initial assignment of judgeScoreT and judgeScoreC from the console.log?

Thank you in advance!


#2

the console logs that:
3 points are awarded to TypeNirvash
4 points are awarded to the computer

it could still log "TypeNirvash wins!

It could? Do you mean that it does or that you want it to? It's difficult to make out what you're asking.

3 is not greater than 4, no.

Are you trying to read what you printed? Or refer to values that you've overwritten? Neither is possible. Just put what you're printing in a variable/don't overwrite values you mean to keep/create another variable

What's wrong? The thing you previously wrote, or the next thing? How is it wrong?

"this"? What? And separate from what?


Please write less, make sure what you write is abundantly clear. If the code is relevant, provide a complete example, preferably with unrelated code removed so that only the code you're asking about remains


#3

ionatan,

Thanks for the reply. To put it short, this is what the code looks like in it's entirety. In a way, the majority of the code is related.

.....

var judgeScoreT = function (list) {
return list[Math.floor((Math.random()*list.length))];
// call this function with ---> judgeScoreT([1, 2, 3, 4, 5])
}

var judgeScoreC = function (list) {
return list[Math.floor((Math.random()*list.length))];
// call this function with ---> judgeScoreC([1, 2, 3, 4, 5])
}

var fight = true;

while(fight) {
console.log("The judges award" + " " + judgeScoreT([1, 2, 3, 4, 5]) + " points to TypeNirvash!");

console.log("The judges award" + " " + judgeScoreC([1, 2, 3, 4, 5]) + " points to Computer!");
}

This part ----> if (judgeScoreT > judgeScoreC) { _
console.log("TypeNirvash wins!")
}
_else { _
console.log("Computer wins!")
}
var fight = false;
}

The italic portion (beginning where I marked "This Part") is where my problem exists. Here is an example of the output:

"The judges award 3 points to TypeNirvash!"
"The judges award 1 point to Computer!"
"Computer wins!"

Essentially, what I'm asking is if I could change this code to compare the bolded numbers above instead of selecting two new numbers and comparing those. The output that says "Computer wins!" is referencing the second check (judgeScoreT > judgeScoreC). Obviously, the computer can't win if he's gotten 1 point compared to 3. But the reason why it "won" in this instance is because the second check "(judgeScoreT > judgeScoreC)" resulted in a higher value for the computer.

Hopefully that makes sense.


#4

You're comparing two functions to each other, that's not a very meaningful comparison. Call the function and store the result in a variable


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.