Ask coding questions

← Back to all posts
Javascript
isaiah08 (86)
Hello learner!
Are you new to python?
Press 1 if you are, and 2 if you know some. 

<script>
document.addEventListener('keydown', function(event){
	if (event.key == '1'){
		document.writeln("That's okay! You came to the right place!");
	} else if (event.key == '2'){
		document.writeln("Well that's good! Let's take a quiz to see how much you know!");
		setTimeout(() => {  quiz() }, 5536);
	};
	
});
var quiz = function() {
	var variables = 0;
	document.writeln("Which way would you create a variable called var that holds the interger 13?<br>A.print  'banana banana banana banana' <br>B. var = 13' <br>C. var == 13");
	document.addEventListener('keydown', function(event){
		if (event.key == 'a'){
			variables = 0
		} else if (event.key == 'b'){
			variables += 1
		} else if (event.key =='c'){
			variables = 0
		};
	});

	document.close();
	document.writeln("How would you create a variable that holds a list with three items?<br>A. a_list = ['item', 5, 4.3]<br>B. a_list == an item, another item, 43 <br>C. a_list == chicken, sheep, cow");
	document.addEventListener('keydown', function(event){
		if (event.key == 'a'){
			variables += 1;
		} else if (event.key == 'b'){
			variables -= 1;
		} else if (event.key == 'c'){
			variables -= 1;
		};
	});
	document.close();
	documetn.writeln("How would you pick a random item from a list?<br>A.<br>B.import random<br>fruit = ['banana','apple','cherry']")

	document.addEventListener('keydown', function(event){
		if (event.key == 'a'){
			variables += 1;
		} else if (event.key == 'b'){
			variables -= 1;
		} else if (event.key == 'c'){
			variables -= 1;
		};
});
	
};
</script>

Try running it, what is wrong?

Answered by Coder100 (18780) [earned 5 cycles]
View Answer
Comments
hotnewtop
Coder100 (18780)

problem 1


Typo

problem 2

This is not how js works, this code will never work. First, there is no document.writeln, we have document.write, but I doubt that is what you want.

Second, JS iS synchronous, so that means this code won't even run in any particular order, its just first come first serve, and finally, you should take a js tutorial

https://www.w3schools.com/js/DEFAULT.asp

RYANTADIPARTHI (6018)

@Coder100 there is something called document.writeln in js...

isaiah08 (86)

Thanks! I read the tutorial, changed my code, and have another question: why isn't this working?

<p id='question'></p>
<p id='a'></p>
<p id='b'></p>
<p id='c'></p>
<p id='d'></p>



<script>
var variable_knowledge = 0;

	

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('b').innerHTML = "B. var = 13"
	document.getElementById('c').innerHTML = "C. var == 13"
};
	
quiz_variable()
</script>

@Coder100

xxpertHacker (931)

Also, you're misusing setTimeout, it should be:
setTimeout(quiz, 5536);

xxpertHacker (931)

Didn't run it, documetn doesn't exist

});
	document.close();
	documetn.writeln("How would you pick a random item from a list?<br>A.<br>B.import random<br>fruit = ['banana','apple','cherry']")
documetn.writeln

->

document.writeln

Also, I'd shame you for using Document#open, Document#write, Document#writeln, Document#close, or Element#innerHTML.

Don't touch those, use the proper W3C DOM API.

RYANTADIPARTHI (6018)

For one thing, try adding === instead of ==. Also where's the prompt, or something. You can make it an int, and try that.

Kitty6709 (1)

Why won’t the event work?? I don’t see anything wrong with it.