JavaScript 刷题一
最近读<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 刷题一的更多相关文章
- Javascript刷题 》 查找数组元素位置
找出元素 item 在给定数组 arr 中的位置 输出描述: function indexOf(arr, item) { ..... } 如果数组中存在 item,则返回元素在数组中的位置,否则返回 ...
- Javascript刷题 》数组求和
计算给定数组 arr 中所有元素的总和 输入描述: 数组中的元素均为 Number 类型 输入例子: sum([ 1, 2, 3, 4 ]) 输出例子: 10 解题方法 1.定义一个变量,将前面的和后 ...
- 教你用python写:HDU刷题神器
声明:本文以学习为目的,请不要影响他人正常判题 HDU刷题神器,早已被前辈们做出来了,不过没有见过用python写的.大一的时候见识了学长写这个,当时还是一脸懵逼,只知道这玩意儿好屌-.时隔一年,决定 ...
- 湾区求职分享:三个月刷题拿到 Google offer,欢迎踊跃提问
本文仅以个人经历和个人观点作为参考.如能受益,不胜荣幸. 本文会不断的修正,更新.希望通过大家的互动最后能写出一份阅者受益的文章. 本文纯手打,会有错别字,欢迎指出,虚心接受及时更改. 小马过河,大牛 ...
- LeetCode刷题感想
断断续续用了半年的时间把LeetCode刷完了,之前复习了数据结构与算法.将刷题与复习数据结构结合起来会更有效果.总之不是为了刷题而刷题,而是为了巩固和补充一部分知识. LeetCode真的是一个很好 ...
- 用JavaScript刷LeetCode的正确姿势
虽然很多人都觉得前端算法弱,但其实 JavaScript 也可以刷题啊!最近两个月断断续续刷完了 leetcode 前 200 的 middle + hard ,总结了一些刷题常用的模板代码.走过路过 ...
- 用 JavaScript 刷 LeetCode 的正确姿势【进阶】
之前写了篇文章 用JavaScript刷LeetCode的正确姿势,简单总结一些用 JavaScript 刷力扣的基本调试技巧.最近又刷了点题,总结了些数据结构和算法,希望能对各为 JSer 刷题提供 ...
- 基于Typescript和Jest刷题环境搭建与使用
写在前面 前几个月在公司用vue3和ts写项目,想巩固一下基础,于是我想起了去年基于JavaScript和Jest搭建的刷题环境,不如,给它搞个加强版,结合Typescript和Jest 搞一个刷题环 ...
- 通过刷题HTML遇到的问题
通过刷题HTML遇到的问题 1.有关选择器的权重问题 1.通配符选择器和继承:权重为0, 2.标签选择器:权重为0001 3.类选择器:权重为0010 4.id选择器:权重为0100 5.行内样式:权 ...
随机推荐
- C++11之Lambda特性探析
目录 目录 1 1. 什么是Lambda? 1 2. 语法格式 1 2.1. 语法格式 1 2.2. 最简定义 2 3. 应用示例 2 4. capture列表 3 4.1. 基本形式 3 4.2. ...
- C++ springf()的用法(转)
转:http://blog.csdn.net/masikkk/article/details/5634886 在将各种类型的数据构造成字符串时,sprintf 的强大功能很少会让你失望.由于sprin ...
- bootstrap css排版
smart-form 单行元素: 一般用div包含,class="row" 列元素:用section包含,class="col col-x"(section带有 ...
- [Erlang15]“hello world”与<<”hello world”>>的具体区别是什么?
参见 :http://learnyousomeerlang.com/buckets-of-sockets 为了加深理解,自译如下,若理解有误或更好的建议,请帮忙指出, :) Buckets of So ...
- Delegate Func Action Predicate default() 知识点
看仓储模式,有代码写到这几个关键字,陌生,记录下来. 定义一个类型,此类型抽象化了相似结构的某一类方法,因此我们能将此类型代表的方法作为参数进行传递. Delegate至少0个参 ...
- Spring各个jar包详解
Spring各jar包详解 spring.jar 是包含有完整发布模块的单个jar 包.但是不包括mock.jar,aspects.jar, spring-portlet.jar, and sprin ...
- 南昌网络赛 I. Max answer 单调栈
Max answer 题目链接 https://nanti.jisuanke.com/t/38228 Describe Alice has a magic array. She suggests th ...
- 如何自动在Eclipse里对指定类或接口生成要覆盖的方法?
我们经常遇到对对指定类或接口生成要覆盖的方法,但是我们不了解这些类和接口,完全手写不能保证完全正确,那么是否可以准确知道指定类或接口要被覆盖的方法,自动生成这些代码呢?是的,完全可以,按照下面步骤操作 ...
- Django-05模型层之单表操作1
7.1 ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开 ...
- (1)python的基础认知
Python程序员的信仰:人生苦短,我用python! (一)python的发展史 1989年开发的语言,创始人范罗苏姆(Guido van Rossum),别称:龟叔(Guido).为了打发圣诞节假 ...