记录实验,方便学习
let a = [4,9,5,27];
let max = a[0];
for(let i = 0;i<a.length;i++){
if(a[i]>max){
max = a[i]
}
}
console.log(max);
/* 取大于十的数 */
let a = [4,9,5,27,99,52];
// let max = a[0];
let am = [];
let k = 0;
for(let i = 0;i<a.length;i++){
if(a[i] >= 10){
// max = a[i]
// am[k] = a[i] //第一种方式
// k++ // 第一种方式
am[am.length] = a[i]; //第二种方式
}
}
console.log(am);
/* 倒序排列 */
let a = [1,2,3,4,5];
let b = [];
for(let i = a.length - 1;i>=0;i--){
b[b.length] = a[i];
}
console.log(b);
/* fibonacci 方式 */
let = fibonacci = {
[Symbol.iterator](){
let per = 0,cur = 1
return{
next(){
[ pre,cur ] = [ cur,pre + cur ]
return {done:false,value:cur}
}
}
}
}
for (let n of fibonacci){
if(n>1000)
break
console.log(n);
}
function fibonacci(){
if(n<0)throw new Error("输入的数字不能小于0");
if(n == 1 || n == 2){
return 1;
}else {
return fibonacci (n - 1) + fibonacci(n - 2)
}
}
console.log();
/* 冒泡排序 */
let a = [1,2,3,4,5];
// let a = [5,4,3,2,1];
// let b = [];
for(let i = 0;i<a.length - 1;i++){
for(let j = 0; j<a.length - i - 1;j++){
if (a[j] > a[j+1]) {
let am = a[j];
a[j] = a[j+1];
a[j+1] = am;
}
}
}
// console.log(a);
/* 二维数组 */
let a = [1,"鲁班",9,5,'hello',[546,74]];
console.log(a[5][0]);
/* for..of 数组*/
let a = [1,'小七',8,'hello'];
// console.log(a);
for(let value of a){
console.log(value);
}
function fun(num){
console.log(num);
var num = 123;
console.log(num);
}
var num = 456;
fun(num);