由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 问几个javascript面试题
相关主题
parsing bibliography and sorting (转载)请问如何在球体表面均匀分布N个点(赠送包子) (转载)
这样写牛逼吗?[合集] 问个土问题 printf, 别Peng
what's wrong with this scripts?variable passing?[合集] perl symbol tables 一问
C# HtmlElement.InvokeMember at Amazon.com问个Perl的简单问题
怎么非ASCII字符就过滤不了呢?[Perl]容器里边放指针怎么办?
how to sed from grep output in c shell? (转载)PERL问题
a linux disk IO question (转载)perl problem
这里的人用BOOST都是用来做什么?foreach statement 使用
相关话题的讨论汇总
话题: birthdate话题: firstname话题: diastolic话题: function话题: var
进入Programming版参与讨论
1 (共1页)
w*s
发帖数: 7227
1
给出以下文件,display the following:
1.Patient names and ages, sorted by age
2.All patients with high blood pressure (systolic over 140 or diastolic over
90)
3.Display all columns and allow the user to pick a column to sort by
我已经送答案给他们了,但我js不熟,想看看正确答案。谢谢!
[
{
"firstName":"Alice",
"lastName":"Green",
"emailAddress":"[email protected]/* */",
"birthDate":"1955-03-06",
"latestBloodPressureDate":"2014-08-23T13:02:05.807Z",
"systolic":118,
"diastolic":84
},
{
"firstName":"Andrew",
"lastName":"Shorter",
"emailAddress":"[email protected]/* */",
"birthDate":"1975-09-22",
"latestBloodPressureDate":"2014-09-20T13:42:58.839Z",
"systolic":143,
"diastolic":89
},
{
"firstName":"Carlos",
"lastName":"Santos",
"emailAddress":"[email protected]/* */",
"birthDate":"1950-01-02",
"latestBloodPressureDate":"2014-09-06T19:44:07.679Z",
"systolic":136,
"diastolic":91
},
{
"firstName":"Daniel",
"lastName":"Poole",
"emailAddress":"[email protected]/* */",
"birthDate":"1979-06-15",
"latestBloodPressureDate":"2014-08-21T18:33:26.668Z",
"systolic":141,
"diastolic":90
},
{
"firstName":"Fiona",
"lastName":"Reeves",
"emailAddress":"[email protected]/* */",
"birthDate":"1944-04-10",
"latestBloodPressureDate":"2014-10-21T20:13:18.887Z",
"systolic":140,
"diastolic":91
},
{
"firstName":"Gertrude",
"lastName":"Stark ",
"emailAddress":"[email protected]/* */",
"birthDate":"1939-12-31",
"latestBloodPressureDate":"2014-09-18T19:29:25.630Z",
"systolic":117,
"diastolic":85
}
]
i****t
发帖数: 61
2
function PatientSorter () {
var stringComparer = function (a, b) {
return a.toLowerCase() < b.toLowerCase() ? -1 : 1;
};
var dateComparer = function (a, b) {
return Date.parse(a) < Date.parse(b) ? -1 : 1;
};
var numberComparer = function (a, b) {
return a - b;
};
var comparers = {
firstName: stringComparer,
lastName: stringComparer,
emailAddress: stringComparer,
birthDate: dateComparer,
latestBloodPressureTest: dateComparer,
systolic: numberComparer,
diastolic: numberComparer
};
return {
sortByColumns: function (patients, columnName) {
return patients.sort(function (a, b) {
var comparer = comparers[columnName];
return comparer(a[columnName], b[columnName]);
});
}
};
};
var ps = PatientSorter();
ps.sortByColumns(patients, 'birthDate');
w*s
发帖数: 7227
3
var fs=require('fs');
var origData = JSON.parse(fs.readFileSync('0422.json', 'utf8'));
/////////////////////////////////////////////
var sort_by = function(field, primer) {
var key = primer ?
function(x) { return primer(x[field])} :
function(x) { return x[field]};
return function(a, b) {
return a = key(a), b=key(b), ((a>b) - (b>a));
}
}
/////////////////////////////////////////////
function certainValue(element)
{
return (element.systolic > 140 || element.diatolic > 90);
}
/////////////////////////////////////////////
console.log("n----------Q1:sort by age ------------");
arrayQ1 = origData.sort(sort_by('birthDate', function(a) {
return new Date(a).getTime();
}
));
for (i in arrayQ1) {
console.log(arrayQ1[i].firstName + " " + arrayQ1[i].birthDate);
}
console.log("n-------Q2: filter by value----------");
var arrayQ2 = origData.filter(certainValue);
console.log(arrayQ2);
console.log("n----------Q3: sort by name ------------");
console.log(origData.sort(sort_by('firstName', function(a) {
return a.toUpperCase();
}
)));
console.log("n----------Q3: sort by diastolic ------------");
console.log(origData.sort(sort_by('diastolic', parseInt)));
我从网上拼凑了些答案,也没完全理解,他们有时间限制。请大家点评一下。

【在 i****t 的大作中提到】
: function PatientSorter () {
: var stringComparer = function (a, b) {
: return a.toLowerCase() < b.toLowerCase() ? -1 : 1;
: };
: var dateComparer = function (a, b) {
: return Date.parse(a) < Date.parse(b) ? -1 : 1;
: };
: var numberComparer = function (a, b) {
: return a - b;
: };

d**********6
发帖数: 4434
4
允许用lodash吗,基本上否是一到三句话而已

【在 w*s 的大作中提到】
: var fs=require('fs');
: var origData = JSON.parse(fs.readFileSync('0422.json', 'utf8'));
: /////////////////////////////////////////////
: var sort_by = function(field, primer) {
: var key = primer ?
: function(x) { return primer(x[field])} :
: function(x) { return x[field]};
: return function(a, b) {
: return a = key(a), b=key(b), ((a>b) - (b>a));
: }

w*s
发帖数: 7227
5
应该可以吧,求指点,我做angular, node,对js本身不行。
对了我的答案里primer是什么意思?

【在 d**********6 的大作中提到】
: 允许用lodash吗,基本上否是一到三句话而已
k********4
发帖数: 858
6

primer是个函数类型的参数,看样子是用来处理需要排序的元素获取排序用的值。做
angular, node连这个都看不懂?别怪我mean

【在 w*s 的大作中提到】
: 应该可以吧,求指点,我做angular, node,对js本身不行。
: 对了我的答案里primer是什么意思?

s*i
发帖数: 5025
7
1.Patient names and ages, sorted by age
map & sort
2.All patients with high blood pressure (systolic over 140 or diastolic over
90)
filter
3.Display all columns and allow the user to pick a column to sort by
sort
example solutions (data is the input array):
1.
var mapped = data.map(i => { return {
firstName: i.firstName,
lastName: i.lastName,
age: Math.floor((new Date()-Date.parse(i.birthDate))/(365.25 * 24 * 3600 *
1000))}});
var finallySorted = mapped.sort((a, b) => a.age - b.age);
2.
var finallyFiltered = data.filter(i => i.systolic >140 || i.diastolic > 90)
3.
var sortby = function (data, field){
return data.sort(
function(a, b) {
if(a[field] < b[field])
return -1;
else if(a[field] == b[field])
return 0;
return 1
}
)
}

over

【在 w*s 的大作中提到】
: 给出以下文件,display the following:
: 1.Patient names and ages, sorted by age
: 2.All patients with high blood pressure (systolic over 140 or diastolic over
: 90)
: 3.Display all columns and allow the user to pick a column to sort by
: 我已经送答案给他们了,但我js不熟,想看看正确答案。谢谢!
: [
: {
: "firstName":"Alice",
: "lastName":"Green",

m***y
发帖数: 227
8
很好奇, js本身不行, 能做 angular, node?

【在 w*s 的大作中提到】
: 应该可以吧,求指点,我做angular, node,对js本身不行。
: 对了我的答案里primer是什么意思?

n*****t
发帖数: 22014
9
json 可以直接 require,sortBy 写得太啰嗦了 。。。
这题目其实就是 Array.sort/filter。
iPad 刷买提,code 暂时不写了

【在 w*s 的大作中提到】
: var fs=require('fs');
: var origData = JSON.parse(fs.readFileSync('0422.json', 'utf8'));
: /////////////////////////////////////////////
: var sort_by = function(field, primer) {
: var key = primer ?
: function(x) { return primer(x[field])} :
: function(x) { return x[field]};
: return function(a, b) {
: return a = key(a), b=key(b), ((a>b) - (b>a));
: }

w********m
发帖数: 1137
10
这题用直接写不容易。
ES6很简单,可以用父子两个class。
相关主题
how to sed from grep output in c shell? (转载)请问如何在球体表面均匀分布N个点(赠送包子) (转载)
a linux disk IO question (转载)[合集] 问个土问题 printf, 别Peng
这里的人用BOOST都是用来做什么?[合集] perl symbol tables 一问
进入Programming版参与讨论
w*s
发帖数: 7227
11
thanks a lot !

【在 n*****t 的大作中提到】
: json 可以直接 require,sortBy 写得太啰嗦了 。。。
: 这题目其实就是 Array.sort/filter。
: iPad 刷买提,code 暂时不写了

w*s
发帖数: 7227
12
i can read that line, but want to know more info, u SB.

【在 k********4 的大作中提到】
:
: primer是个函数类型的参数,看样子是用来处理需要排序的元素获取排序用的值。做
: angular, node连这个都看不懂?别怪我mean

w*s
发帖数: 7227
13
u SB,
after routes and promise and factory etc, complicated back end task can be
done more efficiently using python

【在 m***y 的大作中提到】
: 很好奇, js本身不行, 能做 angular, node?
n*****t
发帖数: 22014
14
var data = require('./data.json');
var sortBy = function(arr, column) {
return arr.sort(function(a, b) {
return a[column] < b[column] ? -1 : 1;
});
}
var print = function(x) {
console.log('Name : ' + x.firstName + ' ' + x.lastName + ', Age: ' +
getAge(x.birthDate));
}
function q1() {
var today = new Date(), y = today.getYear() + 1900, m = today.getMonth()
+ 1, d = today.getDate();
var getAge = function(birth) {
var tmp = birth.split('-').map(Number), age = y - tmp[0];
if (tmp[1] != m)
age -= tmp[1] < m ? 0 : 1;
else if (tmp[2] != d)
age -= tmp[2] < d ? 0 : 1;
return age;
}
sortBy(data, 'birthDate').forEach(print);
}
function q2() {
data.filter(function(x) {
return x.systolic > 140 || x.diastolic > 90;
}).forEach(print);
}
function q3(column) {
sortBy(data, column).forEach(print);
}
q1();
q2();
q3('firstName');
w*s
发帖数: 7227
15
好像不比我的好多少,:)
先顶再看

【在 n*****t 的大作中提到】
: var data = require('./data.json');
: var sortBy = function(arr, column) {
: return arr.sort(function(a, b) {
: return a[column] < b[column] ? -1 : 1;
: });
: }
: var print = function(x) {
: console.log('Name : ' + x.firstName + ' ' + x.lastName + ', Age: ' +
: getAge(x.birthDate));
: }

n*****t
发帖数: 22014
16
计算 age 比较麻烦

【在 w*s 的大作中提到】
: 好像不比我的好多少,:)
: 先顶再看

w*s
发帖数: 7227
17
牛!

over

【在 s*i 的大作中提到】
: 1.Patient names and ages, sorted by age
: map & sort
: 2.All patients with high blood pressure (systolic over 140 or diastolic over
: 90)
: filter
: 3.Display all columns and allow the user to pick a column to sort by
: sort
: example solutions (data is the input array):
: 1.
: var mapped = data.map(i => { return {

c*********e
发帖数: 16335
18
尽量把business logic放在server side,别让hacker看到你的business logic.

over

【在 w*s 的大作中提到】
: 给出以下文件,display the following:
: 1.Patient names and ages, sorted by age
: 2.All patients with high blood pressure (systolic over 140 or diastolic over
: 90)
: 3.Display all columns and allow the user to pick a column to sort by
: 我已经送答案给他们了,但我js不熟,想看看正确答案。谢谢!
: [
: {
: "firstName":"Alice",
: "lastName":"Green",

w*s
发帖数: 7227
19
100%同意,其实我本来是做c的,更喜欢server side

【在 c*********e 的大作中提到】
: 尽量把business logic放在server side,别让hacker看到你的business logic.
:
: over

n*****t
发帖数: 22014
20
这样计算 age 有点偷懒吧

over

【在 s*i 的大作中提到】
: 1.Patient names and ages, sorted by age
: map & sort
: 2.All patients with high blood pressure (systolic over 140 or diastolic over
: 90)
: filter
: 3.Display all columns and allow the user to pick a column to sort by
: sort
: example solutions (data is the input array):
: 1.
: var mapped = data.map(i => { return {

s*i
发帖数: 5025
21
擦边球偷懒。直接写 除以 31557600000 更偷懒,怕看不懂

【在 n*****t 的大作中提到】
: 这样计算 age 有点偷懒吧
:
: over

1 (共1页)
进入Programming版参与讨论
相关主题
foreach statement 使用怎么非ASCII字符就过滤不了呢?[Perl]
Shuffle performance (C#)how to sed from grep output in c shell? (转载)
有个SB interviewer和我说++i比i++好 (转载)a linux disk IO question (转载)
VB is a much much better programming language than C#, If not the best in the world!这里的人用BOOST都是用来做什么?
parsing bibliography and sorting (转载)请问如何在球体表面均匀分布N个点(赠送包子) (转载)
这样写牛逼吗?[合集] 问个土问题 printf, 别Peng
what's wrong with this scripts?variable passing?[合集] perl symbol tables 一问
C# HtmlElement.InvokeMember at Amazon.com问个Perl的简单问题
相关话题的讨论汇总
话题: birthdate话题: firstname话题: diastolic话题: function话题: var