for
let nums = [1, 2, 3, 4, 5];
let sum = 0;
for (let i = 0; i < nums.length; i++) {
sum += nums[i];
}
console.log(sum);
let matrix = [[1, 2], [3, 4], [5, 6]];
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
console.log(matrix[i][j]);
}
}
let str = 'Hello';
for (let i = 0; i < str.length; i++) {
console.log(str[i]);
}
let arr = [1, 2, 3, 4, 5];
for (let i = 0; i < arr.length; i++) {
console.log(arr[i] ** 2);
}
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 === 0) {
console.log(numbers[i]);
}
}
for...in
let obj = {a: 1, b: 2, c: 3};
for (let prop in obj) {
console.log(`${prop}: ${obj[prop]}`);
}
let objWithMethods = {
a: function() { console.log('A!'); },
b: function() { console.log('B!'); },
c: function() { console.log('C!'); },
};
for (let method in objWithMethods) {
objWithMethods[method]();
}
let arr = ['Apple', 'Banana', 'Cherry'];
for (let index in arr) {
console.log(`index: ${index}, value: ${arr[index]}`);
}
function Person(first, last, age) {
this.firstName = first;
this.lastName = last;
this.age = age;
}
let myFather = new Person("John", "Doe", 50);
for (let property in myFather) {
console.log(property + " : " + myFather[property]);
}
let original = [1, 2, 3, 4, 5];
let copy = [];
for (let index in original) {
copy[index] = original[index];
}
console.log(copy);
for...of
let fruits = ['apple', 'banana', 'cherry'];
for (let fruit of fruits) {
console.log(fruit);
}
let str = 'Hello';
for (let char of str) {
console.log(char);
}
let map = new Map();
map.set('a', 1);
map.set('b', 2);
map.set('c', 3);
for (let value of map.values()) {
console.log(value);
}
let set = new Set([1, 2, 3, 4, 5]);
for (let value of set) {
console.log(value);
}
function printArguments() {
for (let arg of arguments) {
console.log(arg);
}
}
printArguments('a', 'b', 'c');
while
let nums = [1, 2, 3, 4, 5];
let sum = 0;
let i = 0;
while (i < nums.length) {
sum += nums[i];
i++;
}
console.log(sum);
let str = 'Hello';
let j = 0;
while (j < str.length) {
console.log(str[j]);
j++;
}
let countdown = 5;
while (countdown > 0) {
console.log(countdown);
countdown--;
}
let arr = [1, 2, 3, 4, 5];
let k = 0;
while (k < arr.length) {
console.log(arr[k] ** 2);
k++;
}
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let l = 0;
while (l < numbers.length) {
if (numbers[l] % 2 === 0) {
console.log(numbers[l]);
}
l++;
}
do...while
let nums = [1, 2, 3, 4, 5];
let sum = 0;
let i = 0;
do {
sum += nums[i];
i++;
} while (i < nums.length);
console.log(sum);
let str = 'Hello';
let j = 0;
do {
console.log(str[j]);
j++;
} while (j < str.length);
let countdown = 5;
do {
console.log(countdown);
countdown--;
} while (countdown > 0);
let arr = [1, 2, 3, 4, 5];
let k = 0;
do {
console.log(arr[k] ** 2);
k++;
} while (k < arr.length);
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let l = 0;
do {
if (numbers[l] % 2 === 0) {
console.log(numbers[l]);
}
l++;
} while (l < numbers.length);
Array.prototype.forEach()
let nums = [1, 2, 3, 4, 5];
let sum = 0;
nums.forEach(function(num) {
sum += num;
});
console.log(sum);
let str = 'Hello';
Array.from(str).forEach(function(char) {
console.log(char);
});
let arr = [1, 2, 3, 4, 5];
arr.forEach(function(num) {
console.log(num ** 2);
});
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
numbers.forEach(function(num) {
if (num % 2 === 0) {
console.log(num);
}
});
let people = [{name: 'Alice', age: 20}, {name: 'Bob', age: 25}, {name: 'Charlie', age: 30}];
people.forEach(function(person) {
console.log(person.name);
});
Array.prototype.map()
let nums = [1, 2, 3, 4, 5];
let squares = nums.map(function(num) {
return num ** 2;
});
console.log(squares);
let people = [{name: 'Alice', age: 20}, {name: 'Bob', age: 25}, {name: 'Charlie', age: 30}];
let names = people.map(function(person) {
return person.name;
});
console.log(names);
let numbers = [1, 2, 3, 4, 5];
let strings = numbers.map(function(number) {
return number.toString();
});
console.log(strings);
let fruits = ['apple', 'banana', 'cherry'];
let lengths = fruits.map(function(fruit) {
return fruit.length;
});
console.log(lengths);
let persons = [{name: 'Alice', age: 20}, {name: 'Bob', age: 25}, {name: 'Charlie', age: 30}];
let updatedPersons = persons.map(function(person) {
return {...person, age: person.age + 1};
});
console.log(updatedPersons);
Array.prototype.filter()
let nums = [1, 2, 3, 4, 5];
let evens = nums.filter(function(num) {
return num % 2 === 0;
});
console.log(evens);
let words = ['apple', 'banana', 'cherry', 'do'];
let longWords = words.filter(function(word) {
return word.length > 4;
});
console.log(longWords);
let numbers = [1, 10, 20, 30, 40];
let inRange = numbers.filter(function(number) {
return number > 10 && number < 40;
});
console.log(inRange);
let people = [{name: 'Alice', age: 20}, {name: 'Bob', age: 25}, {name: 'Charlie', age: 30}];
let oldEnough = people.filter(function(person) {
return person.age >= 25;
});
console.log(oldEnough);
let strings = ['Hello', 'World', 'JavaScript', 'Fun'];
let withJ = strings.filter(function(str) {
return str.includes('J');
});
console.log(withJ);
Array.prototype.reduce()
let nums = [1, 2, 3, 4, 5];
let sum = nums.reduce(function(total, num) {
return total + num;
}, 0);
console.log(sum);
let numbers = [1, 10, 20, 30, 40];
let maxNum = numbers.reduce(function(max, num) {
return Math.max(max, num);
}, numbers[0]);
console.log(maxNum);
let duplicates = [1, 2, 2, 3, 4, 4, 5];
let unique = duplicates.reduce(function(unique, num) {
if (!unique.includes(num)) {
unique.push(num);
}
return unique;
}, []);
console.log(unique);
let people = [{name: 'Alice', age: 20}, {name: 'Bob', age: 25}, {name: 'Charlie', age: 30}];
let totalAge = people.reduce(function(total, person) {
return total + person.age;
}, 0);
console.log(totalAge);
let words = ['Hello', 'World', 'JavaScript', 'Is', 'Fun'];
let sentence = words.reduce(function(sentence, word) {
return sentence + ' ' + word;
}, '');
console.log(sentence.trim());
Array.prototype.every() 와 Array.prototype.some()
let nums = [2, 4, 6, 8, 10];
let allEven = nums.every(function(num) {
return num % 2 === 0;
});
console.log(allEven);
let strings = ['Hello', 'World', 'JavaScript', 'Is', 'Fun'];
let hasJS = strings.some(function(str) {
return str === 'JavaScript';
});
console.log(hasJS);
let people = [{name: 'Alice', age: 20}, {name: 'Bob', age: 25}, {name: 'Charlie', age: 30}];
let allOldEnough = people.every(function(person) {
return person.age >= 20;
});
console.log(allOldEnough);
let numbers = [{num: 1}, {num: 2}, {num: 3}, {num: 4}];
let hasThree = numbers.some(function(item) {
return item.num === 3;
});
console.log(hasThree);
let mixed = [1, '2', 3, '4', 5];
let allNumbers = mixed.every(function(item) {
return typeof item === 'number';
});
console.log(allNumbers);