Help me: javascript project

Hello everyone,

I just started to code this kind of betting games where the user have to choose which team will win the game and when the match end he will get points according to the match results. I hope you understand me. I came up with the attached html and javascript code and I need someone to review it. I think when I click win as shown in image my points should be 3 but nothing happened at all. So what is going wrong with my code?

dsadsa
My html code

<div>
	<p>realmadrid <span id="xteam">3</span> - <span id="yteam">2</span> realbetis</p>
	<button value="xwin" id="btn" onclick="calc()">win</button>
	<button value="tie" id="btn" onclick="calc()">tie</button>
	<button value="ywin" id="btn" onclick="calc()">win</button>
	<br>
	<p>your points: <span id="score"></span></p>
</div>

My javascript code

function calc() {
	var x === document.getElementById('xteam').innerText;
	var y === document.getElementById('yteam').innerText;
	
	var matchresult = "";
	var score = 0;

	if (x > y) {
		return matchresult = "xwin";
	}
	else if (x < y) {
		return matchresult = "ywin";
	}
	else{
		return matchresult = "tie";
	}

	if (matchresult = document.getElementsById('btn').value) {
		return score = 3;
	}
	else{
		return score;
	}

	document.getElementById('score').innerText = score;
}

I’m a beginner too, so I may not help, but I will try.

var x === document.getElementById('xteam').innerText;

The === checks for strict equality. I believe you are trying to assign the value to the var. Try that first.

Dear friend,
yeah I use === because x an y must be number so that I can make the comparison in my if statements. But I tried with just =, == and === and still not working.

function calc() {
	var x = document.getElementById('xteam').innerText;
	var y = document.getElementById('yteam').innerText;
	
	var matchresult = "";
	var score = 0;

	if (x > y) {
		matchresult = "xwin";
	}
	else if (x < y) {
	  matchresult = "ywin";
	}
	else{
		matchresult = "tie";
	}
  
  console.log(matchresult);

	if (matchresult = document.getElementById('btn').value) {
		score = 3;
	}

	document.getElementById('score').innerText = score;
}

this should work, the biggest thing was a typo in the
if (matchresult = document.getElement**s**ById('btn'.value) {

Also to clarify:

=== means “is it exactly equal to” "2"===2 //false
== means “mostly equal to” ie "2" == 2 // true;
and
= means “this must equal”

Hi Coders… well there is a bit of confusion here going on (that’s normal as you are at the beginning :slight_smile:) so… before even thinking about the different way to compare value in JavaScript, we have to do a step back here and understand the difference between compare or assign something… I usually remember in this way:
With “=” we are assigning something on the right into the thing on the left of the equal symbol while with “==” or ‘’===" we are comparing two values. (I suggest at the beginning to use the “===” to don’t get so confused)

Try to review your code with this difference in your mind, going step by step and asking yourself if you are comparing or assigning things :slight_smile: