练习案例
某一刻时间距离现实时间的时间
function countDown(time) {
var end = +new Date(time);
var newDay = +new Date();
var count_Down = Math.abs(end - newDay) / 1000;
// console.log(count_Down);
var seconds = parseInt(count_Down % 60),
minutes = parseInt(count_Down / 60 % 60),
house = parseInt(count_Down /3600 % 60);
let days = parseInt(count_Down/(3600*24)%24)
console.log(days)
seconds = seconds < 10 ? '0' + seconds : seconds;
minutes = minutes < 10 ? '0' + minutes : minutes;
house = house < 10 ? '0' + house : house;
return ('事件间隔' + days + '天' + house + '小时' + minutes + '分钟' + seconds + '秒');
}
console.log(countDown('2022-7-17 12:00:00'))
// 如果距离的在60分钟内的 算分钟数目 可以用 毫秒差值计算 absdt代表毫秒差
let min = new Date(absdt).getMinutes() // 条件在 absdt/1000
冒泡排序
//冒泡排序 从大到小
function bubble_sort(ar) {
var exc = 0;
for(var i = 0; i < ar.length; i++) {
for(var j = 0; j < ar.length-1-i; j++) {
if(ar[j] < ar[j+1]) {
exc = ar[j];
ar[j] = ar[j+1];
ar[j+1] = exc;
}
}
}
console.log(ar);
}
//从大到小的冒泡排序
function bubbleSort() {
for(var i = 0; i < arguments.length; i++) {
for(var j = 0; j < arguments.length - i; j++) {
if(arguments[j] > arguments[j + 1]) {
var exc = 0;
exc = arguments[j];
arguments[j] = arguments[j + 1];
arguments[j + 1] = exc;
}
}
}
return arguments;
}
简单计算器
// 简单计算器
function plush(one,two) {
var sum = 0;
sum = Number(one) + Number(two);
return sum;
}
function lower(one,two) {
var result = 0;
result = one - two;
return result;
}
function multiply(one,two) {
var result = one * two;
return result;
}
function remove(one,two) {
var result = one / two;
return result;
}
var num = prompt('欢迎使用简单计算器:/n 1.加法运算; /n 2.减法运算; /n 3.乘法运算 /n 4.乘法运算 /n 5.退出 /n 请输入您的选项');
var result;
while(num <= 5) {
if(num == 5) {
break;
}
var one = prompt('输入one'),two = prompt('输入two');
switch(num) {
case '1':
result = plush(one,two);
alert('结果是:'+result);
break;
case '2':
result = lower(one,two);
alert('结果是:'+result);
break;
case '3':
result = multiply(one,two);
alert('结果是:'+result);
break;
case '4':
result = remove(one,two);
alert('结果是:'+result);
break;
default : break;
}
num = prompt('欢迎使用简单计算器:/n 1.加法运算; /n 2.减法运算; /n 3.乘法运算 /n 4.乘法运算 /n 5.退出 /n 请输入您的选项');
}
随机点名
// 随机点名
var names = ['张三','张二','张一'];
var i = Math.floor(Math.random() * names.length);
console.log(i);
console.log(names[i]);
// 随机生成一个1-10的数字,并让用户输入一个数字
var num = prompt('输入一个1-10数字');
var rand = Math.floor(Math.random() * 10) + 1;
console.log(rand);
while(num != rand) {
if(num > rand) {
num = prompt('数字大了');
}
else {
num = prompt('数字小了');
}
}
alert('猜对了');
数组去重
var arr = ['c','a','z','a','x','c','b','c'];
function deWeight(arr) {
var arrs = [];
for(var i = 0; i < arr.length; i++) {
if(arrs.indexOf(arr[i]) == -1) {
arrs.unshift(arr[i]);
}
}
return arrs;
}
console.log(deWeight(arr));
小练习
//将数组中大于10的元素选择出来
var ar = [2,0,6,1,77,0,52,0,25,7];
var ars = [];
for(var i = 0; i < ar.length; i++) {
if(ar[i] > 10) {
ars[ars.length] = ar[i];
}
}
console.log(ars);
//去0
var ar = [2,0,6,1,77,0,52,0,25,7];
var ars = [];
for(var i = 0; i < ar.length; i++) {
if(ar[i] != 0) {
ars[ars.length] = ar[i];
}
}
console.log(ars);
//数组中的元素倒过来存储
var ar = [2,0,6,1,77,0,52,0,25,7];
var ars = [];
for(var i = ar.length-1; i >= 0; i--) {
ars[ars.length] = ar[i];
}
console.log(ars);
// 利用函数求任意个数的最大值
function maxValue() {
var max = arguments[0];
for(var i = 1; i < arguments.length; i++) {
if(max < arguments[i]) {
max = arguments[i];
}
}
return max;
}
//翻转数组
function overTrue(ar) {
var arrs = [];
for(var i = ar.length-1; i >= 0; i--) {
arrs[arrs.length] = ar[i];
}
return arrs;
}
var a = [45,56,10,34,5,3];
console.log(overTrue(a));
// 判断闰年还是平年
function year_def(year) {
if(year % 4 == 0 || year % 400 == 0) {
return '闰年';
}
else {
return '平年';
}
}
原创文章,作者:254126420,如若转载,请注明出处:https://blog.ytso.com/275243.html