break문, continue문
<!DOCTYPE html>
<html lang="en">
<head>
<title>break문, continue문</title>
<script>
for(let i = 0; i < 5; i++){
console.log(i);
if(i==3){
console.log("i가 3일 때 탈출");
break;
}
}
for(let i=0; i<10; i++){
if(i%2 == 0){
continue;
}
console.log(i);
}
for(let i=0; i<100;i++){
if(i>50){
break;
}
console.log(i);
}
for (let i = 1; i <= 100; i++) {
if (!(i % 3 == 0 || i % 5 == 0)) {
continue;
}
console.log(i);
}
let count = 0;
for (let i = 1; i <= 100; i++) {
if (i % 10 == 3 || i % 5 == 0) {
continue;
}
console.log("밟은 돌 : " + i);
count++;
}
console.log("밟은 총 돌의 갯수 : " + count);
</script>
</head>
<body>
</body>
</html>
배열
<!DOCTYPE html>
<html lang="en">
<head>
<title>배열</title>
<script>
let arrNums = [10,20,30];
console.log(arrNums[0]);
console.log(arrNums[1]);
console.log(arrNums[2]);
for(let i = 0; i<3; i++){
console.log(arrNums[i]);
}
for(let num of arrNums){
console.log(num);
}
arrNums.forEach(
function(num){
console.log(num);
}
);
arrNums.forEach(
(num) => {
console.log(num);
}
);
let names = ["장미", "수선화", "천일홍"];
console.log("< for문 사용 출력 >");
for (i = 0; i < 3; i++) {
console.log(names[i]);
}
console.log("< for of문 사용 출력 >");
for (let num of names) {
console.log(num);
}
</script>
</head>
<body>
</body>
</html>
이중 반복문
<!DOCTYPE html>
<html lang="en">
<head>
<title>이중 반복문</title>
<script>
for(let i=0; i<5; i++){
console.log("i:"+i);
for(let j=0;j<5;j++){
console.log("j:"+j);
}
}
for(let i=2; i<10; i++){
console.log(i+"단");
for(let j=1; j<10; j++){
console.log(i + "*" + j + "=" + i*j);
}
}
let num = prompt('구구단 숫자 입력 : ');
let arrDans = num.split(",")
console.log(result);
for (let i = Number(arrDans[0]); i <= Number(arrDans[1]); i++) {
console.log(i + "단");
for (let j = 1; j < 10; j++) {
console.log(i + "*" + j + "=" + i * j);
}
}
</script>
</head>
<body>
</body>
</html>
2차배열
<!DOCTYPE html>
<html lang="en">
<head>
<title>2차 배열</title>
<script>
let nums = [10, 20, 30];
let nums2D = [
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
];
console.log(nums2D[0][0]);
console.log(nums2D[2][1]);
for (let i = 0; i < nums2D.length; i++) {
for (let j = 0; j < nums2D[0].length; j++) {
console.log(nums2D[i][j]);
}
}
nums2D.sort((a, b) => b[0] - a[0]);
for (let nums of nums2D) {
console.log(nums);
}
let max = Number.MIN_SAFE_INTEGER;
for (let i = 0; i < nums2D.length; i++) {
for (let j = 0; j < nums2D[0].length; j++) {
if (max < nums2D[i][j]) {
max = nums2D[i][j];
}
}
}
console.log(max);
let sum = 0;
for (let i = 0; i < nums2D.length; i++) {
for (let j = 0; j < nums2D[0].length; j++) {
sum = sum + nums2D[i][j];
}
}
console.log(sum);
let students = [
[1, '홍길동', 70, 20],
[2, '변사또', 60, 30],
[3, '사임당', 50, 40],
[4, '김진사', 90, 50],
];
let maxScore = 0;
for (let student of students) {
console.log(student);
console.log(student[2]);
if (maxScore < student[2]) {
maxScore = student[2];
}
}
console.log("최대점수:" + maxScore);
students.sort((a,b)=>b[2]-a[2]);
console.log(students);
let index = 1;
for(let student of students){
console.log(`${index++} ${student[1]} ${student[2]}`);
}
</script>
</head>
<body>
</body>
</html>
반복문의 레이블
<!DOCTYPE html>
<html lang="en">
<head>
<title>반복문의 레이블 label</title>
<script>
loop1:
for(let i=0; i<5; i++){
loop2:
for(let j=0; j<5; j++){
if(j>2){
break loop1;
}
if(i>3){
continue loop1;
}
}
}
let lottoNums = [0,0,0,0,0,0];
loop1:
for(let i=0; i<6; i++){
let radomNum = parseInt(Math.random() * 45 + 1);
console.log(radomNum);
loop2:
for(let j=0; j<i; j++){
if(lottoNums[j] == radomNum){
console.log("재추첨!");
i--;
continue loop1;
}
}
lottoNums[i] = radomNum;
}
</script>
</head>
<body>
</body>
</html>
반복문 for, while, do while
<!DOCTYPE html>
<html lang="en">
<head>
<title>반복문 for, while, do while</title>
<script>
for (let i = 0; i < 5; i++) {
console.log(i);
}
console.log("____________________________");
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
console.log("1.__________________________");
i = -10;
while (i < 11) {
console.log(i);
i++;
}
console.log("2.__________________________");
i = 0
while (i < 11) {
console.log(i);
i += 3;
}
console.log("3.__________________________");
i = 10
while (i > -21) {
console.log(i);
i -= 5;
}
console.log("4.__________________________");
i = 1
while (i < 101) {
if (i % 3 == 0) {
console.log(i);
}
i++;
}
console.log("5.__________________________");
i = 1
let count = 0;
while (i < 101) {
if (i % 10 == 3) {
console.log("#" + i);
count++;
}
if (Math.floor(i / 10) == 3) {
console.log("_" + i);
count++;
}
i++;
}
console.log("3이 들어가는 숫자의 갯수:" + count);
</script>
</head>
<body>
</body>
</html>
함수
<!DOCTYPE html>
<html lang="en">
<head>
<title>함수</title>
<script>
function func1(){
}
func1();
function func2(num){
console.log("func2()호출됨.");
console.log("num:"+num);
}
func2(10);
function func3(){
console.log("func() 호출됨.");
return "hong";
}
let name = func3();
console.log(name);
function func4(name){
console.log("func4 호출됨.");
console.log(`name:${name}`);
}
let welcome = func4('홍길동');
console.log(welcome);
function add(num1, num2){
console.log("입력값:", num1, num2);
console.log("출력값:",num1 + num2);
}
let input_num = add(10,20);
console.log(input_num);
function max(num1, num2){
console.log("입력값:", num1, num2);
if(num1 > num2){
console.log("출력값:", num1);
}
else if(num2 > num1){
console.log("출력값:", num2);
}
}
let max_num = max(10,20);
console.log(max_num);
function vendingMachine(input_value){
if(input_value==1000){
return "콜라";
}
else if(input_value==2000){
return "이온음료";
}
else if(input_value==3000){
return "햄버거";
}
}
console.log(vendingMachine(1000));
console.log(vendingMachine(2000));
console.log(vendingMachine(3000));
</script>
</head>
<body>
</body>
</html>