var num1 = +prompt("请输入数字:");
var num2 = +prompt("请输入数字:");
var num3 = +prompt("请输入数字:");
if(num1 > num2 && num1 > num3){
if(num2 > num3){
alert(num3 + "," + num2 + "," + num1);
}else{
alert(num2 + "," + num3 + "," + num1);
}
}else if(num2 > num1 && num2 > num3){
if(num1 > num3){
alert(num3 + "," + num1 + "," + num2);
}else{
alert(num1 + "," + num3 + "," + num2);
}
}else{
if(num1 > num2){
alert(num2 + "," + num1 + "," + num3);
}else{
alert(num1 + "," + num2 + "," + num3);
}
}
var score = prompt("请输入分数:");
switch(parseInt(score / 10)){
case 10:
case 9:
case 8:
case 7:
case 6:
console.log("及格啦");
break;
default:
console.log("不及格");
}
var date = prompt("请输入8位数日期:");
var year = parseInt(date / 10000)
var month = parseInt(date / 100) % 100;
var day = date % 100;
var count = 0;
var february = 28;
switch(month - 1){
case 12:
count += 31;
case 11:
count += 30;
case 10:
count += 31;
case 9:
count += 30;
case 8:
count += 31;
case 7:
count += 31;
case 6:
count += 30;
case 5:
count += 31;
case 4:
count += 30;
case 3:
count += 31;
case 2:
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
count += 29;
}else{
count += 28;
}
case 1:
count += 31;
break
}
count += day;
alert("第" + count + "天");
for(var i = 1000 , count = 0 ; i < 2001 ; i++){
if(i % 4 === 0 && i % 100 !== 0 || i % 400 === 0){
document.write(i + " ");
count++;
}
// count为计数器
if(count % 4 === 0){
document.write("
")
}
}
for(var i = 100 ; i < 1000 ; i++){
var a = i % 10; // 个位数字
var b = parseInt(i / 10) % 10; // 十位数字
var c = parseInt(i / 100); // 百位数字
// i === Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3)
// i === a ** 3 + b ** 3 + c ** 3
if(i === a * a * a + b * b * b + c * c * c){
document.write(i + "
");
}
}
for(var i = 2 ; i < 101 ; i++){
var flag = true; //使用中间量 flag 记录状态
for(var j = 2 ; j < i ; j++){ //代码优化1: j < i 改为 j <= Math.sqrt(i)
if(i % j === 0){
flag = false;
break; //代码优化2
}
}
if(flag){
console.log(i);
}
}
var num = prompt("请输入一个大于1的数字:");
var flag = true; //使用中间量 flag 记录状态
for(var i = 2 ; i < num ; i++){
if(num % i === 0){
flag = false;
break;
}
}
if(flag){
document.write(num + "是质数");
}else{
document.write(num + "不是质数");
}
for(var i = 0 ; i < 10 ; i++){
for(var j = 0 ; j < i + 1 ; j++){
document.write("* ");
}
document.write("
");
}
for(var i = 10 ; i > 0 ; i--){
for(var j = 0 ; j < i ; j++){
document.write("* ");
}
document.write("
")
}
for(var i = 0 ; i < 10 ; i++){
for(var j = 0 ; j < i * 2 + 1 ; j++){
document.write("* ");
}
document.write("
")
}
for(var i = 0 ; i < 10 ; i++){
for(var j = 0 ; j < i * 2 + 1 ; j++){
document.write("* ");
}
document.write("
")
}
for(var i = 8 ; i >= 0 ; i--){
for(var j = 0 ; j < i * 2 + 1 ; j++){
document.write("* ");
}
document.write("
")
}
for(var i = 0 ; i < 10 ; i++){
for(var j = 0 ; j < i * 2 + 1 ; j++){
if(j === 0 || j === i * 2){
document.write("* ");
}else{
document.write(" ");
}
}
document.write("
")
}
for(var i = 8 ; i >= 0 ; i--){
for(var j = 0 ; j < i * 2 + 1 ; j++){
if(j === 0 || j === i * 2){
document.write("* ");
}else{
document.write(" ");
}
}
document.write("
")
}
for(var i = 1 ; i < 10 ; i++){
for(var j = 1 ; j < i + 1 ; j++){
document.write(j + "*" + i + "=" + j * i + " ");
}
document.write("
");
}
function fun(a, b, c) {
var temp;
if(a > b){
temp = a;
a = b;
b = temp;
}
if(a > c){
temp = a;
a = c;
c = temp;
}
if(b > c){
temp = b;
b = c;
c = temp;
}
console.log("从小到大的顺序是" + a + "," + b + "," + c);
}
fun(11, -2, 5);
function printForm(N, M){
document.write("")
for(i = 0 ; i < N ; i++){
document.write("")
for(j = 0 ; j < M ; j++){
document.write("" + parseInt(Math.random() * 101) + " ")
}
document.write(" ")
}
document.write("
")
}
printForm(8, 5);
function foo(a){
return function(b){
return function(c){
return function(d){
console.log(a * b * c * d);
}
}
}
}
foo(1)(2)(3)(4);
<body id="body">
<button id="btn">变换背景颜色</button>
function setRandomColor(ele, color){
var r = color === "red" ? 255 : parseInt(100 * Math.random());
var g = color === "green" ? 255 : parseInt(100 * Math.random());
var b = color === "blue" ? 255 : parseInt(100 * Math.random());
random_color = "rgb(" + r + "," + g + "," + b + ")";
ele.style.cssText = "background-color : " + random_color;
}
btn.onclick = function(){
setRandomColor(body, "red");
setRandomColor(btn, "green");
}
<button id="btn">随机点名</button>
function getRandomName(){
var random_index = parseInt(arguments.length * Math.random());
return arguments[random_index];
}
btn.onclick = function(){
var random_name = getRandomName("吴京", "吴奇隆", "吴彦祖", "吴亦凡", "吴孟达", "吴晨");
console.log(random_name);
}
function calculate(a, b, operator){
switch(operator){
case "+":
return a + b;
case "-":
return a - b;
case "*":
return a * b;
case "/":
return a / b;
}
}
console.log(calculate(10, 2, "/"));
function fun(a){
return Number(a) || a === "0" ? true : false;
}
function generateCode(){
var code = "";
for(var i = 0; i < 4; i++){
code += parseInt(Math.random() * 10)
}
return code;
}
console.log(generateCode());
function generatePassword(i){
// 千位数字
var a = (parseInt(i / 1000) + 5) % 10;
// 百位数字
var b = (parseInt(i / 100) % 10 + 5) % 10;
// 十位数字
var c = (parseInt(i / 10) % 10 + 5) % 10;
// 个位数字
var d = (i % 10 + 5) % 10;
var temp = a;
a = d;
d = temp;
temp = b;
b = c;
c = temp;
return a * 1000 + b * 100 + c * 10 + d;
}
console.log(generatePassword(3759));
function countOdd(a, b){
// 默认ab则a,b换位
if(a > b){
var temp = a;
a = b;
b = temp;
}
for(var i = a , count = 0 ; i <= b ; i++){
for(var j = a ; j <= b ; j++){
if(i % 2 !== 0 && j !== i){
console.log("" + j + i);
count++;
}
}
}
return count;
}
console.log(countOdd(3, 0));
function calculator(n){
if(n === 1 || n === 2){
return 1 / n;
}
return 1 / n + calculator(n - 2);
}
console.log(calculator(5));
console.log(calculator(6));
function factorial(n){
if(n === 1){
return 1;
}
return n * factorial(n - 1);
}
var value = prompt("请输入数字:");
btn.onclick = function(){
console.log(factorial(value));
}
function fun(i){
if(i === 1 || i === 2){
return 1;
}
return fun(i - 1) + fun(i - 2);
}
console.log(fun(3));
function fun(a, b){
if(a % b === 0){
return b;
}
var r = a % b;
a = b;
b = r;
return fun(a , b);
}
console.log(fun(16, 24));
var arr = [];
var res = [];
var sum = 0;
for(var i = 1 ; i < 31 ; i++){
arr.push(2 * i);
sum += 2 * i;
if(i % 5 === 0){
res.push(sum / 5);
sum = 0;
}
}
console.log(res);
function map(arr){
var new_arr = [];
for(var i = 0; i < arr.length; i++){
// Math.round 解决数组元素中存在浮点数时的计算误差问题
new_arr.push(Math.round(arr[i] * 1.3 * 10) / 10);
}
return new_arr;
}
var arr = [1, 10, 100, 1000, 10000];
console.log(map(arr));
function sort(arr, value){
if(arr[arr.length - 1] <= value){
arr.push(value);
}else{
for(var i = 0 ; i < arr.length ; i++){
if(arr[i] > value){
arr.splice(i, 0, value);
break;
}
}
}
return arr;
}
var arr = [1, 5, 8, 10, 14];
var value = +prompt("请输入数字:");
console.log(sort(arr, value));
function norepeat(arr){
for(var i = 0; i < arr.length; i++){
for(var j = i + 1; j < arr.length; j++){
if(arr[i] === arr[j]){
arr.splice(j, 1);
j--;
}
}
}
return arr;
}
var arr = [1, 2, 2, 5, 5, 6, 4, 4, 7, 1, 3, 5];
console.log(norepeat(arr));
var arr =[];
var res = [];
var new_arr = [];
for(var i = 1; i < 26; i++){
res.push(i);
if(i % 5 === 0){
arr.push(res);
res = [];
}
}
for(var j = 0; j < 5 ; j++){
new_arr.push(arr[j].slice(0, j + 1));
}
console.log(arr);
console.log(new_arr);
function filter(arr, fn){
var res_arr = [];
for(var i = 0; i < arr.length; i++){
var res = fn(arr[i]);
if(res){
res_arr.push(arr[i]);
}
}
return res_arr;
}
function every(arr, fn){
var flag = true;
for(var i = 0; i < arr.length; i++){
if(!fn(arr[i])){
flag = false;
break;
}
}
return flag;
}
function some(arr, fn){
var flag = false;
for(var i = 0; i < arr.length; i++){
if(fn(arr[i])){
flag = true;
break;
}
}
return flag;
}
var arr = [1, 2, 3, 4, 5];
console.log(filter(arr, function(item){
return item > 3;
}))
console.log(every(arr, function(item){
return item > 3;
}))
console.log(some(arr, function(item){
return item > 3;
}))
var arr = [3, 5, 2, 7, 9, 11, 6];
for(var k = 0 ; k < arr.length - 1 ; k++){
// i < arr.length - 1 ==> 没必要比较最后一个数和undefined
// i < arr.length - 1 - k ==> 已经排序好的不用再比对
for(var i = 0 ; i < arr.length - 1 - k ; i++){
if(arr[i] > arr[i + 1]){
var temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
console.log(arr);
var arr = [3, 5, 2, 7, 9, 11, 6];
for(var k = 0 ; k < arr.length ; k++){
// 假定的最小值
var min = arr[k];
var index = k;
for(var i = 1 + k ; i < arr.length ; i++){
// 记录实际最小值
if(arr[i] < min){
min = arr[i];
index = i;
}
}
// 实际最小值和假定最小值换位
arr[index] = arr[k];
arr[k] = min;
}
console.log(arr);
var arr = [3, 2, 5, 7, 10];
// 建立对于最大值数量的桶,该步可省略
// 承载下标的容器
var bocket = [];
for(var i = 0; i < arr.length; i++){
// 往桶里注水
bocket[arr[i]] = 1;
}
arr.length = 0;
// 用for in可略过empty的数据
for(var attr in bocket){
// attr是字符串,需要转换为数字
arr.push(Number(attr));
}
console.log(arr);
function quickSort(arr){
// 终止条件
if(arr.length === 1 || arr.length === 0){
return arr;
}
// 找中点
var mid_index = parseInt(arr.length / 2);
var mid_num = arr[mid_index];
// 分左右
var left = [];
var right = [];
for(var i = 0; i < arr.length; i++){
// 不和中比对
if(i === mid_index){
continue;
}
// 分配左右
if(arr[i] > mid_num){
right.push(arr[i]);
}else{
left.push(arr[i]);
}
}
// 拼接左、中、右三者的结果(注意中点为值,需要加中括号变成数组)
return quickSort(left).concat([mid_num], quickSort(right));
}
var arr = [2, 5, 1, 3, 7];
console.log(quickSort(arr));
function getRandomInt(min , max){
return min + parseInt( Math.random() * (max - min + 1));
}
function generateCode(){
var min, max;
var code = "";
for(var i = 0 ; i < 4 ; i ++){
switch(getRandomInt(1 , 3)){
case 1 :
min = 48;
max = 57;
break;
case 2 :
min = 65;
max = 90;
break;
case 3 :
min = 97;
max = 122;
break;
}
var randomInt = getRandomInt(min , max);
code += String.fromCharCode(randomInt);
}
return code;
}
console.log(generateCode());
function fn(str){
var obj = {};
for(var i = 0; i < str.length; i++){
var attr = str[i];
if(obj[attr] === undefined){
obj[attr] = 1;
}else{
obj[attr]++;
}
}
console.log(obj);
var string = "";
for(var key in obj){
string += key;
}
console.log(string);
}
var str = "aabccd";
fn(str);
// 方法1
function judgePalindrome(str){
var n = parseInt((str.length + 1) / 2);
var flag = true;
for(var i = 0; i < n; i++){
if(str[i] !== str[str.length - i - 1]){
flag = false
break;
}
}
return flag;
}
// 方法2
function judgePalindrome(str){
// 字符串 => 数组 => 倒序数组 => 字符串
var new_str = str.split("").reverse().join("");
return str === new_str;
}
console.log(judgePalindrome("abccba"));
console.log(judgePalindrome("abcgcba"));
console.log(judgePalindrome("abcdba"));
function getColorNum(){
var color = "#";
for(var i = 0 ; i < 6 ; i ++){
color += parseInt(Math.random() * 15).toString(16);
}
return color;
}
console.log(getColorNum());
<head>
<style>
body{margin: 0; padding: 0; background: #b6b6b6; }
.center{ width: 1380px; height: 233px; margin: 0 auto; }
.center .box{ width: 260px; height: 193px; border-radius: 4px; float: left; margin-right: 20px; background: #fff; }
.center .box:last-of-type{ margin-right: 0; }
.center .box img{ width: 260px; height: 146px; }
.center .box p{ padding: 0 10px; margin-top: 4px; font: 14px/1.5 "PingFang SC","Lantinghei SC","Microsoft YaHei","HanHei SC","Helvetica Neue","Open Sans",Arial,"Hiragino Sans GB","微软雅黑",STHeiti,"WenQuanYi Micro Hei",SimSun,sans-serif; }
</style>
</head>
<body>
<div class="center" id="center"></div>
<script>
var json = `[
{
src : "https://image.gogoup.com/course/20181016/1539669673749.jpg",
title : "教你画卡通IP"
},
{
src : "https://image.gogoup.com/course/20190115/1547541116847.jpg",
title : "商业插画之时尚人物篇"
},
{
src : "https://image.gogoup.com/course/20180824/1535089522410.jpg",
title : "10堂课看懂西方艺术史"
},
{
src : "https://image.gogoup.com/course/20200301/1583074941120.png",
title : "UI设计适配与落地"
},
{
src : "https://image.gogoup.com/course/20181016/1539689983190.jpg",
title : "商业UI设计:全链式设计方法与流程"
}
]`
var item_list = eval(json);
var insert_html = "";
for(var i = 0; i < item_list.length; i++){
insert_html += '' +
'+ item_list[i].src +'" alt="">' +
''
+ item_list[i].title +'' +
''
}
var center = document.getElementById("center");
center.innerHTML = insert_html;
</script>
</body>
</html>
YYYY-MM-DD HH-MM-SS
function getTime(){
var d = new Date();
var res = "";
res += d.getFullYear() + "-";
res += getDblTime(d.getMonth() + 1) + "-";
res += getDblTime(d.getDate()) + " ";
res += getDblTime(d.getHours()) + "-";
res += getDblTime(d.getMinutes()) + "-";
res += getDblTime(d.getSeconds());
return res;
}
function getDblTime(num){
if(num >= 10){ return num; }
return "0" + num;
}
console.log(getTime());
多少天多少小时多少分钟多少秒
function dateReduce(options){
if(options.start === undefined || options.start === "now"){
start_d = new Date();
}else{
var start_str = formateDateArray(options.start);
var start_d = new Date(start_str);
}
var end_str = formateDateArray(options.end);
var end_d = new Date(end_str);
var reduce_ms = end_d.getTime() - start_d.getTime();
return {
days : parseInt(reduce_ms / 1000 / 3600 / 24),
hours : parseInt(reduce_ms / 1000 / 3600 % 24),
min : parseInt(reduce_ms / 1000 / 60 % 60),
sec : parseInt(reduce_ms / 1000 % 60 )
}
}
function formateDateArray( arr ){
var date_str = "";
switch(arr.length){
case 3 :
date_str = arr.join("/");
break;
case 6 :
date_str = arr.slice(0,3).join("/");
date_str += " " + arr.slice(3).join(":")
break;
}
return date_str;
}
var res = dateReduce({
// start : [1995,8,24,8,0,0],
start : "now",
end : [2020,3,6,0,0,0]
});
console.log(res);