You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've recently been tinkering around with DataviewJS & using some arrays to manage some data within a function. However, I've found that somehow the array has an additional value upon creation, and seemingly cannot be removed by usual means.
Here is what I am trying to do:
let scores = [];
// Generate 4d6 into scores
for (i = 0; i < 4; i++) {
scores[i] = Math.floor(Math.random() * (6 - 1)) + 1;
}
// Drop the lowest number
let lowestDigitKey = 0;
for (i = 0; i <= scores.length; i++) {
if (scores[i + 1] < scores[i]) {
lowestDigitKey = i + 1;
}
}
scores.splice(lowestDigitKey, 1);
// Add up scores
let abilityScore = 0;
for (i = 0; i <= scores.length; i++) {
if (i < 3) {
dv.paragraph(scores[i]);
abilityScore += scores[i];
}
}
return abilityScore;
Which produces 3 numbers within the range i.e.
4
5
5
However, if I remove the if (i < 3) condition, a dash (-) is shown at the end of the array, and thus is thrown into the abilityScore calculation, producing NaN when I display abilityScore.
3
2
5
-
While I can technically solve this issue by keeping the if (i < 3) condition, it's not very practical for other implementations that may not have a fixed max length, even if the condition was changed to i < scores.length - 1.
Has anyone else ran into this issue, or is there another approach I should use to do this?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I've recently been tinkering around with DataviewJS & using some arrays to manage some data within a function. However, I've found that somehow the array has an additional value upon creation, and seemingly cannot be removed by usual means.
Here is what I am trying to do:
Which produces 3 numbers within the range i.e.
However, if I remove the
if (i < 3)
condition, a dash (-) is shown at the end of the array, and thus is thrown into theabilityScore
calculation, producing NaN when I displayabilityScore
.While I can technically solve this issue by keeping the
if (i < 3)
condition, it's not very practical for other implementations that may not have a fixed max length, even if the condition was changed toi < scores.length - 1
.Has anyone else ran into this issue, or is there another approach I should use to do this?
Beta Was this translation helpful? Give feedback.
All reactions