+ 11

Please Fix This Code.

If is Add a word in input option , like a " Apple , Book , Scool " etc. Its show me , not found word, If i add a last word " Class" , its show me word meaning.... How its work fine ? https://code.sololearn.com/W8E9E5AkXhRO/?ref=app

22nd Jun 2021, 6:21 AM
𝐀𝐲𝐞𝐬𝐡𝐚 𝐍𝐨𝐨𝐫
𝐀𝐲𝐞𝐬𝐡𝐚 𝐍𝐨𝐨𝐫 - avatar
13 Answers
+ 18
function Mean(){ //input value var text=document.getElementById("word").value.toLowerCase(); var total_=database.length; for(i=0;i<total_;i++){ var db= database[i]["word"].toLowerCase(); if(text===db){ var got_=database[i]["meaning"]; result.innerHTML=`<center>Word: <span id="result_word">${text}</span></center> <center>Meaning :<b id="meaning">${got_}</b></center>`; break; } else{ result.innerHTML ="<center>Not Found</center>"; } } return false; }
22nd Jun 2021, 6:30 AM
Cmurio
Cmurio - avatar
+ 3
JESUS EDUARDO CANUL KOYOC in addition, your code is not working, as use same value for 'text' and 'db' ^^
22nd Jun 2021, 6:32 AM
visph
visph - avatar
+ 2
Ayesha Noor the code provided do what I said, without explaining what's changed ^^
22nd Jun 2021, 6:31 AM
visph
visph - avatar
+ 2
Ayesha Noor did you test the code? it should always output the first word meaning in list ;P
22nd Jun 2021, 6:35 AM
visph
visph - avatar
+ 1
it work only for last value in list because you always output something for each value in list... so only last one remain ^^ to fix it, just add 'break' after output of found word to exit the loop as soon as word found (and not displaying "not found" for next results)
22nd Jun 2021, 6:30 AM
visph
visph - avatar
+ 1
it's a shame that's a not correct code get the best answer mark... all the more that after correcting it, poster did more verbose solution than initial one (OP used 'word' variable implicitly declared with 'word' id element reference) ^^ on my side I have provided explanation and just the minimal change to do in code to be fixed ;P
22nd Jun 2021, 6:46 AM
visph
visph - avatar
+ 1
function Mean(){ var input= document.getElementById("word").value.toLowerCase(); var total_=database.length; for(i=0;i<total_;i++){ var db= database[i]["word"].toLowerCase(); if(input.trim()===db.trim()){ var got_=database[i]["meaning"]; result.innerHtml=''; result.innerHTML=`<center>Word: <span id="result_word">${db}</span></center> <center>Meaning :<b id="meaning">${got_}</b></center>`; return true; } } result.innerHTML ="<center>Not Found</center>"; return false; }
23rd Jun 2021, 2:12 AM
Naveen Surya
Naveen Surya - avatar
+ 1
I think here last return statement false is spelling mistaked by falsd. Its the fault.
23rd Jun 2021, 6:24 PM
RAJANBABU V
RAJANBABU V - avatar
+ 1
Js: const database=[ {"word":"Apple","meaning":"پھل"}, {"word":"Book","meaning":"کتاب"}, {"word":"School","meaning":"سکول"}, {"word":"Class","meaning":"جماعت"} ] function Mean(){ var input= document.getElementById("word").value.toLowerCase(); var total_=database.length; for(i=0;i<total_;i++){ var db= database[i]["word"].toLowerCase(); if(input.trim()===db.trim()){ var got_=database[i]["meaning"]; result.innerHtml=''; result.innerHTML=`<center>Word: <span id="result_word">${db}</span></center> <center>Meaning :<b id="meaning">${got_}</b></center>`; return false; } } result.innerHTML ="<center>Not Found</center>"; return false; } falsd = false
23rd Jun 2021, 6:52 PM
The Happy villager prod.
The Happy villager prod. - avatar
+ 1
const database=[ {"word":"Apple","meaning":"پھل"}, {"word":"Book","meaning":"کتاب"}, {"word":"School","meaning":"سکول"}, {"word":"Class","meaning":"جماعت"} ] function Mean(){ var input= document.getElementById("word").value.toLowerCase(); var total_=database.length; for(i=0;i<total_;i++){ var db= database[i]["word"].toLowerCase(); if(input.trim()===db.trim()){ var got_=database[i]["meaning"]; result.innerHtml=''; result.innerHTML=`<center>Word: <span id="result_word">${db}</span></center> <center>Meaning :<b id="meaning">${got_}</b></center>`; return false; } } result.innerHTML ="<center>Not Found</center>"; return false; }
23rd Jun 2021, 9:15 PM
Maxwell D. Dorliea
Maxwell D. Dorliea - avatar
0
The code is working Fine!
24th Jun 2021, 12:57 AM
Error!
Error! - avatar
- 1
p
28th Jun 2021, 5:18 PM
Agus Maharandika
Agus Maharandika - avatar
- 9
Hi
22nd Jun 2021, 3:29 PM
Mimicha Mimicha