Please review my javascript code because it's not working

Posts
23
Registration date
Sunday February 17, 2019
Status
Member
Last seen
March 2, 2019
- - Latest reply: ac3mark
Posts
13036
Registration date
Monday June 3, 2013
Status
Moderator
Last seen
October 11, 2019
- 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);
}
}
See more 

1 reply

Posts
13036
Registration date
Monday June 3, 2013
Status
Moderator
Last seen
October 11, 2019
1459
0
Thank you
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);
}
}
rab_del
Posts
23
Registration date
Sunday February 17, 2019
Status
Member
Last seen
March 2, 2019
-
thank you for your reply but I still can't get this search function to work
ac3mark
Posts
13036
Registration date
Monday June 3, 2013
Status
Moderator
Last seen
October 11, 2019
1459 -
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?
ac3mark
Posts
13036
Registration date
Monday June 3, 2013
Status
Moderator
Last seen
October 11, 2019
1459 -
I just saw another flaw, your variabke of dictionary is referenced with lower and upper case names. Variables are case sensitive.
rab_del
Posts
23
Registration date
Sunday February 17, 2019
Status
Member
Last seen
March 2, 2019
-
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
ac3mark
Posts
13036
Registration date
Monday June 3, 2013
Status
Moderator
Last seen
October 11, 2019
1459 -
You are welcome. Come back if you need more help with javascripting.
Respond to ac3mark