2

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.

2
  • 4
    z = milestones.length should be z < milestones.length Commented Nov 5, 2019 at 19:43
  • To elaborate on Barmar's comment above, z = something will not compare z to something, it will assign z as something. In JavaScript, equality comparison is done using == or ===. Also, in this case of a for loop, 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. Commented Nov 5, 2019 at 19:51

1 Answer 1

2

z = milestones.length should be z < milestones.length.

You never call wordsPerDayF().

newWordCount += newWordCount will double the word count each day, but that's not what you really want. You just want to add newWordCount to a total. You can also use w * newWordCount to get the total for each day.

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 * w)
  };
  let count = 1;
  for (let z = 0; z < milestones.length; z++) {
    finalMilestones.push("Day " + count + ": " + milestones[z]);
    count++;
  };
};

function final() {
  wordsPerDayF(50000);
  createMilestones();
  let str = '<ul>'
  finalMilestones.forEach(function(item) {
    str += '<li>' + item + '</li>';
  });
  str += '</ul>';
  document.getElementById("wordsNeeded").innerHTML = str;
};
final();
<div id="wordsNeeded"></div>

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

1 Comment

This worked! Thank you so much! I appreciate the help here.

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.