0

Woo first stackover flow post!

I'm trying to change a result depending on what "character" button is clicked.

So I start with a variable called: gendr = [];.

if the button named "marge" is clicked, the margeFunction will run, which pushes the value "female" inside the array. If the button "Henry" is clicked, the henryFunction will run, which pushes the value "male" inside the array.

A later function contains an if else statement, where if the array contains the value male "male sentence appears." else if the arrays value is female, "female sentence appears."

 function helloFunction() {
   if(gendr[female]) {
       document.getElementById ('traitText').innerHTML = "and says the word hello in every sentence she speaks";
   } else {
      document.getElementById ('traitText').innerHTML = "and says the world hello in every sentence he speaks"
   }
 }

I'm not exactly sure how to do this I just made a guess but I would like to know the correct way to do this! thanks in advance :)

Josh McIntosh
  • 59
  • 1
  • 7
  • Possible duplicate of [Check if an element is present in an array](https://stackoverflow.com/questions/7378228/check-if-an-element-is-present-in-an-array) – Ravi Jul 13 '19 at 11:06
  • Possible duplicate of [How do I check if an array includes an object in JavaScript?](https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-an-object-in-javascript) – Ivar Jul 13 '19 at 11:08
  • I edited your question to correctly format the code (4 spaces in front), I also removed the "first question" notice, we know that already (the UI tells us). In the future, focus on the question itself and format the code properly, that said, welcome to SO :) – Jonas Wilms Jul 13 '19 at 11:11

3 Answers3

2

gendr[female] won't work, as there is no female variable, and you don't want to access the female position in the array, instead, it sounds as if you want to get the last value of that array. That can be done with gendr[gendr.length - 1] . Now you want to check if that value is "female" and you can check that by comparing it (===).

However it is questionable wether you need an array at all, why not just keep a boolean (isFemale = false;)?

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • thanks for your comment but the way in which you're speaking is beyond my knowledge mostly hahaha. so if i were to keep the array, I could use: gendr [gendr.length - 1 === "female"] and itd work? also the way I wanted to do it was so there would only ever be one value in the array (either female or male, depending on which button would be clicked), so wouldnt that make the length always 0? - thanks for the reply btw – Josh McIntosh Jul 13 '19 at 11:43
  • An array with one value has length 1. An array with only one value makes little sense, go with a boolean then. Also, read my answer carefully, when you program you have to be exact. But you were close (`gendr[gendr.length - 1] === "female"`) – Jonas Wilms Jul 13 '19 at 11:47
  • 1
    I'll definetely give the boolean a shot after I'm done with the array method. this worked a heap thanks! – Josh McIntosh Jul 14 '19 at 10:14
1

There are multiple ways to do what you are trying to achieve. The indexOf method is an effective way to check if an element exists in an array. Also, ECMAScript 2016 has a new method includes to check if an element exists inside an array. Here's an example:

var gender = ['male', 'female'];

function checkInArray(genderArray) {
  if(genderArray.indexOf('male') > -1) {

    //do something
    return 'male found';
  } else {
    //do something
    return 'female found';
  }
}

function checkInArray2(genderArray) {
  return genderArray.includes('male'); 
}

console.log(checkInArray2(gender))

var array = [1,2,3,4,5];
array.includes(2);     //true
array.includes(4);     //true
array.includes(1, 2);  //false (second parameter is the index position in this array at which to begin searching)

Check this out: https://playcode.io/373046

Rohit Kashyap
  • 1,553
  • 1
  • 10
  • 16
0

The HTML part of your question should look like this

<button onclick="helloFunction('female')">Marge</button>
<button onclick="helloFunction('male')">Henry</button>

The javascript then

function helloFunction(gendr) {
  if(gendr=="female") ...
}

I believe your problem is more about parameter passing and it is not about arrays.

Jan Turoň
  • 31,451
  • 23
  • 125
  • 169