发布时间:2022-08-18 文章分类:编程知识 投稿人:李佳 字号: 默认 | | 超大 打印
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
//简易atm机
/* var sum = 100;
do {
var text = parseFloat(prompt("1.存钱\n2.取钱\n3.显示余额\n4.退出"));
switch (text) {
case 1:
var cq = parseFloat(prompt("存钱金额"));
sum += cq;
alert("金额为" + sum);
break;
case 2:
qq = parseFloat(prompt("取钱金额"));
if (qq > sum) {
alert("金额不足");
} else {
sum -= qq;
alert("金额为" + sum);
break;
}
case 3:
alert("余额" + sum);
break;
case 4:
alert("你已经退出");
break
default:
alert("没有这个功能");
break;
}
} while (text !== 4); */

//求最大值
/* var arr = [2, 6, 1, 77, 52, 25, 7, 99];
max = arr[0];
for (var i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
// max=arr[i]>max?arr[i]:max;
}
console.log(max); */

//求最小值
/* var arr1 = [2, 6, 1, 77, 52, 25, 7, 99];
min = arr[0];
for (var i = 1; i < arr1.length; i++) {
if (arr1[i] < min) {
min = arr1[i];
}
// max=arr[i]>max?arr[i]:max;
}
console.log(min); */

//输出1-10的整数在arr2里面
/* var arr2 = [];
for (var i = 0; i < 10; i++) {
arr2[i] = i + 1;
}
console.log(arr2); */

//求数组大于10的存在arr4数组里面
/* var arr3 = [2, 0, 6, 1, 77, 0, 52, 0, 25, 7];
var arr4 = [];
for (var i = 0; i < arr3.length; i++) {
if (arr3[i] >= 10) {
arr4[arr4.length] = arr3[i];
}
}
console.log(arr4); */

//求数组小于10的存在arr4数组里面
/* var arr3 = [2, 0, 6, 1, 77, 0, 52, 0, 25, 7];
var arr4 = [];
for (var i = 0; i < arr3.length; i++) {
if (arr3[i] <= 10) {
arr4[arr4.length] = arr3[i];
}
}
console.log(arr4); */

//反转数组
/* var arr = ["red", "green", "blue", "pink", "purple", "hotpink"];
arr1 = [];
for (var i = arr.length - 1; i >= 0; i--) {
arr1[arr1.length] = arr[i];
}
console.log(arr1); */

//冒泡排序
/* var arr = [5, 3, 1, 4, 2];
for (var i = 0; i <= arr.length - 1; i++) { //i只是判断要循环的趟数
for (var j = 0; j < arr.length - i - 1; j++) { //j是判断循环的次数
if (arr[j] > arr[j + 1]) {
var temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
console.log(arr); */

// 简易计算器
/* num1 = prompt("请输入第一个数字");
ysf = prompt("请输入运算符");
num2 = prompt("请输入第个二数字");
function getJs() {
switch (ysf) {
case "+":
return parseFloat(num1) + parseFloat(num2);
break;
case "-":
return parseFloat(num1) - parseFloat(num2);
break;
case "*":
return parseFloat(num1) * parseFloat(num2);
break;
case "/":
return parseFloat(num1) / parseFloat(num2);
break;
default:
alert("没有这个运算");
break;
}
}
alert("运算结果为" + getJs(num1, ysf, num2)); */

//输入任意两个数求最大值
/* var num1=prompt('请输入第一个值');
var num2=prompt('请输入第二个值');
function getMax(num1, num2) {
return num1 > num2 ? num1 : num2;
}
alert(getMax(num1,num2)); */

//输入任意三个数求最大值
/* var num1 = prompt("请输入第一个值");
var num2 = prompt("请输入第二个值");
var num3 = prompt("请输入第二个值");
function getMax(num1, num2, num3) {
arr = [num1, num2, num3];
max = arr[0];
for (var i = 1; i <= arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
alert(getMax(num1, num2,num3)); */

//判断是否为质素
/* var num = prompt("请输入一个数");
function getZs(num) {
if (num <= 2) {
return false;
}

for (var i = 2; i < num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
alert(getZs(num)); */

//arguments求最大值
/* function getMax() {
var max = arguments[0];
for (var i = 1; i < arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i];
}
}
return max;
}
console.log(getMax(56, 89, 33, 4));
console.log(getMax(56, 5, 89, 353, 4));
console.log(getMax(56, 89, 33, 994)); */

//利用函数翻转任意数组 reverse翻转的意思
/* function reverse(arr) {
var newArr = [];
for (var i = arr.length - 1; i >= 0; i--) {
newArr[newArr.length] = arr[i];
}
return newArr;
}
var arr1 = reverse([1, 2, 3, 4, 5]);
console.log(arr1);
var arr2 = reverse([2, 6, 8, 10, 12]);
console.log(arr2); */

//函数冒泡排序 sort排序的意思
/* function sort(arr) {
for (var i = 0; i < arr.length - 1; i++) {
for (var j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
var temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
var arr1 = sort([9, 62, 8, 2, 3, 66, 89, 10]);
console.log(arr1); */

//函数判断闰年 是返回true 否则false
/* function isRunnian(year) {
var flag = false;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
flag = true;
}
return flag;
}
console.log(isRunnian(2000));
console.log(isRunnian(1999));

//利用调用函数判断闰年天数 if 里面默认为是true else 则是false
function getDate() {
var year1 = prompt("请输入年份");
if (isRunnian(year1)) {
alert("第二月有29天");
} else {
alert("第二月有28天");
}
}
getDate(); */

//利用{}按照要求创建对象
/* var obj = {
name: "可可",
type: "阿拉斯加犬",
age: 18,
color: "pink",
say: function () {
console.log("汪汪汪");
console.log("演电影");
}
};
console.log(obj.name);
console.log(obj.age);
obj.say(); */

//利用new Object根据要求创建对象
/* var obj1=new Object();
obj1.name='鸣人';
obj1.sex='男';
obj1.age=19;
obj1.skill=function(){
console.log('影分身术');
}
console.log(obj1.name);
console.log(obj1['age']);
obj1.skill(); */

//利用构造函数创建对象
/* function King(name,type,blood){
this.name=name;
this.type=type;
this.blood=blood;
this.attack=function(attack){
console.log(attack);
}
}
var lp=new King('廉颇','力量型','500血量')
console.log(lp.name);
console.log(lp['type']);
lp.attack('近战')

var hy=new King('后裔','射手型','100血量')
console.log(hy.name);
console.log(hy['type']);
lp.attack('远程'); */

//利用函数反转数组
/* function getFz(arr) {
newArr = [];
for (var i = arr.length - 1; i >= 0; i--) {
newArr[newArr.length] = arr[i];
}
return newArr;
}
var arr1 = getFz([1, 2, 3, 4, 5]);
console.log(arr1); */

//利用函数实现对数字数组的排序
/* function paiXu(arr) {
for (var i = 0; i < arr.length - 1; i++) {
for (var j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
var temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
var arr1 = paiXu([5, 4, 3, 2, 1]);
console.log(arr1); */

//建议计算器2
/* while (true) {
var num = parseInt(
prompt("1.加法运算\n2.减法运算\n3.乘法运算\n4.除法运算\n5.退出")
);
if (num == 5) {
alert("正在退出");
break;
}
switch (num) {
case 1:
var num1 = parseFloat(prompt("请输入第一个数"));
var num2 = parseFloat(prompt("请输入第二个数"));
alert(num1 + num2);
break;
case 2:
var num1 = parseFloat(prompt("请输入第一个数"));
var num2 = parseFloat(prompt("请输入第二个数"));
alert(num1 - num2);
break;
case 3:
var num1 = parseFloat(prompt("请输入第一个数"));
var num2 = parseFloat(prompt("请输入第二个数"));
alert(num1 * num2);
break;
case 4:
var num1 = parseFloat(prompt("请输入第一个数"));
var num2 = parseFloat(prompt("请输入第二个数"));
alert(num1 / num2);
break;

default:
alert("没有这个运算");
break;
}
} */

//['red', 'blue', 'red', 'green', 'pink','red'], 求 red 出现的位置和次数
/* var arr1 = ["red", "blue", "red", "green", "pink", "red"];
var index = arr1.indexOf("red");
var num = 0;
while (index !== -1) {
console.log(index);
num++;
index = arr1.indexOf("red", index + 1);
}
console.log(num); */ //判断字符串中出现最多的值,输出值和几次
/* var str = "abcoefoxyozzopp";
var o = {};
for (var i = 0; i < str.length; i++) {
var chars = str.charAt(i);
if (o[chars]) { //o[chars]是属性值,应为chars是字符串型所以不能直接o.chars
o[chars]++;
} else {
o[chars] = 1; //可以理解为在o对象里创建了chars属性并赋值为1;
}
}
console.log(o);
var max = 0;
var ch = "";
for (k in o) { //遍历对象k是属性,o[k]是属性值
if (o[k] > max) {
max = o[k];
ch = k;
}
}
console.log("出现最多的属性是" + ch);
console.log("出现的次数是" + max); */

//给定一个字符串,如:'abaasdffggghhjjkkgfddsssss3444343'
/* 1.求出字符串的长度,
2.取出指定位置的字符,如0,3,5,9等
3.查找指定字符串是否有以上字符存在,如:i,c,b等
4.替换指定字符,如:g替换成22,ss替换成b等操作方法
5.截取指定开始位置到结束位置的字符串,如:取得1-5的字符串
6.找出以上字符串中出现最多的字符和出现次数 */
/* var str = "abaasdffggghhjjkkgfddsssss3444343";
console.log(str.length);

console.log(str.charAt(0));
console.log(str.charAt(3));
console.log(str.charAt(5));
console.log(str.charAt(9));

function panDuan(num) {
if (num !== -1) {
return "有该字符串";
} else {
return "没有该字符串";
}
}
console.log(panDuan(str.indexOf("i")));
console.log(panDuan(str.indexOf("c")));
console.log(panDuan(str.indexOf("b")));

function tiHuan(string) {
while (str.indexOf(string) !== -1) {
str = str.replace(string, 22);
}
return str;
}
console.log(tiHuan("g"));

var str = "abaasdffggghhjjkkgfddsssss3444343"; //上面tiHuan函数把str替换,重新声明字符串
console.log(str.substr(1, 5));

var n = {};
for (var i = 0; i < str.length; i++) {
var chars = str.charAt(i);
if (n[chars]) {
n[chars]++;
} else {
n[chars] = 1;
}
}
console.log(n); //先转换成对象,在遍历对象求出现最多的字符和次数
var max = 0;
var ch = "";
for (b in n) {
if (n[b] > max) {
max = n[b];
ch = b;
}
}
console.log("出现最多的字符是" + ch + "出现了" + max); */
</script>
</body>
</html>