let a = 20;
let b = 40;
if (a < b) {
document.write("a < b");
}
if (a < b) {
document.write("a < b");
} else {
document.write("a > b");
}
let a = 20;
let b = 40;
let c = 60;
if (a > b) {
document.write("a > b");
} else if (b > c) {
document.write("b > c");
} else if (a < c) {
document.write("a < c");
} else if (b < c) {
document.write("b < c");
} else {
document.write("모든 조건을 만족하지 않는다.");
}
let a = 20;
let b = 40;
if (a !== b) {
if (a > b) {
document.write("a > b")
} else {
document.write("a < b")
}
} else {
document.write("a === b")
}
let num = 0;
while (num < 10) {
document.writeln(num);
num++;
}
let i = 12;
do {
document.write(i);
i++
} while (i < 10);
구구단 출력하기 예시 (중괄호 안넣은것때문에 계속 틀렸었음)
function timesTable(n) {
for(let i = 1; i < 10; i++) {
document.write(n + "x" + i + "=" + (n*i) + '<br>');
}
}
timesTable(2); // 2단 출력
document.write(Math.floor(Math.random() * 6) + 1);
Math.random : 0과 1사이의 임의의 숫자 출력하기
Math.floor : 숫자 내림하기
// 매개변수 n이 소수라면 true를, 소수가 아니라면 false를 반환합니다.
function isPrime(n){
let divisor = 2;
if (n === 1) {
return false; // 1은 소수가 아니기때문에 false출력시켜야
}
while (n > divisor) { //만약 나눌 숫자(n)가 4면 2,3까지 증가
if (n % divisor === 0) { // 나머지가 0이면(=나누어떨어지면)
return false;
} else {
divisor++;
}
} return true
}
for(var i = 1; i <= 10; i++) {
document.writeln(i, isPrime(i));
}
function reverse(str){
var reverStr = ""; // 처음에 공백으로 시작
for (let i = str.length -1; i >= 0; i--) { // 문자열의 길이에서 -1해야 문자의 인자값(?)이 나오기때문
document.write(reverStr + str.charAt(i));
}
return reverStr;
}
document.write(reverse("Nice to meet you"));
const readline = require("readline");
readline 모듈을 이용해 입출력을 위한 인터페이스 객체 생성
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
생성한 rl변수는 아래와 같이 이용
rl.on("line", (line) => {
console.log(line); // 한 줄씩 입력받은 후 실행할 코드, 입력된 값은 line에 저장
rl.close(); // close가 없으면 입력을 무한히 받는다.
});
rl.on('close', () => {
// 입력이 끝난 후 실행할 코드
});
var fruit = [
'apple',
'banana',
'grape',
'orange'
];
// 화살표 함수를 이용해 변경하기
console.log(fruit.map(e => e.length));
//변경 전
//console.log(fruit.map(function(e) {
// return e.length;
//}));