Help needed! I've written some code in JavaScript, but it's throwing an error.
function Brain(){
var request = "request";
var reply = "reply";
var words = '';
this.Reply = function(){
if(request == words.nouns[0].noun){
reply = words.nouns[0].noun;
}
else {
reply = this.words.nouns[0].noun;
}
}
this.SetRequest = function(req){
request = req;
}
this.GetReply = function(){
return reply;
}
this.Parse = function(u){
var xmlhttp = new XMLHttpRequest();
var url = u;
var result;
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
words = JSON.parse(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
return result;
}
this.Construct = function(){
words = this.Parse('brain/words.json');
}}
var brain = new Brain();
brain.Parse('brain/words.json');
I am also providing my json file content below:
{
"nouns": [
{"noun": "dog"},
{"noun": "cat"}
]
}
Everything works fine in the testing environment, but upon executing the code, an error is thrown:
Uncaught TypeError: Cannot read property 'nouns' of undefined