最近读<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 Emilydied 27/04/2006: Black Leclèreborn 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;
}

转载于:https://www.cnblogs.com/sansan/p/3491287.html

JavaScript 刷题一相关推荐

  1. [JavaScript 刷题] 搜索 - 腐烂的橘子, leetcode 994

    [JavaScript 刷题] 搜索 - 腐烂的橘子, leetcode 994 唉--之前写过笔记总结的问题,还是又卡住了. 自挂东南枝-- 题目地址: Rotting Oranges 题目如下: ...

  2. [JavaScript 刷题] 树 - 完全二叉树的节点个数, leetcode 222

    [JavaScript 刷题] 树 - 完全二叉树的节点个数, leetcode 222 github repo 地址: https://github.com/GoldenaArcher/js_lee ...

  3. [JavaScript 刷题] Code Signal - 相似数组(Are Similar?)

    [JavaScript 刷题] Code Signal - 相似数组(Are Similar?) 题目地址:Are Similar? 题目 如下: Two arrays are called simi ...

  4. Javascript刷题 》 查找数组元素位置

    找出元素 item 在给定数组 arr 中的位置 输出描述: function indexOf(arr, item) {..... } 如果数组中存在 item,则返回元素在数组中的位置,否则返回 - ...

  5. 刷题汇总(一)leetcode 精选50题 JavaScript答案总结

    题目来源 腾讯精选练习(50 题) 信息更新时间:2019-3-11,因为要准备面前端,就用js做了遍来熟悉JavaScript这门语言,50道题均已提交通过. GitHub地址:https://gi ...

  6. JavaScript——易班优课YOOC课群在线测试自动答题解决方案(十四)自动刷题

    前文 JavaScript--易班优课YOOC课群在线测试自动答题解决方案(一)答案获取 Spring Boot--易班优课YOOC课群在线测试自动答题解决方案(二)答案储存 Spring Boot- ...

  7. LeetCode部分刷题笔记!!!JavaScript!!!

    详细解说请看视频JS老毕:人人都能看得懂的Leetcode力扣刷题教程合集 边看视频边记录笔记!!!部分题目在视频中无! 文章目录 LeetCode第1题:1. 两数之和 LeetCode第2题:2. ...

  8. JavaScript|免费导入题库,考试复习工具,刷题神器,支持导入excel题库【完全免费+提供源码】

    工具界面展示 点击这里下载源代码↓↓↓ 基于JS的导入EXCEL题库复习小工具_intitle:javascript搜题-Javascript文档类资源-CSDN下载 目录 一.工具使用说明 二.界面 ...

  9. 【牛客刷题日记】— Javascript 通关秘籍(2)

    CSDN话题挑战赛第2期 参赛话题:面试宝典 众人拾柴火焰高,我们一起携手共筑面试宝典,大家可以将各语言各技术各类面试真题或者你刷过的面试题汇聚于此,金九银十之际,让我们助应聘同学一臂之力~ 提升宝题 ...

最新文章

  1. CSS之布局(默认样式)
  2. 巨大的需求之下 人工智能如何更快落地?
  3. 2020-12-01 Halcon初学者知识[1] 初始界面
  4. C++Merege Sort合并排序的实现算法(附完整源码)
  5. CF1547F Array Stabilization (GCD version) st表 + 尺取/二分
  6. 【Clickhouse】ClickHouse REST API(HTTP接口)及Engine引擎的使用
  7. WSGI Middleware
  8. 删除PHP配置文件中的注释行
  9. 王乐园 php_见到海贼王主题乐园的雕像,感觉认知回不去了,索隆是认真的?...
  10. Java解决高并发下商品库存更新
  11. 计算机优化英语课堂教学,多媒体课件优化英语课堂的反思性研究
  12. 遗传算法在机器人路径规划中的应用研究(Matlab代码实现)
  13. 从懵懵懂懂到如今的恍恍惚惚
  14. jQuery图片播放插件ColorBox使用方法
  15. 身份证实名认证API接口,选择的时候应该注意什么?
  16. 电脑ie怎么设置html5,电脑中IE浏览器工具栏设置选项显示的是英文如何改成中文...
  17. 斐波那契数列之不死神兔 14
  18. Spring AOP 切面@Around注解的具体使用
  19. android手机调用linux ping命令
  20. Java编写数据统计程序_个人项目--wc文本统计程序(Java实现)

热门文章

  1. 设计一个60T数据仓库及大数据分析平台,医院数字化该怎么做?
  2. 手动创建一个标准web工程
  3. 最低服务器协议,的WebSocket服务器最低要求
  4. jquery实现app开发闹钟功能_一款让你真正摆脱懒觉的“闹钟APP软件”
  5. centos7 安装mysql8_CentOS7中安装MySQL8.0.21爬坑记录
  6. pytorch模型保存
  7. 偏差(bias)、方差(variance)和噪音(noise)
  8. GPU服务器的配置计划
  9. Java获取资源的路径
  10. 机器学习-多项式回归、正规方程(标准方程)