JavaScript 刷题一
最近读<JavaScirpt编程精解>,想把里面的三个大的程序实现,现在记录下来.
问题一:
从下面这封信中,emily奶奶每封信的结尾都会用同样的格式注明哪只猫出生了,哪只猫死去了.现要求提取cat的信息,要求知道cats的bitrth,date,name.
格式如下:
输入:mailArchives 数组
输出:
{ Spot: { name: 'Spot', birth: Wed Mar 05 1997 00:00:00 GMT+0800 (CST), mother: 'unknown' }, ... ... }
信的格式如下:
Dear nephew, Your mother told me you have taken up skydiving. Is this true? You watch yourself, young man! Remember what happened to my husband? And that was only from the second floor! Anyway, things are very exciting here. I have spent all week trying to get the attention of Mr. Drake, the nice gentleman who moved in next door, but I think he is afraid of cats. Or allergic to them? I am going to try putting Fat Igor on his shoulder next time I see him, very curious what will happen. Also, the scam I told you about is going better than expected. I have already gotten back five 'payments', and only one complaint. It is starting to make me feel a bit bad though. And you are right that it is probably illegal in some way. (... etc ...) Much love, Aunt Emily died 27/04/2006: Black Leclère born 05/04/2006 (mother Lady Penelope): Red Lion, Doctor Hobbles the 3rd, Little Iroquois
我的代码完成如下:
var mailArchive={ 0:"Dear nephew,\ \nYour mother told me you have taken up skydiving. Is this true? You watch yourself, young man! Remember what happened to my husband? And that was only from the second floor!\ \nAnyway, things are very exciting here. I have spent all week trying to get the attention of Mr. Drake, the nice gentleman who moved in next door, but I think he is afraid of cats. Or allergic to them? I am going to try putting Fat Igor on his shoulder next time I see him, very curious what will happen.\ \nAlso, the scam I told you about is going better than expected. I have already gotten back five 'payments', and only one complaint. It is starting to make me feel a bit bad though. And you are right that it is probably illegal in some way.\ \nMuch love, Aunt Emily\ \ndied 27/04/2006: Black Spot\ \nborn 05/04/2006 (mother Lady Penelope): Red Lion, Doctor Hobbles the 3rd, Little Iroquois", 1:"Dear nephew,\ \nYour mother told me you have taken up skydiving. Is this true? You watch yourself, young man! Remember what happened to my husband? And that was only from the second floor!\ \nAnyway, things are very exciting here. I have spent all week trying to get the attention of Mr. Drake, the nice gentleman who moved in next door, but I think he is afraid of cats. Or allergic to them? I am going to try putting Fat Igor on his shoulder next time I see him, very curious what will happen.\ \nAlso, the scam I told you about is going better than expected. I have already gotten back five 'payments', and only one complaint. It is starting to make me feel a bit bad though. And you are right that it is probably illegal in some way.\ \nMuch love, Aunt Emily\ \ndied 27/04/2007: White Star\ \nborn 30/05/2006 (mother LaLy Penelope): Black spot" } var livingCats={"a":true}; var cats = {"Spot": catRecord("Spot", new Date(1997, 2, 5), "unknown")}; for(var mail=0;mail<2;mail++) { var paragraphs=mailArchive[mail].split("\n"); for(var i=0;i<paragraphs.length;i++) { if(startswith(paragraphs[i],"born")) addCats(cats,catNames(paragraphs[i]),extractDate(paragraphs[i]),extractMother(paragraphs[i])); else if(startswith(paragraphs[paragraph],"died")) deadCats(cats,catNames(paragraphs[i]),extractDate(paragraphs[i]),extractMother(paragraphs[i])); } } console.log(cats); function extractMother(paragraph) { var start=paragraph.indexOf("mother")+"mother".length; var end=paragraph.indexOf(")"); return paragraph.slice(start,end); } function extractDate(paragraph) { function numberAt(start,length){ return Number(paragraph.slice(start,start+length)) } return new Date(numberAt(11,4),numberAt(8,2),numberAt(5,2)); } function catRecord(name,birthdate,mother) { return {name: name,birth:birthdate,mother:mother}; } function addCats(set,names,birthdate,mother) { set[names]=catRecord(names,birthdate,mother); } function deadCat(set,names,birthdate) { set[names].death=birthdate; } function catNames(paragraph) { var colon=paragraph.indexOf(":"); return paragraph.slice(colon+2).split(",")[0]; } function startswith(string,pattern) { return string.slice(0,pattern.length)==pattern; }