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;
    }
  • 相关阅读:
    无语的index hint:手工分配哈希区,5小时不出结果,优化后20分钟
    EL 表达式中自己定义函数
    关于SetCapture() 和 ReleaseCapture()的使用方法
    MySql的like语句中的通配符:百分号、下划线和escape
    字符串匹配之horspool算法(简化的BM算法)
    strcmp函数和strcpy函数
    ehci符合USB2.0,uhci,ohci,
    软件发布版本区别介绍-Alpha,Beta,RC,Release
    Wi-Fi定位,AP定位
    CentOS 启动提示unexpected inconsistency;RUN fsck MANUALLY, ntfs的input/output Error,InPageError c000009c使用chkdsk修复磁盘,12款Linux系统恢复工具
  • 原文地址:https://www.cnblogs.com/sansan/p/3491287.html
Copyright © 2011-2022 走看看