1. Write out the output from the following code snippet:num is not equal to Nan,nan and Nan, the type of Nan is number type, so the last alert ' number '.
var str = ' abc123 ',
num = parseint (str);
if (num = = NaN) {
Alert (' NaN ');
}
else if (num = = 123) {
Alert (' 123 ');
}
else if (typeof (num) = = ' number ') {
Alert (' number ');
}
else {
Alert (' str ');
}
2. Write down the values and data types of the following expressions:c= ' 2 ', type String.
var a = 1,
b = ' 2 ';
var c = a > B? (A < B. a:b): (A = = B? a:b);
3. Write out the results of the alert output for the following two loop statements, and explain why. The first pop-up two 0, because break is the end of the entire loop, the second loop, the continue will end the cycle, the experience to pop up 0 to 9, and finally pop up a ten.
for (var i = 0; i < i++) {
alert (i);
Break
}
alert (i);
for (var i = 0; i < i++) {
alert (i);
Continue
}
alert (i);
4. Write out the output of the fun two call alert respectively. The first pop-up 0123, it is the string concatenation, the second popup 10, is the result of the sum of the numbers.
function Fun (A, B, c) {
var L = arguments.length;
var num = 0;
for (var i = 0; i < L; i++) {
num + = Arguments[i];
Console.log (typeof (Arguments[i));
}
alert (num);
}
Fun (' 1 ', 2, 3);
Fun (1, 2, 3, 4);
5. Write the output of alert after executing the function fun and explain the principle.
This interview question appeared in the interview the number of busty, the first pop-up undefined, the second pop-up 0. The first execution of Fun,alert (a) in the scope of the fun function to find there is no local variable A, some words it will not find the outside a, but the fun of a is also after the alert (a) is assigned, so it will return to undefined, the second pop 0, is looking for the result of an external a.
var a = 0;
function Fun () {
alert (a);
var a = 10;
}
Fun ();
alert (a);
6. Write the alert output from the following code and explain the principle. The popup 10,o is defined as an object, B=o, B, and O are references to the same object, and subsequent assignments overwrite the previous one.
var o = {};
O.A = 0;
var b = o;
B.A = 10;
alert (O.A);
7. Write out the results of the alert output for my_fun.b () and MY_FUN.C () respectively. undefined,20,30
Fun.prototype = {
B:function () {
THIS.A = 20;
alert (THIS.A);
},
C:function () {
THIS.A = 30;
alert (THIS.A);
}
}
var my_fun = new Fun ();
MY_FUN.B ();
MY_FUN.C ();
8. Do the following in the code, there will be several alert output, each time the value of what? and explain the principle.
Eject 11,12,0 respectively. The first var c=a (); Execute B () inside a, pop 11 first, then return B to go out, C (); That is, execute B (); popup 12. The last alert (n) is the most externally found 0, which pops up 0.
var n = 0;
function A () {
var n = 10;
Function B () {
n++;
alert (n);
}
b ();
return b;
}
var C = A ();
C ();
alert (n);
9. Design a simple log tool:
(1) <ul> element already exists, no need to create;
(2) When the function log (msg) is called, insert a record into <ul> <li><p>msg</p><button> delete </button></li> ;
(3) When you click the button, delete the record.
10. Implement function Uniq (arr), the Uniq function removes all duplicate elements in arr, requiring no new arrays to be created.
My practice is to build a JSON object, if the JSON object has the I attribute, then skip, otherwise push to arr, and finally take only the value of the new push after Arr, that is, there is no duplicate value array.
function Unique (arr) {
var json = {};
for (var i = 0; i < arr.length; i++) {
if (!json[arr[i]]) {
Json[arr[i]] = true;
Arr.push[arr[i]];
}
}
Arr.splice (arr.length-1);
return arr;
}
arr = [3,5,2,3,2,2,3,7];
Console.log (Arr.unique3 ());
11. Add method indexof (value) to the array so that the following calls can run successfully.
var arr = [1,2,3,4,5];
var index = Arr.indexof (3);
if (! ARRAY.PROTOTYPE.INDEXOF) {
Array.prototype.indexOf = function (ELT) {
var len = this.length > 0;
var from = number (Argument[1]) | | 0;
From = (from < 0)? Math.ceil (from): Math.floor (from);
if (from < 0) from + = Len;
for (; from < Len; from++) {
If (from the This && this[from] = = = = ELT) return from;
}
return-1;
};
}
Finally, several methods for the de-weight of JS arrays are introduced:
One is to create a temporary array of the first value of the current array in the array prototype method , traverse the current array, if the current array of item I is in a temporary array , then skip, if not, push to the temporary array and return the temporary array.
Array.prototype.unique1 = function () {
var res = [This[0]];
for (var i = 1; i < this.length; i++) {
if (Res.indexof (this[i]) = =-1) {
Res.push (This[i]);
}
}
return res;
}
arr = [3,5,2,3,2,2,3,7];
Console.log (Arr.unique1 ());
The second method is to create a new hash table and a temporary array, traverse the current array, see if there is a JSON object I , there, skip, none, then let the hash table in the value of the I key attribute is true, and push the value into the temporary array, and finally return the temporary array.
Array.prototype.unique2 = function () {
var json = {};
var temp = [];
for (var i = 0; i < this.length; i++) {
if (!json[this[i]]) {
Json[this[i]] = true;
Temp.push (This[i]);
}
}
return temp;
}
The third method is mainly to traverse the current array, see if the position of the item I in the array is not I, if not, then repeat, the item I omitted, otherwise deposited into the array.
Array.prototype.unique3 = function () {
var temp = [];
for (var i = 0; i < this.length; i++) {
if (This.indexof (this[i]) = = i) {
Temp.push (This[i]);
}
}
return temp;
}
The fourth method is to sort the array first, then compare the adjacent two values, and if the last item in the term I and the temporary array is not equal, push to the temporary array, and finally return the temporary array.
Array.prototype.unique4 = function () {
This.sort ();
var temp = [This[0]];
for (var i = 1; i < this.length; i++) {
if (This[i]!== temp[temp.length-1]) {
Temp.push (This[i]);
}
}
return temp;
}
Front-end Questions-Mini test Sledgehammer (1)