r/learnjavascript • • 34m ago

Coursera Meta Front-End Developer Professional Certificate - HELP

Hello!

I am taking the Meta Front-End Developer Professional Certificate course on Coursera. I am totally new at this. In fact, this is a career change for me. I started this course under the impression it was for beginners. But I am stuck in course 2 of 9, the JavaScript portion. The assignment to get past module 2. I have submitted it 3 times and get the same exact grade. I have no idea what it is asking me to do at this point. I have put in a complaint. I have tried posting to their discussion board. They still have not approved my post. I have gone back through the modules and rewatched and reread everything. I have consulted YouTube videos, and apparently I'm still not doing it right? I am using studio VS like they suggested. Below is the code I wrote, the top data is the starter data they gave me and I will attach a screenshot of the assignment. I understand I am still missing info. But in my last submission, I had all 6 functions, but the score was still 40%. My first 2 attempts barely had 2 functions. I don't see how my score will still be 40%.

P.S. This is my first time posting on Reddit. If this is not the right spot or the right way, please let me know. Thank you in advance.

ASSIGNMENT:
Write a small set of JavaScript utility functions that use arrays, objects, and functions, apply common built-in methods, correctly handle undefined vs null vs "", and demonstrate defensive programming with throw and try/catch.

Submit one code block containing the starter data, all functions, Part C answers (as comments), and the required test calls with expected outputs.

1.

Question 1

Starter Data:

const users = [

{ id: 101, name: " Ada ", scores: [10, 20, 30] },

{ id: 102, name: "", scores: [5, 0, 15] },

{ id: 103, name: null, scores: [7, 14] },

{ id: 104, /* name missing on purpose */ scores: [3, 3, 3, 3] },

{ id: 105, name: "Grace", scores: [] }

];

Tasks (implement exactly these 6 items)

1. normalizeName(value) Return "Unknown" if value is null, undefined, or trims to ""; else return value.trim(). Must use trim() and typeof at least once.

2. averageScore(scores)

  • If not an array: throw new Error("scores must be an array")
  • If empty array: return null
  • Else return average rounded to 2 decimalsMust use Array.isArray() and Math.round() (or equivalent math rounding).

3. buildUserSummary(user)

  • If not a non-null object: throw new Error("user must be an object")Return { id, name, scoreCount, avg } using the rules:
  • name: normalizeName(user.name)
  • scoreCount: number of scores; if missing/not array use 0
  • avg: averageScore(scores); if missing/not array treat as empty → nullMust use dot and bracket notation at least once each.

4. summarizeUsers(userArray)

  • If not an array: throw new Error("userArray must be an array")
  • Return userArray.map(buildUserSummary)Must use map().

**5. safeSummarizeUsers(userArray)**Use try/catch around summarizeUsers. Return:

  • success: { ok: true, data: ... }
  • error: { ok: false, error: "<message>" }

6. getUserDisplayNameById(userArray, id)

  • If userArray not array: throw new Error("userArray must be an array")
  • If id not number: throw new Error("id must be a number")
  • Use find() to locate user by id; if not found: throw new Error("user not found")
  • Return normalizeName(found.name)

Part C:

// Part C answers:

// 1) typeof undefined = ?

// 2) typeof null = ?

// 3) Why treat "" differently than null/undefined in normalizeName (conceptually)?

Required test calls:

console.log(normalizeName(" Ada ")); // expected:

console.log(normalizeName(" ")); // expected:

console.log(normalizeName(null)); // expected:

console.log(averageScore([10, 20, 30])); // expected:

console.log(averageScore([])); // expected:

console.log(buildUserSummary(users[0])); // expected:

console.log(buildUserSummary(users[3])); // expected:

console.log(safeSummarizeUsers(users).ok); // expected:

console.log(getUserDisplayNameById(users, 105)); // expected:

console.log(safeSummarizeUsers("not an array")); // expected:

const users = [


  { id: 101, name: " Ada ", scores: [10, 20, 30] },


  { id: 102, name: "", scores: [5, 0, 15] },


  { id: 103, name: null, scores: [7, 14] },


  { id: 104, /* name missing on purpose */ scores: [3, 3, 3, 3] },


  { id: 105, name: "Grace", scores: [] }


];


function normalizeName(value) {
    if (typeof value !== "string") return "unknown";
    const cleaned = value.trim().replace(" ");
    return cleaned || "unknown";


}


const normalizedUsers = users.map(user => ({
    ...user,
    name: normalizeName(user.name)
}));
console.log(normalizedUsers)


function averageScore(users) {
    const validScores = users
    .flatMap(user => user.scores || [])
    .filter(score => score !== null && score !== undefined && String(score).trim() !== "")
    .map(Number)
    .filter(num => !Number.isNaN(num));


    if (validScores.length === 0) return 0;


    const total = validScores.reduce((sum, score) => sum + score, 0);
    const average = total / validScores.length;


    return Math.ceil(average);
}
console.log(averageScore(users));


function buildUserSummary(user) {
    const validScores = Array.isArray(user.scores)
    ? user.scores.filter(score => score !== "" && score !== null && !isNaN(Number(score))).map(Number)
    : [];


    const scoreCount = validScores.length;


const total = validScores.reduce((sum,score) => sum + score, 0);
const avg = scoreCount > 0 ? Math.round(total / scoreCount) : 0;


return {
    id: user.id,
    name: user.name,
    scoreCount,
    avg
};


}
const userSummaries = users.map(buildUserSummary);


console.log(userSummaries)


function summarizeUsers(userArray) {
    if (!userArray || userArray === 0) {
        return { totalUsers: 0, averageScore: 0 };
    }


    const totalScore = userArray.reduce((sum, user) => sum + (Number(user.score) || 0), 0);
    const averageScore = Math.round(totalScore / userArray.length);
    return {
        totalUsers: userArray.length,
        averageScore: averageScore
    };
}
const summary = summarizeUsers(users);
console.log(summary);


function safeSummarizeUsers(userArray) {
    if (!Array.isArray(userArray)) {
        return [];
    }
    return userArray.map((user)=> {
        try {
            return buildUserSummary(user);
        } catch (error) {
            return {
                name: "unknown",
                averageScore: 0,
                error: error.message
            };
        }
    });


}
const summaries = safeSummarizeUsers(users);
console.log(summaries);


function getUserDisplayNameById(userArray, id) {
    const user = userArray.find(u => u.id === id);
    return user ? user.displayName : "unknown";
}
const name = getUserDisplayNameById(users, 101);
console.log(name);
1 Upvotes

0 comments sorted by