Ask coding questions

← Back to all posts
Some really simple js code is not working
isaiah08 (86)

I'm making a programming language adviser, and when i just started, it isn't working. What is wrong with it? Thanks!

Answered by RYANTADIPARTHI (6018) [earned 5 cycles]
View Answer


All of your code seems fine. but the tiny error you have is, you forgot the () while defining your function. here's the code. you can copy, and paste this:

var prompt = document.getElementById('prompt');
var a = document.getElementById('a');
var b = document.getElementById('b');

prompt.innerHTML = "Hi! I'm Walt, your programming language adviser. Before we begin, have you learned a programming language before?";
a.innerHTML = "Yes!";
b.innerHTML = "No, i'm totally new!";
a.onclick = "a1";
b.onclick = "a2";

function a1() { // adding ()
	prompt.innerHTML = "That's great! What one is it?";
	a.innerHTML = "Python";
	b.innerHTML = "Java";
	var c = document.createElement("button");
	c.innerHTML = "JavaScript";

so that way you are defining it properly. for every function it is like that.
That should work

isaiah08 (86)

I changed it, but it still isn't working. @RYANTADIPARTHI


@isaiah08 it does work, you have no errors, but if you mean by the buttons, you need to get a, b in your element.

isaiah08 (86)

What do you mean a, b in your element? @RYANTADIPARTHI


@isaiah08 sorry, in your onclick, you have to use the functions. like this. copy, and paste it:

a.onclick = a1;
b.onclick = a2;
Coder100 (18780)

Also, just saying, prompt is a reserved function for getting user input. You won't have any issues with it in the short term, but you should definitely rename it.


you never actually added this element into the document, so you won't ever see it.

Coder100 (18780)


functions must have an argument list, even if it may be empty


function a1() {

so easy!

isaiah08 (86)

I changed it, but it still isn't working. @Coder100

Coder100 (18780)

well yeah, you passed in strings as your functions silly! @isaiah08

Coder100 (18780)

@isaiah08 correct code is:

a.onclick = a1;
b.onclick = a1;
angrydoge (481)

You put function a1 {} instead of function ai() {}

JasonLiu19 (71)

uhh for html maybe just do:

<p id="prompt">Hi! I'm Walt, your programming language adviser. Before we begin, have you learned a programming language before?</p>
<button id="a" onclick="a1">Yes!</button>
<button id="b" onclick="b1">No, I'm totally new!</button>

inside a1 js:

document.getElementById('prompt').innerHTML="That's great! What one is it?";

Try that.

JasonLiu19 (71)

@JasonLiu19 also make sure to define b1.

JasonLiu19 (71)

@JasonLiu19 also add paretheses like these:
"a1()" and a1(){

JasonLiu19 (71)

@JasonLiu19 the one for JS and Cpp don't work, so remove those for now.

SixBeeps (5314)

You need () after your function name