03-2:
const input =prompt('input number','')
const number = Number(input)
const result = (number >= 0) ? 'number is greater than 0':'number is less than 0'
alert(result)
// 불 표현식 ? 참일 때 결과: 거짓일 때 결과
//number= 7, 7 >= 0 result= true , 'number is greater than 0' 출력
//number= -5, -5 >= 0 result= false, 'number is less than 0' 출력
true || console.log ('good') // 좌변이 참이면 우변 실행 하지 않는다 > true 출력
false || console.log('good') // 좌변이 거짓이면 우변 실행 > good 출력
false || 불 표현식이 거짓일 때 실행할 문장
true && console.log ('good') // 양변이 모두 참이여야 true > true
false &&
prompt 가 실행이 안된다 그래서 구글링을 했더니 prompt-sync 패키지를 설정하고 prompt 를 require 하라고 했다.
const prompt=require("prompt-sync")({sigint:true});
var name = prompt("What's your name");
console.log("hello"+name+"!");
레퍼런스: https://www.geeksforgeeks.org/javascript-is-showing-reference-error-prompt-is-not-defined/
근데도 크롬 브라우저에서 require is not defined 에러 메세지가 떴다.
나는 지금 브라우저에서 prompt를 실행하려고 하는거니까 require 코드가 필요 없었던 것이다. 인터넷에서 찾았던 코드를 삭제하고 const number = prompt(’message’,’ ‘) 이렇게 다시 변경했더니 브라우저에서 prompt가 잘 실행됬고 하기 조건문에 맞게 알럿이 잘 떴다.
const number = prompt('enter a number', '')
if (number > 0) {
alert('positive number')
} else {
if (number <0) {
alert ('negative number')
}
}
인터넷에서 찾은 오류 해결법을 적용하기전에 내가 코드를 실행하는 목적은 무엇인지, 내 코드는 어디서 실행되는지 먼저 확인해 보고 적용해보는 습관이 필요하다.
03-1 확인문제 6번 : 월을 입력하면 계절을 구분하는 프로그램
const month = prompt('enter the number of a month', '')
if (month <0) {
alert ('not a vailid entry, enter postive number')
} else {
if ( month < 6) {
alert('spring')
} else if ( month <9 ) {
alert ('summer')
} else if ( month < 11){
alert ('fall')
} else{
alert ('winter')
}
}
오류 해결
if ( month <= 3) {
if (month < 6){
alert ('spring')
}
}
if (month <= 6){
if (month <9){
alert('summer')
}
}
if (month <= 9){
if (month <12){
alert('fall')
}
} else {
alert('winter')
}
const month = prompt('enter the number of a month', '')
if (month <0) {
alert ('not a vailid entry, enter postive number')
} else {
if ( 3 <= month && month < 6) {
alert('spring')
} else if ( 6 <= month && month<9 ) {
alert ('summer')
} else if ( 9 <= month && month < 12){
alert ('fall')
} else {
alert ('winter')
}
}