+ 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
13 Réponses
+ 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;
}
+ 3
JESUS EDUARDO CANUL KOYOC
in addition, your code is not working, as use same value for 'text' and 'db' ^^
+ 2
Ayesha Noor the code provided do what I said, without explaining what's changed ^^
+ 2
Ayesha Noor did you test the code? it should always output the first word meaning in list ;P
+ 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)
+ 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
+ 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;
}
+ 1
I think here last return statement false is spelling mistaked by falsd.
Its the fault.
+ 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
+ 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;
}
0
The code is working Fine!
- 1
p
- 9
Hi