輸出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 ...
-
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);