loops - JavaScript looping through array - Stack Overflow

I'm trying to plete this assignment, I've got the code set up, however, there's a proble

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
Add a ment  | 

2 Answers 2

Reset to default 5

Your 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745441704a4627859.html

相关推荐

  • loops - JavaScript looping through array - Stack Overflow

    I'm trying to plete this assignment, I've got the code set up, however, there's a proble

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信