✍️
javascript
  • 자바스크립트 시작하기
  • 자바스크립트 기초 문법
  • 변수
  • 배열
  • 객체
  • 연산자
  • 조건문
    • if 문
    • if~else
    • 다중 if문
    • 중첩 if문
    • switch문
    • 삼항 연산자
  • 반복문
    • while문
    • do while문
    • for문
    • 중첩 for문
    • break문
    • continue문
  • 함수
    • 선언적 함수
    • 익명함수
    • 매개변수가 있는 함수
    • Arguments 함수
    • 리턴값이 있는 함수
    • 재귀 함수
    • 콜백 함수
    • 내부함수(스코프)
    • 객체 생성자 함수
    • 프로토타입 함수
    • 화살표 함수
    • 클래스
    • Promise
    • 함수 정리
    • 템플릿 리터럴
  • 내장 객체
    • String 객체
      • split()
      • join()
    • Number 객체
    • Date 객체
    • Array 객체
    • Math 객체
    • 정규표현 객체
  • 브라우저 객체
    • window 객체
    • navigator 객체
    • screen 객체
    • history 객체
    • location 객체
  • 문서 객체
  • 이벤트
Powered by GitBook
On this page
  1. 함수

리턴값이 있는 함수

return 문은 함수에서 결괏값을 반환할 때 사용합니다.

PreviousArguments 함수Next재귀 함수

Last updated 4 years ago

Was this helpful?

CtrlK
  • 리턴값이 있는 함수
  • 매개변수 + 리턴값
  • 리턴값이 있는 함수(결과)
  • 리턴값이 있는 함수(종)
  • 평균 구하기
  • 배열 + 함수 + 평균
  • 이미지 슬라이드

Was this helpful?

리턴값이 있는 함수

function(함수 이름){ //실행 코드 return 리턴값; } let 변수 = 함수명( ); //함수 호출

function func4(){
    let str = "함수를 실행하였습니다.";
    return str;  //return은 결과
}
let value = func4();
document.write(value);

매개변수 + 리턴값

function func5(num1, num2){
    return num1 + num2;
}
let resulf = func5(100,200);
document.write(resulf);   // 300

리턴값이 있는 함수(결과)

function func5(){
    let str = "함수가 출력되었습니다5.<br>";
    return str;
}
let value = func5();
document.write(value);

// 함수가 출력되었습니다5.

리턴값이 있는 함수(종)

function func6(){
    document.write("함수가 출력되었습니다6.");
    return;
    document.write("함수가 출력되었습니다7.");
}
func6();

// 함수가 출력되었습니다6.
// 중간에 return 값이 실행하게되면 그뒤에 오는 실행문 무시

평균 구하기

function testAvg(arrData){
    let sum = 0;
    for(let i = 0; i < arrData.length; i++){
        sum += Number(prompt(arrData[i] + " 점수는?","0"));
    }
    let avg = sum / arrData.length;
    return avg;
}

let arrSubject = ["국어","수학","영어","과학"];
let resulf = testAvg(arrSubject);

document.write(resulf);

배열 + 함수 + 평균

let arr1 = [100,200,300,400,500];

function avg(){
    //배열안에 있는 데이터값을 출력
    for(let i = 0; i<arr1.length; i++){
        document.write(arr1[i]);  // 100,200,300,400,500
    }
    document.write("<br>");

    //배열안에 있는 데이터값의 합을 출력
    let sum = 0;
    for(let i =0; i<arr1.length; i++){
        sum = sum + arr1[i];
    }
    document.write(sum);  // 1500
    document.write("<br>");

    //배열안에 있는 데이터 값의 평균값(sum2)을 출력 for
    let sum2 = 0;
    for(let i =0; i<arr1.length; i++){
        sum2 += arr1[i];
    }
    let avg2 = sum2 / arr1.length;
    document.write(avg2);  // 300
    document.write("<br>");  
}
avg();

이미지 슬라이드

let prev = document.querySelector(".prev");
let next = document.querySelector(".next");
let image = document.getElementById("img");
let num = 1

prev.addEventListener("click",function(){
    if( num == 1){
        alert("처음 이미지입니다.");
        return;
    }
    num--;
    image.setAttribute("src","img/pic_"+ num + ".jpg");
    console.log(num);
});
next.addEventListener("click",function(){
    if( num == 8){
        alert("마지막 이미지입니다.");
        return;
    }
    num++;
    image.setAttribute("src","img/pic_"+ num + ".jpg");
    console.log(num);
});
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    
</head>
<body>
    <div id="galleryZone">
        <p><img id="photo" src="img/pic_1.jpg" alt="이미지1" style="width: 1000px;"></p>
        <div>
            <button onclick="gallery(0)">이전</button>
            <button onclick="gallery(1)">다음</button>
        </div>
    </div>

    <script>
        let num = 1;
        function gallery(direct){
            if(direct){
                if(num == 8) return;
                num++;
            } else {
                if(num == 1) return;
                num--;
            }

            let imgTag = document.getElementById("photo");
            imgTag.setAttribute("src","img/pic_"+num+".jpg");
        }
    </script>
</body>
</html>