Challenge 15 Megathread

I hope this helps clarify something…

Math.round() 

It will round up OR down depending what the value after the decimal is. Up if its >= .5 and down if it is < .5

Math.ceil()

It will round up as long as there is any value after the decimal. So 1.7 rounds up to 2 and 9.00000001 would round up to 10. This might help people when it comes to figuring out how many days till the launch.

As a side note, depending on what order you have your variables in when you calculate the time difference, you may get a negative number.

Math.abs()

This will give you the absolute value of a number. So whether you have a negative number or not, this will give you a positive number to work with. When comparing dates, who knows if the date is in the future or the past. I believe this should allow you to calculate for both.

4 Likes
... launchDate.getDate() - today.getDate();

For my solution, it uses a subtraction between two getDate()s from the input dates, but it would only work if the dates are of the same month from my understanding. Is that correct?

Hi,

I think I might have done something stupid and wold like to begin anew.

Is there any way to ‘reset’ to the original code when I first opened the challenge?

Thank you,

Thank you for the explanation.

At first I ran the code without the Math.round( ) and it passed all tests. I did not get a fractional amount of days using :

let daysRemaining = (takeOffDate - fakeToday)/(1000 * 60 * 60 * 24);

To make it more general I then added rounding in case the launchDate included more than YYYY-MM-DD.

Yes, this only works within the same month as per the link in the hint section:

This was the code before any input from participants:

const timeRemaining = (launchDate, missionName, fakeToday) => {
const today = fakeToday || new Date(); // Do not alter this line!

// Code here!

This is what worked for me. It may look complicated and I am sure I could break it down further. But it works. Lol.

Math.ceil((Math.abs(Date.parse(fakeToday) - Date.parse(launchDate)))/(1000 * 60 * 60 * 24))

That gave me the days between the two dates. No matter what order I have the variables in (launchDate - today) OR (today - launchDate). If you reverse your variable order in your calculation, does it still pass?

1 Like

I am struggling with figuring out why I fail the first test, here is my code:


const timeRemaining = (launchDate, missionName, fakeToday) => {
  const today = fakeToday || new Date() // Do not alter this line!

  // Code here!
  let replacedLaunch = launchDate.replace(/-/g,'');
  let replacedFake = fakeToday.replace(/-/g,'');
  
  let countDays = replacedLaunch - replacedFake;
  
  return `${countDays}: ${missionName}`;
  
}
console.log(timeRemaining);

The first fail says: Reason: Your code could not run. “Cannot read property ‘replace’ of undefined”

I understand that it wants an object and I will work that part out, but why does it say it can’t read replace and that launchDate is undefined? Isn’t there a value already assigned to it like in the other challenges?

Thank you!

I think it is because you are returnning a string when it is basically looking for an array. If you assign countDays and missionName to properties of an object and then pass that object, it should work.

let objRemaining = {
   daysRemaining: countDays,
   missionName: missionName
};
return objRemaining;
}

EDIT: Nope. That didn’t help. That is an issue but the problem is with your calculation of countDays.

Instead of launchDate.replace and today.replace try Date.parse(launchDate) and Date.parse(today)

1 Like

The reason your code is failing is because fakeToday gets passed as a null in some of the test cases. You should try using the today value as this will always be defined as a Date Object.

The Lighthouse Team explained in a previous post:

This being said, though I have not tested it, I don’t think that parsing the string value of a Date Object would work. Try initializing launchDate as a Date Object (new Date(launchDate)) and using the built-in functions of the Date Object to perform the calculations.

My code would fail if I swapped the values in my subtraction calculation, however we always know that launchDate will be greater than today. Therefore there is no reason to find the absolute value of this calculation.

In my personal opinion I would also like my code to tell me if I have completely missed the launch of the rocket by getting a negative number of days past the launch date, though I do not believe that the tests are checking for this at all.

EDIT: (ran out of replies for today lol)
I re-read the requirements and it states calculate the difference, therefore you are absolutely right, Math.abs() should be there! Thanks @Sonicthecronic

I agree with you. It’s the little details, forethought and error checking that make good code. But would you not agree that coding instruction and get quite literal with the explainations sometimes? Lol, It’s not what they say, but what they don’t say that you have to watch out for.

Thank you very much !

One final thought before I take off…

"A part of the code has been written for you, and it is important that it stays the same for the tests to work out."

One thing I love about coding is that there is usually more than one way to get your answer. It is possible to do the challenge without keeping that first line of code they tell you not to delete. I was able to get my code down to just six lines. seven if you count the final bracket. Is it possible to get a shorter answer? I’m not that fluent in JS to know all the built-in functions.

you could cut it down to two lines but that does not make good code. Good code is not the fewest lines at all. Solving the problem, Readability, Efficency of the execution, Maintainability, loosely coupled, and DRY code, are some things to measure good code by. Readability and maintainability go hand in hand with self-documenting code so good variable names , function and/or objects that clearly state what they are for and what they are doing. ie. sometimes using longer syntax pulling out and defining variables for elements when needed instead of jamming things in one line allow you to read and understand your code when you or anyone else looks at it months or years from now.

3 Likes

image
Took longer than usual because, the example input given for this challenge has fakeToday created as a string, but the actual problem passes a Date object for fakeToday.

2 Likes

I agree, short code isn’t the best way unless it’s absolutely clear and easy to follow.

I’m guilty in a least one instance of actually hard coding in a buffer overflow into a program to change the action of the program while it runs, and then changing the action back to normal before exiting. I’ve only done it once, and yes, it’s terrible code, but it works, and stupidly, it is merely a fix on someone else’s code that was terrible but I can’t edit it as it was encrypted.

Will anyone ever be able to follow my code in that program? I doubt it. It was kind of a crowning achievement to me though.

2 Likes

The other thing … const today = fakeToday || new Date(); …does (and, I think, its true purpose) is that if fakeToday is null then the current date is returned

1 Like

Today’s challenge was good. The instructions were essentially wrong (claimed to be passing us 3 strings). I’m not sure if that was on purpose, but it certainly does reflect real world scenarios, especially as you start to work with 3rd party systems (like APIs).

Probably far to advanced for day 15, but forcing devs to not assume an input and actually test for it is an essential step to learning.

Also good for learners here to remember. It never hurts to run your first test as a debug. Console log everything and see what you’re working with.

1 Like

Hi Im having trouble with my code. My error message is “timeRemaining should return an object” and I do not know how to fix it. Here is my code:

const timeRemaining = (launchDate, missionName, fakeToday) => {
const today = fakeToday || new Date() // Do not alter this line!

// Code here!
today.missionName = missionName;
today.daysRemaining = (Date.prototype.getDate(launchDate) - Date.prototype.getDate(fakeToday));

return today;
}