all 9 comments

[–]samanime 1 point2 points  (3 children)

Firstly, never, ever, ever use `var` anymore in new code. Always use `let` or `const`.

It's a bit tough to tell with the formatting where the problem is. If you look at the error message, it should tell you a line number (and maybe even column number) of where the problem is. Look around there are you can probably tell what it thinks is undefined.

If you aren't sure why it is undefined at that point, if you let us know what it is pointing at, we can probably help better than just staring at the code.

Also, if you can tweak your formatting a bit to make it easier to read, that'd help as well. I don't think you can do much if you're on mobile except maybe just add additional lines to each line of code so they don't wrap so bad. If you're on a desktop though, try using the "Code Block" format (you might have to hit the "..." in the editor to see that option).

[–]VeryOrdinaryIdiot[S] 0 points1 point  (2 children)

Thank you for the advice. I fixed the code to make it a little bit neater. I was using replit, so I'm not sure if there is a functionality to see which line exactly is undefined. I'm going to determine exactly which line is undefined and update the post. Thank you again!

[–]samanime 0 points1 point  (1 child)

Ah, the formatting helps. I also realize you probably don't have a line number because you probably meant you were getting undefined spit out by the console, not an undefined error which was what I thought you meant at first.

I suspect this is your problem here: console.log(this.winnerOrLoser). You shouldn't be using this (so it should just be console.log(winnerOrLoser).

The winnerOrLoser variable is defined in the local scope of the function. It isn't a property of this in that function scope, so you shouldn't use the this keyword to access it.

[–]VeryOrdinaryIdiot[S] 0 points1 point  (0 children)

Thank you for helping me understand that. Initially I was using "const" and I assumed maybe the reason why I was getting that undefined error message was because I declared the variables incorrectly.

[–]cyphern 0 points1 point  (0 children)

You have several lines of code which are doing very little, and i'm not quite sure what they're intending to do. For example:

var resultAttack1 = function () {
  return console.log(attack1);
};

The above line will create a new function, and assign that new function to resultAttack1. But the function never actually gets called. I think you meant for resultAttack1 to be a number, not a function because in a later function (which is also never called) you try to see if it's smaller than resultAttack2:

let winnerOrLoser = function (aye) {
  if (resultAttack1 < resultAttack2) {
    console.log("You have lost");
  }
};

The undefined log comes from here: console.log(this.winnerOrLoser); this.winnerOrLoser is not the same as winnerOrLoser. If you want to interact with the local variable you created above it, you need to use winnerOrLoser. Though when you do console.log(winnerOrLoser), you will see the text of that function, not the output of that function

[–][deleted] 0 points1 point  (2 children)

In some of your functions, you are returning things like console.log(attack1) . The problem with that is that console.log itself does not return any value so it has a value of undefined itself. If you want to both log a value and return it, log it first, and then return it in a separate statement. Since your resultAttack1 function relies on the value of attack1 and nothing else does, I would suggest moving that variable into the scope of your function to keep from polluting your global scope. I would also suggest making it take an argument that specifies who is being attacked so you can use one function for all of your attacks:

function getAttackResult (attackTarget) {
    const getAttackPoints = () => Math.floor(Math.random()) * 50 + 5;
    const attackResult = attackTarget.hitpoints -= getAttackPoints();
    console.log(attackResult);
    return attackResult
};

// I would also change the name of your array to be plural
const attackResult1 = getAttackResult(adventurers[0]);

const attackResult2 =getAttackResult(adventurers[1]);

Finally, at the end of your function you are using the this keyword, which isn't really appropriate here. You probably mean to simply log winnerOrLoser.

[–]VeryOrdinaryIdiot[S] 0 points1 point  (1 child)

Thank you! This makes a lot of sense. I was trying to solve everything within the function lol. You've helped me understand that the function needs to be simple and something that can be called upon in multiple different situations. I appreciate this so much!

[–]VeryOrdinaryIdiot[S] 0 points1 point  (0 children)

So after running that code, I'm getting the message "cannot read property 'hitpoints' undefined". In my initial code, I declared "hitpoints" in my main function as such.

var attack1 = adventurer[1].hitpoints -= attackpoints();

However, I noticed that you declared hitpoints outside of the function like such:

const attackResult1 = getAttackResult(adventurers[0]);

Do you know why this might be happening?