JavaScriptは書いてみます

8684 ワード

最近<JavaScriptプログラミングの精解>を読んで、中の三つの大きいプログラムを実現したいです.今記録します.
問題1:
次の手紙の中から、emilyおばあさんは手紙の最後に、どの猫が生まれたのか、どの猫が死んだのかを同じフォーマットで書いています.catの情報を抽出して、catsのbitrth、date、nameを知るように要求します.
フォーマットは以下の通りです
入力:mail Archives配列
出力:
{   

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,\


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.\
Much love, Aunt Emily\
died 27/04/2006: Black Spot\
born 05/04/2006 (mother Lady Penelope): Red Lion, Doctor Hobbles the 3rd, Little Iroquois
", 1:"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.\
Much love, Aunt Emily\
died 27/04/2007: White Star\
born 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("
"); 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; }