最近读<JavaScirpt编程精解>,想把里面的三个大的程序实现,现在记录下来.

问题一:

  从下面这封信中,emily奶奶每封信的结尾都会用同样的格式注明哪只猫出生了,哪只猫死去了.现要求提取cat的信息,要求知道cats的bitrth,date,name.

  格式如下:

  输入:mailArchives 数组

  输出:

{   
Spot:
{ name: 'Spot',
birth: Wed Mar :: GMT+ (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 //: Black Leclère

born // (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;
}

JavaScript 刷题一的更多相关文章

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

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

  2. Javascript刷题 》数组求和

    计算给定数组 arr 中所有元素的总和 输入描述: 数组中的元素均为 Number 类型 输入例子: sum([ 1, 2, 3, 4 ]) 输出例子: 10 解题方法 1.定义一个变量,将前面的和后 ...

  3. 教你用python写:HDU刷题神器

    声明:本文以学习为目的,请不要影响他人正常判题 HDU刷题神器,早已被前辈们做出来了,不过没有见过用python写的.大一的时候见识了学长写这个,当时还是一脸懵逼,只知道这玩意儿好屌-.时隔一年,决定 ...

  4. 湾区求职分享:三个月刷题拿到 Google offer,欢迎踊跃提问

    本文仅以个人经历和个人观点作为参考.如能受益,不胜荣幸. 本文会不断的修正,更新.希望通过大家的互动最后能写出一份阅者受益的文章. 本文纯手打,会有错别字,欢迎指出,虚心接受及时更改. 小马过河,大牛 ...

  5. LeetCode刷题感想

    断断续续用了半年的时间把LeetCode刷完了,之前复习了数据结构与算法.将刷题与复习数据结构结合起来会更有效果.总之不是为了刷题而刷题,而是为了巩固和补充一部分知识. LeetCode真的是一个很好 ...

  6. 用JavaScript刷LeetCode的正确姿势

    虽然很多人都觉得前端算法弱,但其实 JavaScript 也可以刷题啊!最近两个月断断续续刷完了 leetcode 前 200 的 middle + hard ,总结了一些刷题常用的模板代码.走过路过 ...

  7. 用 JavaScript 刷 LeetCode 的正确姿势【进阶】

    之前写了篇文章 用JavaScript刷LeetCode的正确姿势,简单总结一些用 JavaScript 刷力扣的基本调试技巧.最近又刷了点题,总结了些数据结构和算法,希望能对各为 JSer 刷题提供 ...

  8. 基于Typescript和Jest刷题环境搭建与使用

    写在前面 前几个月在公司用vue3和ts写项目,想巩固一下基础,于是我想起了去年基于JavaScript和Jest搭建的刷题环境,不如,给它搞个加强版,结合Typescript和Jest 搞一个刷题环 ...

  9. 通过刷题HTML遇到的问题

    通过刷题HTML遇到的问题 1.有关选择器的权重问题 1.通配符选择器和继承:权重为0, 2.标签选择器:权重为0001 3.类选择器:权重为0010 4.id选择器:权重为0100 5.行内样式:权 ...

随机推荐

  1. react+webpack基础学习配置

    最近学习react,公司的项目是使用create-react-app来搭建的,而我想重新使用node+mysql+react来搭建一个新的博客. 今天尝试从零开始搭建一个webpack+react项目 ...

  2. Java锁---偏向锁、轻量级锁、自旋锁、重量级锁

    之前做过一个测试,反复执行过多次,发现结果是一样的: 1. 单线程下synchronized效率最高(当时感觉它的效率应该是最差才对): 2. AtomicInteger效率最不稳定,不同并发情况下表 ...

  3. Promises讲解

    原生 Promises 是在 ES2015 对 JavaScript 做出最大的改变.它的出现消除了采用 callback 机制的很多潜在问题,并允许我们采用近乎同步的逻辑去写异步代码. 可以说 pr ...

  4. MongoDb安全配置:简单的身份认证

    mongod默认启动不加任何参数时,是没有身份认证的,任何人都可以登录上进行任何操作 启动时添加--auth可以使用身份验证模式 使用mongod -f mongod.conf配置文件启动时,配置文件 ...

  5. List分组迭代器

    说明: 针对长度较大的List对象,可以分组批量进行处理, 如:长度为1000的List对象,可分为10组,每组100条,对数据进行业务逻辑处理... Source /**************** ...

  6. 中文 bootstrapValidator

    官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 转载 ...

  7. 【05】循序渐进学 docker:系统资源和网络

    写在前面的话 在上一篇学习 Dockerfile 的时候其实还有几个相当重要得关键中没有谈到,但没关系,在后面的内容会单独提出来一个一个的学习.这里就先谈谈关于资源的控制个容器的网络~ 资源限制 其实 ...

  8. WPF WebBrowser+TabControl MVVM模式 简单应用 提供源码下载

    源代码下载 这个程序是TabControl和Webbrowser的练手小程序 可达到练手目的有: MVVM设计模式的基本使用 Binding(包括相对源[RelativeSource]绑定)的基本使用 ...

  9. linux命令之进程管理命令

    1.ps:查看进程 该命令用于列出命令执行时刻的进程快照,如果想要动态的显示进程信息,可以使用top命令. 参数 说明 a(常用) 显示与终端相关的所有进程,包含每个进程的完整路径 x(常用) 显示与 ...

  10. ubuntu 12.0.4 下python3.x web环境搭建

    ubuntu 12.0.4 安装python3.x 1. $ sudo add-apt-repository ppa:fkrull/deadsnakes$ sudo apt-get update$ s ...