Bug related to Build an fCC Forum Leaderboard - Build an fCC Forum Leaderboard

Tell us what’s happening:

I have this bug related to Test No. 14.
The text is correct but the test is not passing for some reason.
I tried with another browser, Firefox, and it didn’t work as well.

Your code so far

<!-- file: index.html -->

/* file: styles.css */

/* file: script.js */
const forumLatest =
  'https://cdn.freecodecamp.org/curriculum/forum-latest/latest.json';
const forumTopicUrl = 'https://forum.freecodecamp.org/t/';
const forumCategoryUrl = 'https://forum.freecodecamp.org/c/';
const avatarUrl = 'https://cdn.freecodecamp.org/curriculum/forum-latest';


const allCategories = {
  299: { category: 'Career Advice', className: 'career' },
  409: { category: 'Project Feedback', className: 'feedback' },
  417: { category: 'freeCodeCamp Support', className: 'support' },
  421: { category: 'JavaScript', className: 'javascript' },
  423: { category: 'HTML - CSS', className: 'html-css' },
  424: { category: 'Python', className: 'python' },
  432: { category: 'You Can Do This!', className: 'motivation' },
  560: { category: 'Backend Development', className: 'backend' }
};

function timeAgo(time) {
  const date = new Date();
  const lastDate = new Date(time);
  const minute = Math.floor((date - lastDate) / 60000);
  const hour = Math.floor((date - lastDate) / 3600000);
  const day = Math.floor((date - lastDate) / 86400000);

  if (minute < 60) {
    return `${minute}m ago`
  } if (hour < 24) {
    return `${hour}h ago`
  } else {
    return `${day}d ago`
  }
}


function viewCount(view) {
  if (view >= 1000 && view <= 9999) {
    const result = view.toString().slice(0, 1) + "k"
    return result;
  } else if (view > 9999) {
    const secondResult = view.toString().slice(0, 2) + "k"
    return secondResult;
  }
  return view;
}

function forumCategory(catId) {
  if (Object.hasOwn(allCategories, catId)) {
    const result = `
    <a class="category ${allCategories[catId].className}" href="${forumCategoryUrl}${allCategories[catId].className}/${catId}">${allCategories[catId].category}</a>`
    return result;
  }
}

// console.log(allCategories[299])
console.log(forumCategory(299))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36

Challenge Information:

Build an fCC Forum Leaderboard - Build an fCC Forum Leaderboard

Update: When I eliminated the const and added the a tag directly to return the test passed. But I think it’s a bug that needs to be fixed because the output didn’t change.

  1. If the allCategories object does not have the selected category id as its property, category should be indicated as General and className should be indicated as general.

Does your code meet this requirement?

Is your forumCategory function always returning something?

I actually was about to do that but I was talking about the bug that when I added the a tag to a variable and returned the variable, the test algorithm invalidated my code, even though the output is correct and when I removed the variable and returned directly, the test passes

There’s no bug. Finish implementing the user stories and then run the tests. The tests are not designed to validate every bit of code you write. They are designed to test your final code with all user stories implemented.

Did you run the code? I meant this part

function forumCategory(catId) {

  if (Object.hasOwn(allCategories, catId)) {

    const result = `<a class="category ${allCategories[catId].className}" href="${forumCategoryUrl}${allCategories[catId].className}/${catId}">${allCategories[catId].category}</a>`;
    return result;

  }

}




console.log(forumCategory(299))

If you did this way (the way I did), Test No. 14 forumCategory(299) should return a string containing an anchor element with the text Career Advice. wouldn’t pass.

But if you did this way

function forumCategory(catId) {

  if (Object.hasOwn(allCategories, catId)) {

    return `<a class="category ${allCategories[catId].className}" href="${forumCategoryUrl}${allCategories[catId].className}/${catId}">${allCategories[catId].category}</a>`

  }

}


console.log(forumCategory(299))

The test passes even though the output of both are the same.

How can the tests check anything in that function when it only returns a value if a specific condition is met? The bug is in your code, or lack thereof.

What do you mean? If you run both the codes you will have the same result.

Feel free to create an issue at GitHub Issues.

Ok, so I completed the code and it passes even if I use the variable way.

While I was making the code, the test werent passing and I was confused for why it wasn’t passing.

Thanks for the help

Turns out if you eliminate the newline between the first backtick and the opening anchor tag, that test does pass.

But it’s a bad idea to depend on the tests to validate your code as you write it. Finish implementing the user stories first, then run the tests.

Ok. Thank you very much