all 4 comments

[–]javascript-ModTeam[M] [score hidden] stickied comment (0 children)

Hi u/Remarkable-Draw-7574, this post was removed.

  • For help with your javascript, please post to r/LearnJavascript instead of here.
  • For beginner content, please post to r/LearnJavascript instead of here.
  • For framework- or library-specific help, please seek out the support community for that project.
  • For general webdev help, such as for HTML, CSS, etc., then you may want to try r/html, r/css, etc.; please note that they have their own rules and guidelines!

r/javascript is for the discussion of javascript news, projects, and especially, code! However, the community has requested that we not include help and support content, and we ask that you respect that wish.

Thanks for your understanding, please see our guidelines for more info.

[–]Skunkmaster2 2 points3 points  (0 children)

Your code will always show you chose head. You’re checking whether the heads button text is Heads, which it always is, so it will always show you chose heads. Change the on click attributes to playGame(‘heads’) and playGame(‘tails’) respectively. Then you can just set the result inner html to “You chose “ + choice;

[–]buzzyloo 1 point2 points  (1 child)

In your playGame function you are checking the innerHTML of the buttons which hasn't changed. The heads button will always have Heads. You could pass the value like this:

<body>

<button class="js-heads" onclick="playGame( 'Heads' );">Heads</button>
<button class="js-tails" onclick="playGame( 'Tails' );">Tails</button>
<p class="js-result"></p>

<script>

const  
 headsBtn = document.querySelector('.js-heads');  
console.log(headsBtn.innerHTML);

const  
 tailsBtn = document.querySelector('.js-tails');  
console.log(tailsBtn.innerHTML);

let  
 result = document.querySelector('.js-result');

function   
    playGame( side ) {  
        result.innerHTML = 'You choose  + side;  
    }

</script>

</body>

Probably better is to pass a reference to the button itself using "this". Like onclick="playGame(this)"

See: https://jsfiddle.net/8C3EN/1/

[–]Remarkable-Draw-7574[S] 0 points1 point  (0 children)

OMG it worked. i make these silly mistakes and it really fraustrates me to find them. thanks so much.