JavaScript - interview questions and answers
Blogs20142014-11-25
JavaScript - interview questions and answers
(function () {
'use strict';
var str = 'how many times how many they are total',
ary = [],
obj = {};
ary = str.split(' ');
ary.forEach(function (index) {
console.log(ary[index]);
if (!obj[index])obj[index]=0 ;
obj[index] += 1;
});
alert(JSON.stringify(obj));
}());
//2.
function compare(a, b) {
if(a.id<b.id) return -1;
if(a.id>b.id) return 1;
return 0;
}
var ary = [ {id:1, name:'n1'}, {id:3, name:'n3'}, {id:4, name:'n4'}, {id:2, name:'n2'} ];
console.log(JSON.stringify(ary.sort(compare)));
//3.
function isTwoAParam() {
for(var i=0, len=arguments.length; i < len; i=i+1) {
if(arguments[i] === 2) return true;
}
return false;
}