Ask coding questions

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

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 (6010) [earned 5 cycles]
View Answer
Comments
hotnewtop
RYANTADIPARTHI (6010)

Solution

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 (63)

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

RYANTADIPARTHI (6010)

@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 (63)

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

RYANTADIPARTHI (6010)

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

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

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.

Secondly,

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

Coder100 (17122)

Error

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

Solution

function a1() {

so easy!

isaiah08 (63)

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

Coder100 (17122)

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

Coder100 (17122)

@isaiah08 correct code is:

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

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

JasonLiu19 (69)

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?";
document.getElementById('a').innerHTML="Python";
document.getElementById('b').innerHTML="Java";
document.createElement("button").id="c';
document.getElementById('c').innerHTML="Javascript";
document.createElement("button").id="d';
document.getElementById('d').innerHTML="C++";

Try that.

JasonLiu19 (69)

@JasonLiu19 also make sure to define b1.

JasonLiu19 (69)

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

JasonLiu19 (69)

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

SixBeeps (5067)

You need () after your function name