10.3.JS-简单计算题,条件语句,面向,typeof,类型转换,typeof和类型转换题目

160 阅读3分钟

1. js简单计算题

1.1. 计算2的n次幂,n可以输入,n是自然数。

var n = parseInt(window.prompt('input'));
// 1 * 2 
// 1 * 2 * 2  
// 1 * 2 * 2 * 2 
// 1 * 2 * 2 * 2 * 2
let mul = 1;
for (let i = 0; i < n; i++) {
  mul *= 2;
}
document.write(mul);

1.2. 计算n的阶乘,n可输入

let n = parseInt(window.prompt('input'));
// 5! = 5 * 4 * 3 * 2 * 1
// 4! = 4 * 3 * 2 * 1
let mul = 1;
for (let index = 1; index <= n; index++) {
  mul *= index;
}
document.write(mul)

1.3. 著名的斐波那契数列 1 1 2 3 5 8

let n = parseInt(window.prompt('input'));
// f s t
// 1 1 2 3 5 8
//   f s t
// 每次次计算完,游标进一
let first = 1,
  second = 1,
  third;
if(n>2){
  for (let index = 0; index < n - 2; index++) {
    third = first + second;
    first = second;
    second = third;
  }
}else{
  third = 1;
} 
document.write(third);

1.4. 编写一程序,输入一个三位数的整数,输出时反向输出。如:输入456输出654

let n = 456;
// n % 10 * 100 => 600
// (n - n % 10) % 100 => 50
// (n - n % 10) % 100) / 100 => 4
console.log(n % 10 * 100 + (n - n % 10) % 100 + ((n - n % 10) - (n - n % 10) % 100) / 100 * 1);

1.5. 输入a,b,c三个数字,打印出最大的

let a = parseInt(window.prompt('input'));
let b = parseInt(window.prompt('input'));
let c = parseInt(window.prompt('input'));
if (a > b) {
  if (a > c) {
    document.write(a)
  } else {
    document.write(c)
  }
} else {
  if (b > c) {
    document.write(b)
  } else {
    document.write(c)
  }
}

1.6. 打印出100以内的质数

let count = 0;
for (let i = 2; i < 100; i++) {
  // 看看每一个 i 是否为质数
  for (let j = 1; j <= i; j++) {
    if (i % j == 0) {
      count++;
    }
  }
  if (count == 2) {
    document.write(i + " ");
  }
  count = 0;
}

// 如:100 == 10 * 10 == 4 * 25 == 2 * 50 因为10及以下数字被100整除2次及以上,100不是质数
// 数a 不能整除 数b 的平方根,数a 也不能整除大于 (数b平方根 - 数b)
// 减少循环次数
let countSqrt = 0;
for (let i = 2; i < 100; i++) {
  // 看看每一个 i 是否为质数
  for (let j = 1; j <= Math.sqrt(i); j++) {
    if (i % j == 0) {
      countSqrt++;
    }
  }
  if (countSqrt == 1) {
    document.write(i + " ");
  }
  countSqrt = 0;
}

2. 条件语句

if/else
switch case
break
continue

3. 面向过程和面向对象

  1. 面向过程:机器语言,从上到下,1-2-3-4,一步一步来
  2. 面向对象:人类语言,方法分配,a做1,b做2...,拆分执行步骤,分别交给不同对象负责

4. typeof

  1. 六种类型:number,string,boolean,undefined,object,function

5. 类型转换

5.1. 显示类型转换

  1. Number(mix)
  2. parseInt(string,radix)
  3. parseFloat(string)
  4. toString(radix)
  5. String(mix)
  6. Boolean
  • parseInt 和 toString结合使用,可以转换进制,如:
let a = 11111111;
a = parseInt(a,2); // 二进制 => 十进制
console.log(a); // 255 
a = a.toString(16); // 十进制 => 十六进制
console.log(a); // ff 

5.2. 隐示类型转换

  1. isNaN() 原理:隐式调用Number()
  2. 运算符:++ -- 正负号:+ -
  3. + 原理:如果含有字符串,隐式调用 String(),数字变成字符串
  4. - * / % 原理:隐式调用Number()
  5. > < <= >= 原理:隐式调用 Boolean()
  6. == != 原理:隐式调用 Number()

5.3. 不发生类型转换

  1. === !==

5.4. typeof,类型转换题目

console.log(typeof(a)); // "undefined"
console.log(typeof(undefined)); // "undefined" 
console.log(typeof(NaN)); // "number" 
console.log(typeof null); // "object"
var a = "123abc";
console.log(typeof(+a)); // "number" `+a => NaN`
console.log(typeof !!a); // "boolean"
console.log(typeof(a+"")); // "string"
console.log(1 == "1"); // true
console.log(NaN == NaN); // false
console.log(NaN == undefined); // false
console.log(11+"11"); // "1111"
console.log(1 === "1"); // false
console.log(parseInt(a)); // 123
var num = 123123.123123123
console.log(num.toFixed(3)); //  123123.123
console.log(typeof typeof b); // "string"
console.log(typeof b); // "undefined"