2016-07-27 2 views
1

출력은 "화요일 7월 정의, 정의되지 않은 26, 2016 화요일"여기 출력이 어디로가는 지 "정의되지 않은"용어가있는 이유는 무엇입니까?

내 코드는 "화요일, 7월 26, 2016"과 같이 한 다음 "즐길"하지만 그 대신, 그것은 처럼 보이는한다 : var today = new Date();

function getMonth1(today) { 
    if(today.getMonth() == 0) { 
     console.log("January"); 
    } 
    else if(today.getMonth() == 1) { 
     console.log("February"); 
    } 
    else if(today.getMonth() == 2) { 
     console.log("March"); 
    } 
    else if(today.getMonth() == 3) { 
     console.log("April"); 
    } 
    else if(today.getMonth() == 4) { 
     console.log("May"); 
    } 
    else if(today.getMonth() == 5) { 
     console.log("June"); 
    } 
    else if(today.getMonth() == 6) { 
     console.log("July"); 
    } 
    else if(today.getMonth() == 7) { 
     console.log("August"); 
    } 
    else if(today.getMonth() == 8) { 
     console.log("September"); 
    } 
    else if(today.getMonth() == 9) { 
     console.log("October"); 
    } 
    else if(today.getMonth() == 10) { 
     console.log("November"); 
    } 
    else if(today.getMonth() == 11) { 
     console.log("December"); 
    } 
} 

function getWeekday(today) { 
    if(today.getDay() == 0) { 
     console.log("Sunday"); 
    } 
    else if(today.getDay() == 1) { 
     console.log("Monday"); 
    } 
    else if(today.getDay() == 2) { 
     console.log("Tuesday"); 
    } 
    else if(today.getDay() == 3) { 
     console.log("Wednesday"); 
    } 
    else if(today.getDay() == 4) { 
     console.log("Thursday"); 
    } 
    else if(today.getDay() == 5) { 
     console.log("Friday"); 
    } 
    else if(today.getDay() == 6) { 
     console.log("Saturday"); 
    } 
} 


function getDate1(today) { 
    console.log(getWeekday(today) + "," + getMonth1(today), today.getDate(), ",", today.getFullYear()); 
    if(getWeekday(today) == "Tuesday") { 
     console.log("enjoy"); 
    } 
    } 

getDate1 (오늘);

+4

getMonth1 및 getWeekDay 함수는 정의되지 않은 함수를 반환합니다.이 함수에서'console.log' 대신 값을'return '해야합니다 ... 현재 출력이 3 행 이상인 것을 볼 수 있습니다. 귀하의 질문에 제안 된 –

+0

그래서 당신은 내가 "돌아 가기"getMonth1 및 getWeekday 함수의 "console.log"를 모두 바꾸어야한다고 말하는거야? –

+0

정확히 내가 말한 것입니다. –

답변

0

뿐만 아니라 Jaromanda X가 (Touffy 언급으로, 배열 조회), 당신은 당신의 코드 같은 큰 거래를 간소화 할 수있는 언급으로 값을 반환하는 데 : 그럼 전화를 getDay()를 사용

function getWeekDay(index) { 
    return 
    ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][index]; 
} 

을 :

alert(getWeekDay(new Date().getDay())); 

그런 다음 getMonth()을 사용하여 몇 달 동안 유사한 설정을 호출하십시오.

관련 문제