版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
JavaScript 题库
1. 原始值和引用值
// 原始值操作值, 存于栈区
var a = 2;
var b = a;
b = 3;
console.log(a); //2
// 引用值操作地址, 存于堆区
var a = [1, 2, 3];
var b = a;
b[0] = '玩爬虫的小朋友'
console.log(a); // ['玩爬虫的小朋友', 2, 3]
2. 数据类型转换
/*
(1) Number 将其他数据类型转换成数字
(2) parseInt 从左往右取整, 碰到非数字就结束
(3) toString 将其他数据类型转换成字符串
*/
Number('123gkl'); ==> NaN
Number(typeof true) ==> NaN
Number(typeof typeof true) ==> NaN
Number(!NaN) ==> 1
parseInt('he123aven') ==> NaN
Boolean([]) ==> true
!!null ==> false
({}).toString() ==> '[object object]'
([23,false]).toString() ==> '23, false'
3. 自增自减
/*
++a 自增后的a
a++ 自增前的a
*/
var n1 = 10,
n2 = 20;
var n = n1++;
console.log(n); // n = 10
console.log(n1); // n1 = 11
n = ++n1; // n1 = 12 , n = 12
console.log(n); // n = 12
console.log(n1) // n1 = 12
n = n2--; // n2 = 19, n = 20
console.log(n); // n = 20
console.log(n2); // n2 = 19
n = --n2; // n2 = 18 , n = 18
console.log(n); // n = 18
console.log(n2); // n2 = 18
4. 运算符
/*
(1) !: 对布尔值取反
(2) 布尔值为false的只有6个: 0 '' false null undefined NaN
(3) 一元运算符+ : 对数字取正, 如果为非数字, 则先转化成数字
(4) + 有字符串拼接作用, 当+两侧有引用值时就是字符串的拼接
(5) [][[]] + [] ==> 数组取值 ==> [][0] + [] ==> 'undefined'+[] ==> 'undefined'
(6) - 没有字符串拼接作用, 当遇到非数字时会转换成数字进行运算
(7) NaN 参与的任何运算结果都为NaN
(8) chrome浏览器调试时需要加括号, 否则会出现异常
(9) && 与运算, 取假, 遇到假值就停止
(10) || 或运算, 求真, 遇到真值就停止
(11) 逻辑运算符优先级: ! > && > ||
(12) typeOf null ==> 'object'
(13) (!!’’+!!’ ’-!!false)&&null ==> (false + true - false)&&null ==> null
(14) (!!''+!!" "-!!false)||[1,2]&&null ==> (false + true -false)||[1,2]&&null ==> 1||[1,2]&&null ==> 1
*/
![]; // false
+[]; // 0
+![]; // 0
[]+[]; // ''
{}+{}; // '[object Object][object Object]'
{}+[]; // '[object Object]'
{a:0} +1; //'[object Object]1'
[]+![]; // 'false'
''+{}; // '[object Object]'
[][[]] + []; // 'undefined'
+!![]+[]; // '1'
1-{}; // NaN
1-[]; // 1
true - 1; // 0
{}-1; // NaN
[]==![]; //[]==false ==> 0 == 0 ==> true
true && [1,3]; // [1,3]
[1,3] && typeof null; // 'object'
[1,3] && NaN && typeof null; //NaN
false||'gkl'; // 'gkl'
null||typeof undefined; //'undefined'
null||typeof undefined|| typeof typeof undefined; //'undefined'
(!!'' + !!' '-!!false)&&null; // null
(!!''+!!" "-!!false)||[1,2]&&null; // 1
5. if else
// NaN和任何数据都不相等, 包括他自己
var num = parseFloat('height:20px'); // NaN(not a number, 本质就是number类型)
if (num == 20) {
console.log(20)
} else if (num == NaN) {
console.log(NaN)
} else if (typeof num == 'number') {
console.log('number'); // 'number'
} else {
console.log("什么都不是")
}
6. continue break
/*
(1) i = 0; 执行if语句; i++; 此时i= 1; continue跳过此次循环
(2) i = 3; 执行if语句; i++; 此时i= 4; continue跳过此次循环
(3) i = 6; 执行else语句; i+=3; 此时i = 9; break跳过整个循环
(4) consloe.log(9)
*/
for (var i = 0; i < 10; i += 2) {
if (i < 5) {
i++;
continue;
} else {
i += 3;
break
}
console.log(i);
}
console.log(i); // 9
7.1 图形打印
/*
*****
*****
*****
*****
*****
*/
for (var i = 0; i < 5; i++) {
for (var j = 0; j < 5; j++) {
document.write("*")
}
document.write('<br/>')
}
7.2 图形打印
/*
*
**
***
****
*****
*/
for (var i = 0; i < 5; i++) {
for (var j = 0; j < i + 1; j++) {
document.write("*")
}
document.write('<br/>')
}
7.3 图形打印
/*
11112
11122
11222
12222
22222
*/
for (var i = 0; i < 5; i++) {
for (var j = 0; j < 4 - i; j++) {
document.write('1')
}
for (var k = 0; k < i + 1; k++) {
document.write('2')
}
document.write("<br/>")
}
8. 求所有实参和
/*
arguments: 实参列表
*/
var a = function auto() {
var sums = 0;
for (var i = 0; i < arguments.length; i++) {
sums += arguments[i]
}
console.log(sums); // 12
}
a(3, 4, 5);
9. 通过递归求1~100的和
// 递归: 函数内部调用函数与自己
function sum(num) {
if (num == 100) {
return 100
} else {
return num + sum(num + 1)
}
}
var result = sum(1)
console.log(result); // 5050
10. 通过递归求n的阶乘
function Factorial(num) {
if (num == 1) {
return 1
}
return num * Factorial(num - 1)
}
var result = Factorial(5)
console.log(result) // 120
11. 某侦察队接到一项紧急任务,要求在A、B、C、D、E、F六个队员中尽可能多地挑若千人,但有以下限制条件:
●A和B两人中至少去一人;
●A和D不能起去;
●A、E和F三人中要派两人去
●B和C都去或都不去;
●C和D两人中去一个;若D不去,则E也不去。
问应当让哪几个人去?
/*
分析: 只有两种可能, 去或者不去, 所有可能遍历出来筛选
*/
for (var a = 0; a <= 1; a++) {
for (var b = 0; b <= 1; b++) {
for (var c = 0; c <= 1; c++) {
for (var d = 0; d <= 1; d++) {
for (var e = 0; e <= 1; e++) {
for (var f = 0; f <= 1; f++) {
if (a + b >= 1 && a + d !== 2 && a + e + f == 2 && b == c && c + d == 1 && d + e == 0) {
console.log(a, b, c, d, e, f) // 1 1 1 0 0 1
}
}
}
}
}
}
}
12. 打印水仙花数
for (var i = 100; i <= 999; i++) {
a = parseInt(i / 100);
b = parseInt(i / 10) % 10;
c = i % 10;
if (a ** 3 + b ** 3 + c ** 3 == i) {
console.log(i)
}
}
13. 输出斐波拉切数列(1, 1, 2, 3, 5, 8, 13, …)
var feiBo = function FB(num) {
if (num == 1) {
return 1
}
if (num == 2) {
return 1
}
return FB(num - 1) + FB(num - 2)
}
var result = feiBo(10);
14. 有一对幼兔,幼兔1个月后长成小兔,小兔1个月后长成成兔并生下一对幼兔,问几年后有多少对兔子,幼兔、小兔、成兔对数分别是多少
分析: 推理出前几个月的兔子数, 然后找规律
月份 | 幼兔 | 小兔 | 成兔 | 总数 |
---|---|---|---|---|
1 | 1 | 0 | 0 | 1 |
2 | 0 | 1 | 0 | 1 |
3 | 1 | 0 | 1 | 2 |
4 | 1 | 1 | 1 | 3 |
5 | 2 | 1 | 2 | 5 |
6 | 3 | 2 | 3 | 8 |
7 | 5 | 3 | 5 | 13 |
8 | 8 | 5 | 8 | 21 |
// x代表幼兔; y代表小兔; z代表成兔
var x = 1, y = 0, z = 0;
function num(month) {
for (var i = 0; i <= month; i++) {
console.log(x, y, z); // 在前面打印是因为要输出第一个月的兔子数
// 分析得出以下规律; 会数学归纳法就更简单了
z = y + z;
y = x;
x = z;
}
}
15. 求100以内所有奇数和
// 1 + 3 + 5 + 7 + ....
// 方法一: 递归
function sum(num) {
if (num == 1) {
return 1
}
return num + sum(num - 2)
}
// 方法二: for循环
var sum = 0;
for (var i = 1; i < 100; i += 2) {
sum += i
}
console.log(sum)
16. 打印九九乘法表
document.write('<table>')
for (var i = 1; i <= 9; i++) {
document.write('<tr>')
for (var j = 1; j < i + 1; j++) {
document.write('<th>')
document.write(i + '*' + j + '=' + i * j + '\t')
document.write('</th>')
}
document.write('</tr>')
// document.write('<br/>')
}
document.write('</table>')
/* css样式 */
table {
border-collapse: collapse;
margin: 100px auto;
}
th {
border: 1px solid red;
height: 30px;
width: 100px;
}
17. 输出100以内含有7或能被7 整除的数
for (var i = 0; i <= 100; i++) {
if (i % 7 == 0 || i % 10 == 7 || parseInt(i / 10) == 7) {
console.log(i)
}
}
18. 输出数组中最大值及其索引
function fn(arr) {
// 随便赋一个初始值就行
var maxNum = 0;
var maxNumIndex = 0;
for (var i = 0; i < arr.length; i++) {
if (maxNum < arr[i]) {
maxNum = arr[i]
maxNumIndex = i
}
}
console.log(maxNum, maxNumIndex)
}
fn([22,33,5]) // 33 1
19. 求100以内的所有质数
// 除1和本身外不能被任何数整除
for (var i = 2; i <= 97; i++) {
var num = 0;
for (var j = 1; j <= i; j++) {
if (i % j === 0) {
num++;
}
}
if (num === 2) {
// 如果num = 2就为质数(1和本身)
console.log(i);
}
}
20. JS执行顺序
function auto(b) {
var a = 2;
console.log(b); // fn
function b() {}
}
auto(2); //AO{b:fn, a:undefined}