Lowest price ever! Learn Generative AI for 48% less!
Get my discount+ 2
Can someone please tell me what's wrong with this code?
6 Answers
+ 2
sujay simha
Try this, I'm not good in web programming, but probably this gives you an idea.
Feel free to ask if there's a doubt. I'll try to clear up best I can.
<!DOCTYPE html>
<html>
<head>
<title>Search array</title>
</head>
<body>
<form>
<label>Name: <input type="text" id="username"></label>
<button type="button" onclick="search()">search</button><br />
</form>
<script>
var persons;
window.onload = function() {
persons = [{name:"saritha", age:39}, {name:"sujay", age:10}, {name:"kalyan", age:20}, {name:"madhu", age:43}];
};
function search() {
var ask = document.getElementById("username").value.toLowerCase();
if(ask.length == 0) return false;
persons.forEach(function(person) {
if(person.name == ask)
console.log(person.name + " " + person.age + " years");
});
}
</script>
</body>
</html>
+ 4
No problem buddy, I am glad if it helps. And don't worry, I didn't write the whole code, I copied parts of yours : )
+ 3
Can you elaborate more on what you expected from the code please? I'm not getting any idea.
P.S. Please add HTML to Relevant Tags section to add scope clarity 👍
(Edit)
Tags had been updated. Thanks for understanding 👍
+ 2
Ipang, well, I need to write a code which will print a search bar and when I click the search button, if the word is in the Array, I should print that
btw, I did that
+ 2
thanks a lot Ipang
btw, I'm really sorry for making you type all the code
+ 2
😁😁