Course: How To Make JS Your Own. Please help!


#1


https://www.codecademy.com/en/courses/javascript-intermediate-en-2nQQb/1/3


Oops, try again. The date being passed back doesn't always print out the date in the YYYY/MM/DD format.



Date.prototype.toYYYYMMDD = function() {
	var strReturn = "";
	// Add the code that will take the date and format it like this:
	// YYYY/MM/DD
	
	var date = new Date();
    var day = (date.getDate()).toString();
    var month = (date.getMonth() + 1);
    var year = (date.getFullYear()).toString();

    if (month > 0 && month < 10) {
        month = '0' + month.toString();
    }
    
    strReturn = year + '/' + month + '/' + day;
    return strReturn;
};

I also tried this solution and got the same result:

Date.prototype.toYYYYMMDD = function() {
	var strReturn = "";
	// Add the code that will take the date and format it like this:
	// YYYY/MM/DD
	
	//var date = new Date();
    var day = (this.getDate()).toString();
    var month = (this.getMonth() + 1);
    var year = (this.getFullYear()).toString();

    if (month > 0 && month < 10) {
        month = '0' + month.toString();
    }
    
    strReturn = year + '/' + month + '/' + day;
    return strReturn;
};

var date = new Date();
console.log(date.toYYYYMMDD());

#2

this seems to be part of a track that is no longer maintained? either way, looking at the checking script:

var date1 = new Date("2012/09/01");
var date2 = new Date("2012/12/31");

if(date1.toYYYYMMDD() != "2012/09/01" || date2.toYYYYMMDD() != "2012/12/31") {
		return "The date being passed back doesn't always print out the date in the YYYY/MM/DD format.";
	}

they pass dates into the instance of the object, and then expect this to print, a total different scenario then painted by the instructions


#3

@stetim94
Is it possible to complete somehow this course?

Thanks,
Mark


#4

maybe, i don't have time to look into it now. I have running training. I will ask one of the other moderators, or look into it myself after running training


#5

Ok, thanks, I will try for my part too...


#6

OKay you're on the right track.

This bit handles adding a 0 to months that are only 1 digit.

What about for days that are less than 10 though?


#7

It's not an SCT isssue. This lesson can be passed.


#8

This is closer than the first example. Consider that this is extending the Date class prototype, so is itself a member of that class, hence, this. The method has access to all the methods without invoking the class constructor.


#9

@alexcraig

Thank you for the hint, I changed this condition to the followig:

    if (month > 0 && month < 10) {
        month = '0' + month.toString();
    } else {
        month = month.toString();
    }

but always the same thing: "Oops, try again. The date being passed back doesn't always print out the date in the YYYY/MM/DD format."

The strange thing here is that the date is displayed in the correct format:

Hence it can be concluded that this method is working properly!


#10

@alexcraig makes a good point, in two days it is November first, then your method would print:

2016/10/1

this should be 01, i would say you need to apply the same trick as you did for months.


#11

@alexcraig
@mtf
@stetim94

This is amaizing, thank you guys for your help.
I changed my code to:

Date.prototype.toYYYYMMDD = function() {
	var strReturn = "";
	// Add the code that will take the date and format it like this:
	// YYYY/MM/DD
	
	//var date = new Date();
    var day = this.getDate();
    var month = (this.getMonth() + 1);
    var year = (this.getFullYear()).toString();

    if (month > 0 && month < 10) {
        month = '0' + month.toString();
    } else {
        month = month.toString();
    }
    
    if (day > 0 && day < 10) {
        day = '0' + day.toString();
    } else {
        day = day.toString();    
    }
    
    strReturn = year + '/' + month + '/' + day;
    return strReturn;
};

var date = new Date();
console.log(date.toYYYYMMDD());

and now it works!!! finely, it wooorksss!!! :slight_smile:

Special thanks to @alexcraig and to @mtf! I had an overwhelming desire to find out the cause of this problem, and you have helped me a lot in this. Guys, I express my gratitude to you and wish you a nice day.:wink:

Mark K.


#12

Glad you managed it!

If you want to compact your code a little, you could experiment with ternary operations to get all this down to one line.


#13

The ternary idea is a good one. Back in the day I opted for manipulating the string data in place as opposed to using control flow logic. Now that you've passed the lesson, I don't think this requires a spoiler alert.

Date.prototype.toYYYYMMDD = function() {
    var strReturn = "", s = "";
    strReturn += this.getFullYear() + "/";
    s = "0" + (this.getMonth() + 1).toString(10);
    strReturn += s.substr(s.length - 2, 2) + "/";
    s = "0" + (this.getDate()).toString(10);
    strReturn += s.substr(s.length - 2, 2);
    return strReturn;
};

#14

@alexcraig
@mtf

Thank you so much for all your help! I really appreciate it!


#15

Giving the above a re-think, since by rights strings are treated as immutable. we have this refactoring...

Date.prototype.toYYYYMMDD = function() {
    setLead = (x) => {
        x = ["0", x.toString(10)].join('');
        return x.substr(x.length - 2, 2);
    };
    var s = [];
    s.push(this.getFullYear());
    s.push(setLead(this.getMonth() + 1));
    s.push(setLead(this.getDate()));
    return s.join("/")
};

So many ways to cook an egg!

Prototypal Date Format Function


#16

I do like the old courses, seem to give you so much more flexibility.


#17

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.