Please review my javascript code because it's not working [Closed]

Report
-
 Blocked Profile -

// 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);
}
}

1 reply


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);
}
}
Anonymous User
thank you for your reply but I still can't get this search function to work
Blocked Profile
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?
Blocked Profile
I just saw another flaw, your variabke of dictionary is referenced with lower and upper case names. Variables are case sensitive.
Anonymous User
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
Blocked Profile
You are welcome. Come back if you need more help with javascripting.