var days = parseInt(89/24);
var hours = 89%24;
console.log(days + '天' + hours + '个小时'); // 3天17小时
var h = 80;
var c = 5 / 9.0 * (h-32);
c = parseInt(c*1000) / 1000;
console.log(c); // 26.666
var num1 = 705;
var num2 = 816;
var g1 = num1 % 10;
var g2 = num2 % 10;
var b1 = parseInt(num1 / 100);
var b2 = parseInt(num2 / 100);
var s1 = parseInt(num1 / 10) % 10;
var s2 = parseInt(num2 / 10) % 10;
console.log(g1+g2+b1+b2+s1+s2); // 27
var month = prompt('请输入一个月份');
month = Number(month);
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
console.log(31);
break;
case 2:
console.log(28);
break;
case 4:
case 6:
case 9:
case 11:
console.log(30);
break;
default:
console.log('你输入的月份有误!');
}
var month = prompt('输入一个月份') - 0;
var day = prompt('输入一个日期') - 0;
//记录总天数
var sum = 0;
switch (month) {
//以此类推...
case 4:
sum += 31;
case 3:
sum += 31;
case 2:
sum += 31;
case 1:
sum += day;
console.log(sum);
var year = prompt('输入一个年份');
if (year % 4 === 0 && year % 100 !== 0 || year % 400 == 0) {
console.log(year + '年是闰年');
} else{
console.log('不是闰年');
}
92号汽油,每升6元;如果大于等于20升,那么每升5.9元;
97号汽油,每升7元;如果大于等于30升,那么每升6.59元;
var bh = prompt('请输入汽油编号');
var l = prompt('请输入加多少升油');
switch (bh) {
case "92":
if (l < 20) {
total = 6 * l;
} else if (l >= 20) {
total = l * 5.9;
}
break;
case "97":
if (l < 30) {
total = 7 * l;
} else if (l >= 30) {
total = l * 6.95;
}
break;
default:
alert('编号错误');
}
alert(total);
var num = 10 + Math.floor(Math.random() * (100 - 10 + 1));
console.log(num);
var str = '20150628';
var day = str % 100;
var year = Math.floor(str / 10000);
var month = Math.floor(str / 100) % 100;
console.log(year + '年' + month + '月' + day + '日');
//生成一个1-3的随机数
var _random = 1 + Math.floor(Math.random() * 3);
//用户输入1-3的数字
var cunstomNum = prompt('输入1-3整数');
//进行比较
var result = null;
switch (cunstomNum) {
case '1':
switch (_random) {
case 1:
result = "平局";
break;
case 2:
result = "用户胜利";
break;
case 3:
result = "电脑胜利";
break;
case '2':
switch (_random) {
case 1:
result = "用户胜利";
break;
case 2:
result = "平局";
break;
case 3:
result = "电脑胜利";
break;
case '3':
switch (_random) {
case 1:
result = "电脑胜利";
break;
case 2:
result = "用户胜利";
break;
case 1:
result = "平局";
break;
}
}
alert(result);
var num = 1;
while (num < 100) {
if (num % 2 != 0) {
console.log(num);
}
num++;
}
var num = 0;
while (num <= 100) {
num++;
if (num % 5 == 0) {
continue;
}
console.log(num);
}
var sum = 0;
var i = 1;
while (i <= 100) {
sum += i;
i++;
}
console.log(sum); //5050
var sum = 0;
for (var i = 1; i < 101; i++) {
if (i % 2 == 0) {
sum += i;
}
}
console.log(sum);
//水仙花数是个/十/百位的数字相加等于本身(例如:153=1*1*1+5*5*5+3*3*3)
for (var i = 100; i < 1000; i++) {
var g = i % 10;
var s = Math.floor(i / 10) % 10;
var b = Math.floor(i / 100);
if (i == Math.pow(g, 3) + Math.pow(s, 3) + Math.pow(b, 3)) {
console.log(i); //153、370、371、407
}
}
// 阶乘 10*9*...*1
var j = 1;
for(var i = 10;i>0;i--){
j*=i;
}
console.log(j); //3628800
var j = 1;
var sum = 0;
for (var i = 1; i < 11; i++) {
j *= i;
sum += j;
}
console.log(sum); //4037913
//*
//* *
//* * *
//* * * *
for (var j = 0; j < 4; j++) {
for (var i = 0; i <= j; i++) {
document.write('* ');
}
document.write('');
}
//Math.pow(1.05,50)*10000;
var money = 10000;
for (var i = 0; i < 50; i++) {
money *= 1.05;
}
console.log(money);
//*
//* *
//* * *
//* * * *
//* * * *
//* * *
//* *
//*
for (var i = 1; i < 6; i++) {
for (var j = 0; j < i; j++) {
document.write('* ');
}
document.write('
');
}
for (var i = 1; i < 6; i++) {
for (var j = 0; j < 6 - i; j++) {
document.write('* ');
}
document.write('
');
}
for (var i = 1; i < 10; i++) {
for (var j = 1; j <= i; j++) {
var sum = i * j;
if (sum < 10) {
sum = ' ' + sum;
}
document.write(i + '*' + j + '=' + sum + ' ');
}
document.write('
');
}
for (var j = 100; j <= 200; j++) {
for (var i = 2; i < j; i++) {
var flag = true;
if (j % i == 0) {
flag = false;
break;
}
}
if (flag) {
console.log(j);
}
}
function getRandom(max, min) {
//设置默认值
min = min || 0;
//保证max为最大值,min为最小值
if (max < min) {
var _max = max;
max = min;
min = _max;
}
var _random = min + Math.floor(Math.random() * (max - min + 1));
return _random;
}
console.log(getRandom(1, 30));
var i = 0;
for (var h = 5; h > 0.1; i++) {
h *= 0.3;
}
console.log(i);
var sum = 0;
for(var i = 0; i < 64; i++) {
sum += Math.pow(2, i) ;
}
sum *= 0.00001;
console.log(sum);
function passSeven() {
for (var i = 1; i < 100; i++) {
if (i % 7 != 0 && i % 10 != 7 && Math.floor(i / 10) % 10 != 7) {
console.log(i)
}
}
}
passSeven();
//四位/六位验证码
function getRandom(max, min) {
min = min || 0;
return min + Math.floor(Math.random() * (max - min + 1))
}
function getCode(num) {
num = num || 4;
var str = '';
for (var i = 0; i < num; i++) {
str += getRandom(9);
}
console.log(str);
return str;
}
getCode();
getCode(6);
//随机颜色
function getColor() {
var str = '#';
for (var i = 0; i < 6; i++) {
var num = getRandom(15).toString(16);
str += num;
}
console.log(str);
return str;
}
getColor();
function getJc(num) {
var sum = 1;
for (var i = 1; i <= num; i++) {
sum *= i;
}
return sum;
}
console.log(getJc(10));
function getSum(n) {
var sum = 1;
var _num;
if (n % 2) {
// 能进来证明是奇数
num = 3;
} else {
num = 2;
}
// _num = n % 2 ? 3 : 2;
for (var i = _num; i <= n; i += 2) {
sum += 1 / i;
}
}
function getMax() {
var max = arguments[0];
for (var i = 1; i < arguments.length; i++) {
if (max < arguments[i]) {
max = arguments[i];
}
}
return max;
}
var max = getMax(1, 4, 5, 6, 2, 3, 6, 73, 2);
function jiecheng(num) {
if (num == 1) {
return 1;
}
return jiecheng(num - 1) * num;
}
console.log(jiecheng(5));
function fn(num) {
if (num == 1 || num == 2) {
return 1;
}
return fn(num - 2) + fn(num - 1);
}
console.log(fn(5));
上面是总结的一些常见的逻辑基础练习题供参考学习,有问题可互相交流学习。喜欢支持的可以关注哦。