Build an fCC Forum Leaderboard - Build an fCC Forum Leaderboard

Tell us what’s happening:

I fail the test : 33. The second td element of each table row from the string returned by showLatestPosts should contain the images returned by the avatars function called with posters and users as arguments, nested within a div element with the class of avatar-container.

I tried many ways, removing \n between rows, returning the html content… nothing works

Your code so far

/* 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 postsContainer = document.getElementById("posts-container");

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: 'Back-End Development', className: 'backend' }
};

function timeAgo(iso_timestamp) {
    const from = Date.parse(iso_timestamp);
    const to = Date.now();
    const elapsed_minutes = (to - from) / (1000 * 60);

    if (elapsed_minutes < 60) {
        return `${Math.floor(elapsed_minutes)}m ago`;
    }

    const elapsed_hours = elapsed_minutes / 60;
    if (elapsed_hours < 24) {
        return `${Math.floor(elapsed_hours)}h ago`;
    }

    const elapsed_days = elapsed_hours / 24;
    return `${Math.floor(elapsed_days)}d ago`;
}

function viewCount(views) {
    if (views >= 1000) {
        return `${Math.floor(views / 1000)}k`
    }
    return views;
}

function forumCategory(categoryId) {
    const categoryLink = document.createElement("a");
    const category = allCategories[categoryId];
    const className = category ? category.className : "general";
    categoryLink.textContent = category ? category.category : "General";
    categoryLink.href = `${forumCategoryUrl}${className}/${categoryId}`;
    categoryLink.className = `category ${className}`;
    return categoryLink.outerHTML;
}

function avatars(posters, users) {
    const posterIds = posters.map((poster) => poster.user_id);
    const avatar_imgs = users
        .filter((user) => posterIds.includes(user.id))
        .map((filteredUser) => {
            let src = filteredUser.avatar_template[0] === "/"
                ? avatarUrl + filteredUser.avatar_template
                : filteredUser.avatar_template;
            src = src.replace("{size}", "30");
            return `<img src="${src}" alt="${filteredUser.name}">`
        });
    return avatar_imgs.join("");
}

function showLatestPosts(json_response) {
    const topicTableRows = [];
    for (const topic of json_response["topic_list"]["topics"]) {
        topicTableRows.push(
            `<tr>
                <td>
                    <a class="post-title" href="${forumTopicUrl}${topic.slug}/${topic.id}">${topic.title}</a>
                    ${forumCategory(topic.category_id)}
                </td>
                <td><div class="avatar-container">${avatars(topic.posters, json_response.users)}</div>
                </td>
                <td>${topic.posts_count - 1}</td>
                <td>${viewCount(topic.views)}</td>
                <td>${timeAgo(topic.bumped_at)}</td>
            </tr>`
        )
    }
    const htmlContent = topicTableRows.join("");
    postsContainer.innerHTML = htmlContent;
    return htmlContent;
}

async function fetchData() {
    try {
        const res = await fetch(forumLatest);
        const data = await res.json();
        console.log(data);
        showLatestPosts(data);
    } catch (error) {
        console.log(error);
    }
}

Your browser information:

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

Challenge Information:

Build an fCC Forum Leaderboard - Build an fCC Forum Leaderboard

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-fcc-forum-leaderboard/673c91f0b934834bc4a3ecc2.md at main · freeCodeCamp/freeCodeCamp · GitHub

I vaguely remember running into a similar problem on this one - this guy had the same problem, and his solution works.

Thanks it helped so much !