forked from laihuamin/JS-total
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathceshi.js
166 lines (138 loc) · 3.83 KB
/
ceshi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// function thisBind() {
// console.log(this.name);
// }
// const obj = {
// name: 'baobei',
// fn: thisBind
// }
// obj.fn();
// const bar = obj.fn;
// var name = 'beibei';
// bar();
// setTimeout(obj.fn, 1000);
// function People(name, age) {
// this.name = name;
// this.age = age;
// }
// People.prototype.say = function() {
// console.log('我是' + this.name + ',我今年' + this.age + '岁');
// }
// function Man(name, age, sex) {
// People.call(this, name, age);
// this.sex = sex;
// }
// Man.prototype = new People();
// Man.prototype.constructor = Man;
// Man.prototype.say() = function() {
// console.log('我是' + this.name + ',我今年' + this.age + '岁,性别' + this.sex)
// }
// Function.prototype.extends = function(props){
// var self = this;
// var Parent = function(){};
// Parent.prototype = self.prototype;
// var prev = new Parent();
// var _super = function(){
// return self.apply(this, arguments);
// }
// function Child(){
// if(props.constructor){
// props.constructor.apply(this, arguments);
// }
// for(var i in self.prototype){
// _super[i] = self.prototype[i].bind(this);
// }
// }
// Child.prototype = prev;
// Child.prototype._super = _super;
// for(var i in props){
// if(i !== 'constructor'){
// Child.prototype[i] = props[i];
// }
// }
// return Child;
// }
// function People(name, age){
// this.name = name;
// this.age = age;
// }
// People.prototype.say = function(){
// console.log('我是'+this.name+',我今年'+this.age+'岁');
// }
// var Man = People.extends({
// constructor: function(name, age){
// this._super(name, age);
// this.sex = 'male';
// },
// say: function(){
// console.log('我是'+this.name+',我今年'+this.age+'岁,性别'+this.sex);
// }
// });
// function getCbName (prefix, num) {
// return prefix + Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, num)
// }
// function createScript (src) {
// const script = document.createElement('script')
// script.setAttribute('type', 'text/javascript')
// script.setAttribute('src', src)
// script.async = true
// return script
// }
// function jsonp (url) {
// return new Promise((resolve, reject) => {
// const _cb = getCbName('callback')
// window[_cb] = function (data) {
// resolve(data)
// }
// url += url.indexOf('?') > -1 ? '&' : '?'
// const script = createScript(`${url}callback=${_cb}`)
// script.onload = function () {
// script.onload = null
// if (script.parentNode) {
// script.parentNode.removeChild(script)
// }
// window[_cb] = null
// }
// script.onerror = function () {
// reject()
// }
// document.getElementsByTagName('head')[0].appendChild(script)
// })
// }
// //生成一个script标签
// function createScript(_url, _id) {
// const script = document.createElement('script');
// script.setAttribute('src', _url);
// script.id = _id;
// document.getElementsByTagName('head')[0].appendChild(script);
// }
// function createScript(_url, _id) {
// const script = document.createElement('script');
// script.setAttribute('src', _url);
// script.id = _id;
// document.getElementsByTagName('head')[0].appendChild(script);
// }
// function thisBind(){
// console.log(this.name);
// }
// obj1 = {
// name: 'laihuamin',
// fn: thisBind
// }
// obj2 = {
// name: 'huaminlai',
// fn: thisBind
// }
var hammingDistance = function(x, y) {
let num = x ^ y,
str = num.toString(2),
arr = str.split(''),
i = 0;
console.log(arr);
arr.forEach((index) => {
if(index === '1') {
i++;
}
});
return i;
};
console.log(hammingDistance(2,4));