Troubleshooting a JavaScript script for tracking writing progress in Airtable

Hey everyone! I’m working on a script for an Airtable extension to help me track my writing progress. It’s supposed to do these things:

  1. Add up my written articles for the quarter
  2. Figure out how many more I need to write
  3. Tell me how many articles per week I should aim for
  4. Calculate how many weekdays I have for each remaining article

I’ve cobbled together some code from examples and help from a friend, but I’m running into errors. I think it’s because I’m not declaring variables correctly, which messes up the math.

Here’s a simplified version of what I’m trying to do:

let articleCount = 10;
let quarterlyGoal = 20;
let daysLeftInQuarter = 45;

function calculateProgress() {
  let remaining = quarterlyGoal - articleCount;
  let daysPerArticle = daysLeftInQuarter / remaining;
  let articlesPerWeek = remaining / (daysLeftInQuarter / 7);

  console.log(`Articles left: ${remaining}`);
  console.log(`Days per article: ${daysPerArticle.toFixed(1)}`);
  console.log(`Articles per week: ${articlesPerWeek.toFixed(1)}`);
}

calculateProgress();

Can anyone spot what I’m doing wrong or suggest a better way to structure this? I’m pretty new to JavaScript, so any tips would be super helpful. Thanks!

hey there, try const for quarterlyGoal since it never changes. watch out for dividing by zero if articles written match your goal. also, rounding with Math.ceil() for articles per week may help. hope this helps, happy coding!

I’ve been working with JavaScript for a while now, and I’ve encountered similar issues when trying to track my own writing progress. Your code looks pretty solid overall, but there are a few tweaks that might make it more robust.

First off, consider using ‘const’ instead of ‘let’ for variables that won’t change, like quarterlyGoal. This can help prevent accidental reassignments.

For the calculations, you’re on the right track. However, you might want to add some error handling. What if articleCount equals or exceeds quarterlyGoal? You’d end up dividing by zero.

Here’s a modified version that addresses these points:

const quarterlyGoal = 20;
let articleCount = 10;
let daysLeftInQuarter = 45;

function calculateProgress() {
  const remaining = Math.max(0, quarterlyGoal - articleCount);
  const daysPerArticle = remaining > 0 ? daysLeftInQuarter / remaining : 0;
  const articlesPerWeek = (remaining / daysLeftInQuarter) * 7;

  console.log(`Articles left: ${remaining}`);
  console.log(`Days per article: ${daysPerArticle.toFixed(1)}`);
  console.log(`Articles per week: ${articlesPerWeek.toFixed(1)}`);
}

calculateProgress();

This should give you more accurate results and prevent potential errors. Hope this helps!

Your code structure is sound, but there’s room for improvement. Consider using ‘const’ for unchanging variables like quarterlyGoal. Also, add error handling to prevent division by zero if articleCount exceeds or equals quarterlyGoal.

A key enhancement would be to round the results for better readability. Use Math.ceil() for articles per week to ensure you’re always aiming high enough to meet your goal.

Lastly, consider adding input validation to ensure all variables are positive numbers. This will make your script more robust against unexpected inputs.

Remember, clean code is as much about readability as functionality. Adding comments to explain your logic can be invaluable, especially when you revisit the code later or share it with others.