Please review my javascript code because it's not working

Closed
Anonymous User - Feb 18, 2019 at 12:22 PM
 Blocked Profile - Feb 19, 2019 at 09:11 AM

// search functionality
search = function(){
query = document.getElementById('search').value;

if (query == "") {
return;
}

found = -1; // initialize found to false

for (var i = 0; i < dictionary.length; i++) {
if (query == Dictionary[i].word) {
found = i;
break;
}else {
document.getElementById('word_text').innerHTML = "Word not found";
document.getElementById('definition').innerHTML = "The word you entered is not found in our dictionary";
document.getElementById('related').innerHTML = "No related words";
}
}

if (found >= 0) {
show(found);
}
}
Related:

1 response

Blocked Profile
Feb 18, 2019 at 04:43 PM
Well, I have never seen a Javascript formed in this manner: search = function(){

The above would populate a variable called search, with the return value of what ever the FUNCTION FUNCTION would return. You cannot name a function FUNCTION!

Try this:
// search functionality
function SEARCHPAGE(){
query = document.getElementById('search').value;

if (query == "") {
return;
}

found = -1; // initialize found to false

for (var i = 0; i < dictionary.length; i++) {
if (query == Dictionary[i].word) {
found = i;
break;
}else {
document.getElementById('word_text').innerHTML = "Word not found";
document.getElementById('definition').innerHTML = "The word you entered is not found in our dictionary";
document.getElementById('related').innerHTML = "No related words";
}
}

if (found >= 0) {
msgbox("found: " & found);
}
}
0
Anonymous User
Feb 18, 2019 at 06:45 PM
thank you for your reply but I still can't get this search function to work
0
Blocked Profile
Feb 18, 2019 at 06:52 PM
What does it return? Are there actual elements named with the correct names? There is more working here then just javascript, the rest of the html has to be formatted correctly, and the names are case sensitive. What is the dictionary length at initialization? If dictionary length is less than 1, it wont run anything. Where did you cut this from?
0
Blocked Profile
Feb 18, 2019 at 07:34 PM
I just saw another flaw, your variabke of dictionary is referenced with lower and upper case names. Variables are case sensitive.
0
Anonymous User
Feb 19, 2019 at 08:22 AM
I found the source of the probleme you're right it's case sensitve I replaced the "D" with "d" in this line :

if (query == Dictionary[i].word)

and it workd thank you for your help
0
Blocked Profile
Feb 19, 2019 at 09:11 AM
You are welcome. Come back if you need more help with javascripting.
0