user13738913
user13738913

Reputation:

javascript understanding if else statement

Hey i just started to learn javascript today.

And i'm having a little bit of trouble understanding the else if statement. ​

I thought if the first if statement is true the other two else if statements would not be called? ​

And if the first if statement was false it would try the second else if statement and if the second one returned false it would try the third else if statement?

But all the statements return true and i'm not sure why and was hoping someone could explain to me why? ​

  var Builds = [
    { dead: 0, tier: 8, type: "Stash", uid: 9989, x: 4032, y: 11424 },
    { dead: 0, tier: 1, type: "Wall", uid: 9990, x: 4042, y: 11525 },
    { dead: 0, tier: 1, type: "Wall", uid: 9991, x: 4052, y: 11526 },
    { dead: 0, tier: 0, type: "Wall", uid: 9992, x: 4062, y: 11527 },
    { dead: 0, tier: 0, type: "Door", uid: 9993, x: 4072, y: 11428 },
    { dead: 0, tier: 0, type: "Door", uid: 9994, x: 4082, y: 11429 },
    { dead: 0, tier: 8, type: "Door", uid: 9995, x: 4092, y: 11430 },
    { dead: 0, tier: 1, type: "Gun", uid: 9996, x: 4100, y: 11431 }
  ];

  Object.keys(Builds).filter(function(e) {
    return ("Stash" == Builds[e].type || "Wall" == Builds[e].type || "Door" == Builds[e].type);
  }).forEach(s => {

    if (Builds[s].type == "Stash") {
      console.log(Builds[s].type)

    } else if (Builds[s].type == "Wall") {
      console.log(Builds[s].type)

    } else if (Builds[s].type == "Door") {
      console.log(Builds[s].type)

    }
  })

Sorry if my english is bad.

Upvotes: 1

Views: 63

Answers (4)

Banny
Banny

Reputation: 111

According to your snippet, You are filtering the Builds Array to get stash,wall and Door types. what is filtered is pushed to the new array where the if else statements are applied, the second iteration on each entry in the new array returns true because they match the filter except the last iteration which returns Undefined  Screenshot of output after running your code

Upvotes: 0

Rickard Elimää
Rickard Elimää

Reputation: 7591

You don't need to get Object.keys because Builds is an array. You can use filter directly on the array, where filter returns each item in the array. See code below.

Like Joachim says in your answer, you are filtering everything except the gun. The filter method returns a new (filtered) array that you then loop through with for each.

I put your code in a function, and sending in both the Build array and an array with different filters to show how you can use the filter method in a more dynamic way.

var Builds = [
    { dead: 0, tier: 8, type: "Stash", uid: 9989, x: 4032, y: 11424 },
    { dead: 0, tier: 1, type: "Wall", uid: 9990, x: 4042, y: 11525 },
    { dead: 0, tier: 1, type: "Wall", uid: 9991, x: 4052, y: 11526 },
    { dead: 0, tier: 0, type: "Wall", uid: 9992, x: 4062, y: 11527 },
    { dead: 0, tier: 0, type: "Door", uid: 9993, x: 4072, y: 11428 },
    { dead: 0, tier: 0, type: "Door", uid: 9994, x: 4082, y: 11429 },
    { dead: 0, tier: 8, type: "Door", uid: 9995, x: 4092, y: 11430 },
    { dead: 0, tier: 1, type: "Gun", uid: 9996, x: 4100, y: 11431 }
  ];

function filterBuilds(buildsArr, filterArr) {
  buildsArr.filter((build) => {
    return filterArr.includes(build.type);
  })
  .forEach((build) => {
    if (build.type == "Stash") {
      console.log(build.type);
    } else if (build.type == "Wall") {
      console.log(build.type);
    } else if (build.type == "Door") {
      console.log(build.type);
    }
  })
}

filterBuilds(Builds, ['Stash', 'Wall']);
console.log('-----');
filterBuilds(Builds, ['Stash']);

Upvotes: 0

Anurag Ranjan
Anurag Ranjan

Reputation: 21

Your if else block is correct and it is running for every instance of array returned from filter function. Filter method is returning all the index of matched value in an array and you are checking the same index value in Builds array in if else block. so it is returning all the matched condition from if else block.

Upvotes: 0

Joachim Sauer
Joachim Sauer

Reputation: 308001

Your description of if-else is correct. It'll try them all until one is true and execute the corresponding code block and not check the later ones.

However, you're executing your if-else cascade once for each entry in the Builds array that matches the filter.

Upvotes: 2

Related Questions