0

I am trying to assign an output of a created element but keep getting a 'ReferenceError' response is not defined.

I followed instructions online and this worked on my other function I built. I have no idea why it is not working here and would like to understand the process of what is happening and what caused the error for future reference.

-My expectation is that I selected the body element.

-I don't know what the 'section.innerHTML = ''; does and could not find an explanation online. Mozilla had it in there so I just kept it.

-I assigned a variable 'para1' to the created element 'p'.

-I then assigned an output variable to the innertext of 'para1'

-I don't understand how it is saying response is not defined.

Thank you

UPDATE: Sorry I new to Javascript and obviously have things to learn. I am trying to create a variable to output to the created element 'p' in my function. The assignment is to create an array and randomly output a name using Math.random().

I was just trying to get everything created first. I am not done building this function, I just put the variable 'response' in the function to show that I am trying to use the created variable 'response' to output my strings in the function.


let names = ['Chris', 'Li Kang', 'Anne', 'Francesca', 'Mustafa', 'Tina', 'Bert', 'Jada']
let random = Math.floor(Math.random() * 9);



// Add your code here

function chooseName(names) {
    response = 'response';
}



let section = document.querySelector('body');
section.innerHTML = '';
let para1 = document.createElement('p');
para1.innerText = response;
coe
  • 281
  • 1
  • 2
  • 12
  • 2
    "*I don't understand how it is saying response is not defined.*" - what makes you assume that `response` *is* defined? It's nowhere in the code you posted. – Bergi Jun 28 '20 at 15:40
  • Is this all of the code u r having for this section? Where did u define response? – yue you Jun 28 '20 at 15:41
  • 1
    can you post the full code....because here there's no where `response` is defined...what is `response` – Rohit Ambre Jun 28 '20 at 15:41
  • "*I don't know what the `section.innerHTML = '';` does*" - https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML, https://stackoverflow.com/q/3955229/1048572 – Bergi Jun 28 '20 at 15:42
  • Thank you, I am reading up on it now. – coe Jun 28 '20 at 16:13
  • I updated my question everyone, thank you for pointing me in the right direction. – coe Jun 28 '20 at 16:18

3 Answers3

1

let section = document.querySelector('body');
let response = 'some response'; // Define response here
let para1 = document.createElement('p');

section.appendChild(para1);
para1.innerHTML = response;
Rafv
  • 261
  • 2
  • 9
1

You didn't define response so it's normal that error says response is not defined explain clearly what are you try to achieve

0

I did not add 'section.appendChild(para1) and I also did not add 'let response' at the top. I am not able to output in my variable. Thank you for pointing out where I was wrong in my thought process.

// Print a random name from array and run it once



let names = ['Chris', 'Li Kang', 'Anne', 'Francesca', 'Mustafa', 'Tina', 'Bert', 'Jada'];
let random = Math.floor(Math.random() * 9);
let response;


// Add your code heres

function chooseName() {
    if (3 > 1) {
        response = 'test';
    }
}

chooseName()




// Don 't edit the code below here!

let section = document.querySelector('body');
// let section = document.querySelector('body');

// section.innerHTML = '';
section.innerHTML = ' ';

let para1 = document.createElement('p');
// let para1 = document.createElement('p');

para1.textContent = response;

section.appendChild(para1);

coe
  • 281
  • 1
  • 2
  • 12