Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
Is there a way to use items in an array as booleans? Node.js

I'm working on a dungeon crawler in node.js. I wanted an elegant way to keep track of what endings a user has. I was thinking it would be nice if i could have an array of each of the endings and then just set an item to true if that ending is achieved. This would make it easy to go through the list and print the achieved endings then calculate the percentage of completion.

My attempt at doing this:

var end1 = true
var end2 = true
var end3 = false
var end4 = true
var end5 = false

var endings = [end1, end2, end3, end4, end5]

function listEndings() {
  console.log("You have found these endings:")
  var total = 0

  for (let i = 0; i < endings.length; i++) {
    if (endings[i] = true) {
      console.log(String(endings[i]))
      total = total + 1
    }
  }

  console.log(`\nIn total you have ${total}/${endings.length + 1} endings. (${(total/(endings.length + 1))*100}%)`)
}

The output I would like

You have found these endings:
end1
end2
end4

In total you have 3/5 endings. (60%)

Is is possible to use a method like this? Is there another method you would recommend?

Thank you very much for the help!

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I think it's better to store the values as an array storing the endings the player has already found:

let endingsFound = []
const totalEndings = 5
//(as a side note, better use let than var)

console.log('Endings found')
//suppose we find endings three and four
endingsFound.push(3)
endingsFound.push(4)

endingsFound.forEach((end) => console.log(`Ending ${end}`))

console.log(`In total you have found ${endingsFound.length +1}/${totalEndings}`);

if you're still determined to store it as an array of booleans, then use the following function:

const indices = arr.reduce(
    (out, bool, index) => bool ? out.concat(index) : out, 
    []
  )

as was presented here to get to an array of the endings found and then do the same.

about 4 years ago · Juan Pablo Isaza Report

0

It would be easy and straightforward to use object instead of an array and use Object.entries to loop over the key and value to get the desired result.

var end1 = true;
var end2 = true;
var end3 = false;
var end4 = true;
var end5 = false;

var endings = { end1, end2, end3, end4, end5 };

function listEndings() {
  let count = 0, entries = Object.entries(endings);
  
  console.log("You have found these endings:");
  for (let [k, v] of entries) {
    if (v) {
      console.log(`${k}`);
      ++count;
    }
  }

  console.log(
    `In total you have ${count}/${entries.length} endings. (${
      (count * 100) / entries.length
    }%)`
  );
}

listEndings();

about 4 years ago · Juan Pablo Isaza Report

0

in this case, declare array variable that the contain is object. so, your data is flexible. you can add data anytime if some condition you'll need.

use the filter and map funtion to create output like that

var endings = [
{"no": 1,"bool": true},
{"no": 2,"bool": true},
{"no": 3,"bool": false},
{"no": 4,"bool": true},
{"no": 5,"bool": false},
{"no": 6,"bool": true},
{"no": 7,"bool": false}
]

var result = endings.filter((a)=>a.bool==true)
var resultMsg = result.map((a)=> `end${a.no}\n`)
var resultVal = parseInt((result.length/endings.length)*100)

console.log(`You have found these endings:\n ${resultMsg}`)
console.log(`\nIn total you have ${result.length}/${endings.length} endings. (${resultVal}%)`)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!