This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Javascript: 101

My recent threads

You haven't posted any discussions yet.

Recently updated threads

Eloquent JavaScript Exercise 4.6 - Help!

Go back to: General discussion

OK, I've rewritten this a bunch of different ways and keep getting completely the wrong date! I'm sure it's something really obvious but I fear I've been staring at this way too long now..

Can anyone see where I'm going wrong and point me in the right direction?

function extractDate(paragraph{
  return new Date(Number(paragraph.slice(114)),
                                Number(paragraph.slice(821),
                                Number(paragraph.slice(52)));
}

show(extractDate("died 27-04-2006: Black Leclere"));

>Thu Nov 30 1899 00:00:00 GMT+0000 (GMT Standard Time)

dysert's picture
dysert
Thu, 2011-02-10 13:47

When you're slicing, you need to add in your beginning spot to the end spot. So instead of calling slice(11,4), call it like slice(11,11+4). The entire solution follows:

function extractDate(paragraph) {
return new Date(Number(paragraph.slice(11, 11+4)),
Number(paragraph.slice(8, 8+2) - 1),
Number(paragraph.slice(5, 5+2)));
}

show(extractDate("died 27-04-2006: Black Leclere"));

Dave Keith's picture
Dave Keith
Thu, 2011-02-10 14:09

Thank you - I knew it would be something simple! +1 rep for dysert. :-)