Reputation: 407
I have been tinkering with this for a little bit, slowly correcting it as I figure it out, but im fairly stumped now. It is to prompt the user for a name and to check it against the array values and return it. I got the motions down but it seems still not want to work correctly.
here are the specifics instructions.
onload = init;
var dwarfs = new Array();
dwarfs[0] = "Doc";
dwarfs[1]= "Grumpy";
dwarfs[2]= "Happy";
dwarfs[3]= "Sleepy";
dwarfs[4]= "Bashful";
dwarfs[5]= "Dopey";
dwarfs[6]= "Sneezy ";
document.getElementsByTagName("a")[4].onclick =
function(){
dwarfCheck();
}
function dwarfCheck(){
var name = window.prompt("Name a Dwarf from Snow White")
var dwarfFound = false
for(var i=0; i < 7; i++){
if(name == dwarfs[i])
dwarfFound = true
}
window.alert(dwarfFound)
}
Upvotes: -1
Views: 69
Reputation: 9376
var dwarfs = new Array();
dwarfs[0] = "Doc";
dwarfs[1]= "Grumpy";
dwarfs[2]= "Happy";
dwarfs[3]= "Sleepy";
dwarfs[4]= "Bashful";
dwarfs[5]= "Dopey";
dwarfs[6]= "Sneezy ";
The above could be written as
var dwarfs = ["Doc", "Grumpy", "Happy", "Sleepy", "Bashful", "Dopey", "Sneezy"];
for shorter. Javascript allows you to define arrays this way.
Now, your function dwarfCheck
is defined like it accepts a parameter x
in
function dwarfCheck(x){ ....
but you are calling it without passing it any parameters here
document.getElementsByTagName("a")[4].onclick =
function(){
dwarfCheck(); //you are calling dwarfCheck without passing any parameter even though it expects a parameter x!
}
This would not be a problem, if only you were not actually using the expected parameter here
var dwarfFound = dwarfs[x]
so you have to sort that out.
And then your for
loop is all weird, but it could be written correctly, like so
for(var i=0; i < 7; i++){
if(name == dwarfs[i]) {
dwarfFound = true;
window.alert(dwarfFound);
}
}
and this way you do not need to write all these lines of repeated code, the loop will save you the typing.
Finally, get used to ending your statements with ;
. Javascript allows you to omit it in most cases, but it is good practice to use it.
Upvotes: 0
Reputation: 64
var name = window.prompt("Name a Dwarf from Snow White");
var dwarfFound = false; // You haven't found him yet
for (var i = 0; i < dwarfs.length; i++) {
if (dwarfs[i] == name) {
dwarfFound = true; // if dwarf number i is the one you're looking for, then this gets set to true
break;
}
}
if (dwarfFound) {
window.alert("Found " + name + "!");
dwarfFound = false;
}
Ok, a couple things. The main problem is in your dwarfCheck function. So I rewrote the internals a bit for you. You shouldn't need to pass in any variables because you're getting the name from the prompt in the function. You're also looping through the array so you're only dealing with one dwarf at a time (dwarf[i]). You just have to verify that that dwarf is not the one you're looking for. If not the loop will check the next one until it either finds it or has checked them all. I put in a break statement so that it won't keep looping after you've already found the dwarf (what would be the point?). Window alerts should generally have a meaningful message, not just TRUE or FALSE. and if you're going to run it several times you should set the value back to false when you're done. (It's just polite to clean up after yourself.)
Hope this helps!
Upvotes: 0