1--取出数组1与数组2的共同部分元素
思想1:将数组1放入map中,然后对数组2进行遍历,数组2中的元素在map中不存在时不操作,如果数组2中的元素在map中已经存在,将其push进一个新数组newArr,最后返回该新数组。
function getSameNums(arr1,arr2){
var mp={};
var newArr=[];
for(var i=0;i<arr1.length;i++){
if(!mp[arr1[i]]){
mp[arr1[i]]=1;
}
}
for(var i=0;i<arr2.length;i++){
if(mp[arr2[i]]){
newArr.push(arr2[i]);
}
}
return newArr;
}
var arr1 = [4,2,1,10,5];
var arr2 = [2,3,4,0,10,5];
console.log(getSameNums(arr1,arr2));
思想2:将数组1、数组2进行升序排序,然后分别从头开始遍历,比较。如果相等,将该元素push进一个新数组;如果数组1中的该元素大于数组2中的该元素,将该数组1中的该元素与数组2中的下一个元素进行比较;如果数组1中的该元素小于数组2中的该元素,将该数组1中的下一个元素与数组2中的该元素进行比较
var arr = []; arr1.sort(sortby); arr2.sort(sortby); var i=0;
var j=0; while(i<arr1.length && j < arr2.length){ if(arr1[i] == arr2[j]){ arr.push(arr1[i]); i++;
j++; }else if(arr1[i]>arr2[j]){ j++; }else{ i++; } } return arr; } var arr1 = [4,2,1,10,5]; var arr2 = [2,3,4,0,10,5]; console.log(getSameNums(arr1,arr2));
思想3:将数组1进行升序排序,定义一个全局新数组,然后遍历数组2,利用二分查找判断数组2中的元素是否在数组1中,不在不操作,在的话将其push进一个新数组,最后返回。调用方法,再打印出该新数组。
function sortby(a,b){ return a - b ; } var newArr = [];
function binarySearch(arr,findVal,leftIndex,rightIndex){ if(leftIndex>rightIndex){ return ; } var midIndex=Math.floor((leftIndex+rightIndex)/2); var midVal=arr[midIndex]; if(midVal>findVal){ binarySearch(arr,findVal,leftIndex,midIndex-1); }else if(midVal<findVal){ binarySearch(arr,findVal,midIndex+1,rightIndex); }else{ newArr.push(findVal) return ; } } function getSameNums(arr1,arr2){ arr1.sort(sortby); for(var i=0;i<arr2.length;i++){ binarySearch(arr1,arr2[i],0,arr1.length-1); } } var arr1 = [4,2,1,10,5]; var arr2 = [2,3,4,0,10,5]; getSameNums(arr1,arr2); console.log(newArr);
2017-7-22小记
(1)
var a=false;
if(a){
console.log("a is true")
}else{
console.log("a is false") //a is false
}
var a=new Boolean(false);
if(a){
console.log("a is true") //a is true
}else{
console.log("a is false")
}
(2)
"use strict"
str="haha"
console.log(window.str); //报错 undefined
var str="ee"
console.log(str) //ee
(3)
var x=0;
var y=1;
var z=2;
console.log(x||y); //1
console.log(y&&z); //2
console.log(y&z); //0
console.log(x|y); //1
(4)
function num(a){
var n=10;
return function(b){
n+=10;
return (a+b+n);
}
}
var n=num(10);
console.log(n(10)); //40
console.log(n(10)); //50
2017-7-26 小记
(1)实现spacify('hello world') // => 'h e l l o w o r l d'
String.prototype.spacify = function(){
return this.split('').join(' ');
}
console.log('hello world'.spacify());//'h e l l o w o r l d'
(2)实现 log函数log('hello world');
function log(){
var args = Array.prototype.slice.call(arguments);
args.unshift('(app)');
console.log.apply(console, args);
};
log('hello world'); //(app) hello world
ps:arguments为伪数组,需要利用Array.prototype.slice先转换为真正数组,然后利用unshift方法在其头部插入元素。
(3)
var User = { count: 1, getCount: function() { return this.count; } }; console.log(User.getCount()); //1 var func = User.getCount; console.log(func());//undefined
//ps:func的上下文是`window,因此已经失去了count属性
var User = {
count: 1,
getCount: function() { return this.count;
}
};
var func = User.getCount.bind(User);
console.log(func());//1
(4)
function getMax(str){ //字符串中出现次数最多的字符及次数
var mp={};
var max=0;
var tmpkey=0;
for(var key in str){
if(!mp[str[key]]){
mp[str[key]]=1;
}else{
mp[str[key]]++;
}
}
// return mp;
for(var key in mp){
if( mp[key]>max){
max= mp[key];
tmpkey=key
}
}
//console.log(mp[tmpkey])
var obj ={}
obj[tmpkey] = mp[tmpkey]
return obj
}
var str="aaaaaabbgd";
console.log(getMax(str));
2017-7-30 小记
(1)
<script> var str="孙若水"; // alert(str); // var beginStr=str.slice(1); // var beginStr=str.substr(1,2); var beginStr=str.substring(1,3) var psd=""; for(var i= 0;i<1;i++){ psd+="*"; } var strNew=psd+beginStr; alert(strNew); // alert(beginStr); </script>
(2)
var str = "16826275903" var begStr = str.slice(0,3) console.log(begStr) var endStr = str.slice(-3) console.log(endStr) var passwdStr = "" for(var i =0; i< str.length - 6; i++ ) passwdStr+="*"; var tmpStr = begStr + passwdStr + endStr console.log(tmpStr)
2017-7-31小记
/** * Created by DELL on 2017/7/31. */ console.log(typeof undefined === 'undefined'); //true console.log(typeof undefined === undefined); //false console.log(typeof undefined == undefined); //false console.log(typeof true ==='boolean');//true console.log(typeof true ===Boolean);//false console.log(typeof true ==Boolean);//false console.log(typeof 42 === 'number');//true console.log(typeof 42 === Number);//false console.log(typeof 42 == Number);//false console.log(typeof '42' === 'string');//true console.log(typeof '42' === String);//false console.log(typeof '42' == String);//false console.log(typeof {life:42} === 'object');//true console.log(typeof {life:42} === Object);//false console.log(typeof {life:42} == Object);//false console.log(typeof Symbol() === 'symbol');//true console.log(typeof Symbol() === Symbol);//false console.log(typeof Symbol() == Symbol);//false console.log(typeof null === 'object');//true console.log(typeof null === 'null');//false //console.log(typeof null === Null);//Null is not defined
//ps:typeof运算符可查看值的类型,返回的是类型的字符串值,除了null //检测null值的类型 var a = null; console.log((!a && typeof a === 'object'));//true
ps:JavaScript中的变量是没有类型的,值才有。变量可以随时持有任何类型的值。在对变量执行typeof操作时,返回的并不是该变量的类型,而是该变量持有的值的类型。