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!