-
输出100个hello world.
for (var i = 1; i <= 100; i++) {
console.log("hello world");
} -
创建一个包含1~100的数组.
var array = [];
for (var i = 1; i <= 100; i++) {
array.push(i);
}
console.log(array); -
定义一个数组,遍历输出它的每一项.
var array = [55, 45, 77, 88, 99, 101, 120];
for (var i = 0; i < array.length; i++) {
console.log(array[i]);
} -
输出1-100的所有奇数.
for (var i = 1; i < 100; i++) {
if (i % 2 !== 0) {
console.log(i);
}
} -
定义一个数组,输出数组中所有的奇数.
var array = [44, 55, 23, 45, 77, 88];
for (var i = 0; i < array.length; i++) {
if (array[i] % 2 !== 0) {
console.log(array[i]);
}
} -
定义一个数组,找出所有的奇数,放入到一个新数组中.
var array1 = [44, 55, 23, 45, 77, 8];
var array2 = [];
for (var i = 0; i < array1.length; i++) {
if (array1[i] % 2 !== 0) {
array2.push(array1[i]);
}
}
console.log(array2); -
1~100求和.
var sum = 0;
for (var i = 1; i <= 100; i++) {
sum += i;
}
console.log(sum); -
求某个数的阶乘.
function factorial(n) {
var result = 1;
if (n < 1) {
return 0;
}
for (var i = 1; i <= n; i++) {
result *= i;
}
return result;
}
console.log(factorial(5)); -
数组求和.
sum = 0;
var array = [1, 2, 3, 4, 5];
for (var i = 0; i <= array.length; i++) {
sum += i;
}
console.log(sum); -
求数组中的奇数的个数.
var count = 0;
var array = [1, 2, 3, 4, 6];
for (var i = 0; i < array.length; i++) {
if (array[i] % 2 !== 0) {
count++;
}
}
console.log(count); -
求数组中的奇数和.
var sum = 0;
var array = [1, 2, 3, 4, 7];
for (var i = 0; i < array.length; i++) {
if (array[i] % 2 !== 0) {
sum = sum + array[i];
}
}
console.log(sum); -
数组中是否存在某个数,输出 是 或 否.
var array = [77, 88, 66, 54, 32, 21, 1];
var target = 1;
var isFind = false;
for (var i = 0; i < array.length; i++) {
if (array[i] === target) {
isFind = true;
break;
}
}
if ((isFind = true)) {
console.log("是");
} else {
console.log("否");
} -
数组中是否存在某个数,如果存在,则输出它所在的下标,如果不存在,则输出-1.
var array = [99, 55, 34, 25, 77, 65, 32];
var target = 65;
var index = -1;
for (var i = 0; i < array.length; i++) {
if (array[i] === target) {
index = i;
break;
}
}
console.log(index);
var array = [1, 2, 3, 477, 54, 88, 65];
for (var i = 0; i < array.length; i++) {
console.log(i);
} -
找到数组中第一个奇数和最后一个奇数,将它们求和.
var array = [55, 44, 65, 1, 2, 7, 15];
var sum = 0;
var isFind = false;
for (var i = 0; i < array.length; i++) {
if (array[i] % 2 !== 0) {
isFind = array[i];
}
}
var last = false;
for (var i = array.length - 1; i >= 0; i--) {
if (array[i] % 2 !== 0) {
last = array[i];
}
sum = isFind + last;
}
console.log(isFind, last, sum); -
有两个数组,看两个数组中是否都存在奇数,输出 是 或 否.
var array1 = [4, 44, 7, 10, 8, 22, 12];
var array2 = [22, 80, 22, 31];
var isFind1 = false;
var isFind2 = false;
for (var i = 0; i < array1.length; i++) {
if (array1[i] % 2 !== 0) {
isFind1 = array1[i];
break;
}
}
for (var i = 0; i < array2.length; i++) {
if (array2[i] % 2 !== 0) {
isFind2 = array2[i];
break;
}
}
if (isFind1 && isFind2) {
console.log("是");
} else {
console.log("否");
} -
输出一个对象的所有键值对.
var obj = {
a: 23,
c: "jk",
p: "75",
};
for (var key in obj) {
console.log(key + "=" + obj[key]);
} -
计算对象中字符串属性的数量.
var obj = {
a: "55",
jk: "45",
oppo: "54",
txp: 5,
};
var count = 0;
for (var key in obj) {
if (typeof obj[key] === "string") {
count++;
}
}
console.log(count); -
将一个对象所有的数字属性,转换为字符串,并在其前面加上¥.
var obj = {
name: "xxx",
balance: 199.8,
taken: 3000,
};
var newobj = {};
for (var key in obj) {
var velue = obj[key];
if (typeof velue === "number") {
newobj[key] = "¥" + velue;
} else {
newobj[key] = velue;
}
}
console.log(obj, newobj);