1

I have two arrays. They are always the same length. If userEmotions[index] value is true I need to add userTimeData[index] value to a variable:

var userEmotions = [false, true, false, true],
    userTimeData = [140, 320, 730, 50],
    userPercentage = 0;

// e.g add 320 and 50 to userPercentage:
// userPercentage == 370

How do I go about achieving this?

5
  • 1
    do you need just a single value or an array of values? what have you tried? Commented Feb 14, 2018 at 14:53
  • 3
    What about a simple for? Commented Feb 14, 2018 at 14:54
  • 5
    "How do I go about achieving this?" -- you take the third sentence from the text and turn it into code. As simple as: for (index in userEmotions) { if (userEmotions[index]) { userPercentage += userTimeData[index]; } }. Not enough to compute a percentage, but you can use it as a starting point. Commented Feb 14, 2018 at 14:54
  • userPercentage = userEmotions.reduce((sum, e, i) => sum + (e && userTimeData[i]), 0) Commented Feb 14, 2018 at 14:56
  • @NinaScholz an array of values. Commented Feb 14, 2018 at 15:12

5 Answers 5

2

You could use Array#indexOf for the first single value or zero as default value.

var userEmotions = [false, true, true],
    userTimeData = [140, 320, 730],
    userPercentage = userTimeData[userEmotions.indexOf(true)] || 0;

console.log(userPercentage);

For getting an array of values, you could filter the values.

var userEmotions = [false, true, true],
    userTimeData = [140, 320, 730],
    userPercentages = userTimeData.filter((_, i) => userEmotions[i]);

console.log(userPercentages);

Sign up to request clarification or add additional context in comments.

5 Comments

Oh, I was just demonstrating that your code doesn't give the correct answer when the number of trues is not one.
@BuhBuh, the user wants to get a single value, and the first true value's index should be taken for the result, as the title of the post asks. what goes wrong with another true in the userEmotions array?
I see now you have interpenetrated the question differently. I understood first to refer to the first array, where you took it to mean first boolean. Maybe you are right. The question is ambiguous.
Now that I read the question again, yes it is ambiguous and I meant first array. userPercentages.reduce((a, b) => a + b, 0). Out of curiosity what does the (_, part in filter() do?
_ is just a variable, which is not used.
2

You can use a for loop:

var userEmotions = [false, true, false],
  userTimeData = [140, 320, 730],
  userPercentage = 0;

for (i = 0; i < userEmotions.length; i++) { //loop the length of the array
  if (userEmotions[i]) { // check userEmotions is true
    userPercentage += userTimeData[i]; // if it is add userTimeData to percentage
  }
}

console.log(userPercentage);

Comments

1

The OP wanted alternative approaches, so:

userEmotions = [false, true, false],
userTimeData = [140, 320, 730],
userPercentage = userEmotions
    .map(x => x ? 1 : 0)
    .map((x, i) => x * userTimeData[i])
    .reduce((a,b) => a+b);

console.log(userPercentage);

Comments

0

Why over engineer it, a simple for loop solves your problem -

var userEmotions = [false, true, false],
    userTimeData = [140, 320, 730],
    userPercentage = 0;

for (i = 0; i < userEmotions.length; i++) {
  if (userEmotions[i]) {
    userPercentage += userTimeData[i];
  }
}

console.log(userPercentage);

Comments

0

can you try this :

for(let i = 0 ; i < userEmotions .length ; i++){
   if(userEmotions[i]){
       userPercentage = userTimeData[i]
   }
}

Let me know if it help you :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.