I'm building a word count tracker and would like to show how many words needed per day to reach a target within 30 days. For example if someone enters in 50,000 as their target, they'll have to complete 1667 words per day. I want to create a list element that would say:
Day 1: 1667
Day 2: 3334
Day 3....etc.
My current plan was to create an array that contained the list elements, but the script seems to be causing the site to break entirely! I'm not sure what's going on here.
let wordsPerDay = 0;
function wordsPerDayF(x) {
wordsPerDay = x / 30;
wordsPerDay = Math.round(wordsPerDay)
document.getElementById('wordsNeeded').innerHTML = wordsPerDay.toString();
};
// Above functions finds the word count needed per day
let milestones = []
let finalMilestones = [];
function createMilestones() {
let newWordCount = wordsPerDay;
for (let w = 1; w <= 30; w++) {
milestones.push(newWordCount)
newWordCount += newWordCount;
};
let count = 1;
for (let z = 0; z = milestones.length; z++) {
finalMilestones.push("Day " + count + ": " + milestones[z]);
count++;
};
};
function final() {
createMilestones();
let str = '<ul>'
finalMilestones.forEach(function(item) {
str += '<li>' + item + '</li>';
});
str += '</ul>';
document.getElementById("wordsNeeded").innerHTML = str;
};
final();
<div id="wordsNeeded"></div>
I'm new to coding, so apologies if this code is rough or hard to understand.
z = milestones.lengthshould bez < milestones.lengthz = somethingwill not compare z to something, it will assign z as something. In JavaScript, equality comparison is done using==or===. Also, in this case of aforloop, it's usual that you don't want the iteration where z is exactly equal to the length (it starts at zero and ends at length-1), hence the use of<instead.