Ask coding questions

← Back to all posts
Some JS
isaiah08 (63)

What is wrong with this code?

<style>
	button{
		padding: 16px;
		background-color: #555555;
		color: white;
		border-radius: 75%;
	}
</style>

<p id='question'></p>
<button id='a'></button>
<button id='b'></button>
<button id='c'></button>
<button id='d'></button><br>
<p id='score'>0</p>




<script>
var variable_knowledge = 0;

function yes() {
	variable_knowledge += 1;
	document.getElementById('score').innerHTML = variable_knowledge
};
function no() {
	variable_knowledge -= 1;
	document.getElementById('score').innerHTML = variable_knowledge
};

	

function quiz_variable() {
	document.getElementById('question').innerHTML = 'Which way would you create a variable called var that holds the interger 13?'
	document.getElementById('a').innerHTML = "A. print 13"
	document.getElementById('a').onclick = no()
	document.getElementById('b').innerHTML = "B. var = 13"
	document.getElementById('b').onclick = yes()
	document.getElementById('c').innerHTML = "C. var == 13"
	document.getElementById('c').onclick = no()
	
	
};
	
quiz_variable()
</script>
Answered by Coder100 (17035) [earned 5 cycles]
View Answer
Comments
hotnewtop
Coder100 (17035)

Problem

You are calling the function, but you are not supposed to do that, you must pass the function!

Solution

<style>
	button{
		padding: 16px;
		background-color: #555555;
		color: white;
		border-radius: 75%;
	}
</style>

<p id='question'></p>
<button id='a'></button>
<button id='b'></button>
<button id='c'></button>
<button id='d'></button><br>
<p id='score'>0</p>




<script>
var variable_knowledge = 0;

function yes() {
	variable_knowledge += 1;
	document.getElementById('score').innerHTML = variable_knowledge
};
function no() {
	variable_knowledge -= 1;
	document.getElementById('score').innerHTML = variable_knowledge
};

	

function quiz_variable() {
	document.getElementById('question').innerHTML = 'Which way would you create a variable called var that holds the interger 13?'
	document.getElementById('a').innerHTML = "A. print 13"
	document.getElementById('a').onclick = no
	document.getElementById('b').innerHTML = "B. var = 13"
	document.getElementById('b').onclick = yes
	document.getElementById('c').innerHTML = "C. var == 13"
	document.getElementById('c').onclick = no
	
	
};
	
quiz_variable()
</script>
isaiah08 (63)

What is different about your code? @Coder100

Coder100 (17035)

note how I don't have the () around the functions for onclick @isaiah08

Coder100 (17035)

np! Mark as correct answer please! @isaiah08

Coder100 (17035)

be more descriptive please