Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Array Iteration Methods Array Manipulation Practice map()

Could someone please help me pass this challenge?

I don't understand why this code won't work. I've tested this calling abbreviateString() on and it works, but for some reason I'm not passing this challenge. I'm getting unexpected identifier at line 12 (no idea why). Any help will be very appreciated.

Thank you!

app.js
const daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
let abbreviatedDays;

// abbreviatedDays should be: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
// Write your code below

const abbreviateString = string => {
    let result = '';
    let i = 0;

    for (let character of string){
        if (i < 3){
            result += character;
        }
        else {
            break;
        }
        i += 1;
    }
    return result;
};


abbreviateDays = daysOfWeek.map(abbreviateString);

1 Answer

First of all your syntax is wrong and ur being ask to manipluate the data with the map() method. You first need to think the steps to do this:

  1. u need to somehow loop thorugh an array in a way that let u modify the array >> You can do it with the .map() method! 2.u need to think how to cut the first 3 letters from each day in the array >> You can do it with the substring() method , reference:https://www.w3schools.com/jsref/jsref_substring.asp

try to watch the videos again and try again the challange, ull figure this out, if not let me know and ill post the answer

Thank you for helping with the substring method!

But regarding the syntax, where is it incorrect exactly? I don't think it's incorrect at all because a ran this same code in VS Code and it ran ok. The abbreviated array was created correctly. And I'm also using the map method. Before using it, I created a function that works kinda like what substring() would in this case. But I do think the challenge will only accept if the substring method is used. Anyway, thank you for taking the time!