admin管理员组文章数量:1429747
I'm trying to plete this assignment, I've got the code set up, however, there's a problem.
The assignment: "Create an array with seven string values, initialized to the names of these stars: Polaris, Aldebaran, Deneb, Vega, Altair, Dubhe, and Regulus. Create an array with seven additional string values, initialized to the names of the constellations in which the stars are found: Ursa Minor, Taurus, Cygnus, Lyra, Aquila, Ursa Major, and Leo. Next, create a function that accepts a single string parameter. Within the function, iterate through the first array, searching for the star. When the star is found, return the value contained in that index within the second array. In other words, return the constellation name for that star. Use a prompt to gather the name of the star from the visitor, and then call the function with that input. Don’t forget to include code that executes when the star isn’t found. Display the result on the screen."
The code:
var stars = ["Polaris", "Aldebaran", "Deneb", "Vega", "Altair", "Dubhe", "Regulus"];
var stars2 = ["Ursa Minor", "Taurus", "Cygnus", "Lyra", "Aquila", "Ursa Major", "Leo"];
function processStar(starName) {
for (var i = 0; i < stars.length; i++) {
if (starName == stars[i]) {
return stars2[i];
} else {
return "No star found!";
}
}
}
var getStar = prompt("Input the star name.");
var result = processStar(getStar);
alert(result);
The problem:
This code works only for the first value in the stars array. Anything other than the first element of that array ("Polaris"), the function returns with a false value.
I'm trying to plete this assignment, I've got the code set up, however, there's a problem.
The assignment: "Create an array with seven string values, initialized to the names of these stars: Polaris, Aldebaran, Deneb, Vega, Altair, Dubhe, and Regulus. Create an array with seven additional string values, initialized to the names of the constellations in which the stars are found: Ursa Minor, Taurus, Cygnus, Lyra, Aquila, Ursa Major, and Leo. Next, create a function that accepts a single string parameter. Within the function, iterate through the first array, searching for the star. When the star is found, return the value contained in that index within the second array. In other words, return the constellation name for that star. Use a prompt to gather the name of the star from the visitor, and then call the function with that input. Don’t forget to include code that executes when the star isn’t found. Display the result on the screen."
The code:
var stars = ["Polaris", "Aldebaran", "Deneb", "Vega", "Altair", "Dubhe", "Regulus"];
var stars2 = ["Ursa Minor", "Taurus", "Cygnus", "Lyra", "Aquila", "Ursa Major", "Leo"];
function processStar(starName) {
for (var i = 0; i < stars.length; i++) {
if (starName == stars[i]) {
return stars2[i];
} else {
return "No star found!";
}
}
}
var getStar = prompt("Input the star name.");
var result = processStar(getStar);
alert(result);
The problem:
This code works only for the first value in the stars array. Anything other than the first element of that array ("Polaris"), the function returns with a false value.
Share Improve this question edited Aug 25, 2024 at 14:56 Boshra Jaber 1,1556 gold badges17 silver badges33 bronze badges asked Nov 27, 2011 at 16:03 RafayRafay 24.3k5 gold badges22 silver badges27 bronze badges 1- To map a star to its constellation an associative array makes more sense. – mike jones Commented Nov 27, 2011 at 16:08
2 Answers
Reset to default 5Your conditional statement is wrong. Try this out.
var stars = ["Polaris", "Aldebaran", "Deneb", "Vega", "Altair", "Dubhe", "Regulus"];
var stars2 = ["Ursa Minor", "Taurus", "Cygnus", "Lyra", "Aquila", "Ursa Major", "Leo"];
function processStar(starName){
for (var i=0; i < stars.length; i++) {
if(starName == stars[i]){
return stars2[i];
}
}
return "No star found!";
}
var getStar = prompt("Input the star name.");
var result = processStar(getStar);
alert(result);
Inside your loop body, you're always returning a value, so the loop body will only ever execute once.
本文标签: loopsJavaScript looping through arrayStack Overflow
版权声明:本文标题:loops - JavaScript looping through array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745441704a2658475.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论