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;
    }
  • 相关阅读:
    限时购校验小工具&dubbo异步调用实现限
    Android 应用防止被二次打包指南
    什么是高防服务器?
    真屏实据丨数据大屏设计实战—揭秘企业级数据大屏设计过程
    TerminateThread函数学习
    HTML DOM访问
    gdb常用命令
    整数开方算法
    android环境下解决java.io.IOException: Malformed ipv6异常的方法
    HTML DOM 创建与修改
  • 原文地址:https://www.cnblogs.com/sansan/p/3491287.html
Copyright © 2011-2022 走看看