zoukankan      html  css  js  c++  java
  • 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,
    
    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;
    }
  • 相关阅读:
    if __name__
    Python为什么要self
    ubuntu系统中的svn三连
    Python中读取到16进制数如何转成有符号数值
    知网
    Ubuntu 登陆后黑屏 问题解决
    Ubuntu 开机Recovery-Mode,命令行中操作提示 Read-Only File System 只读文件系统的 问题解决
    句子:霓裳虽美始于宫娥之糙手
    VMware 虚拟机 不能打开的解决方案汇总
    Ubuntu 系统安装 数据恢复软件 ext4magic 通过RPM方式
  • 原文地址:https://www.cnblogs.com/sansan/p/3491287.html
Copyright © 2011-2022 走看看