내장기능 겁나 많다. 실무 나가서도 계속 들춰볼 듯 자바스크립트 똑똑하네~
const email = "cdabomi@nate.com";
const p1 = email.substring(0,email.indexOf("@"));
const p2 = email.substring(email.indexOf("@")+1);
console.log(p1);
console.log(p2);
ssn = '020517-3**'
const ssn = '020517-3******';
const date = new Date();
const now_year = date.getFullYear();
let yy = parseInt(ssn.substring(0,2));
let mm = parseInt(ssn.substring(2,4));
let dd = parseInt(ssn.substring(4,6));
let gen = parseInt(ssn.substring(7,8));
yy = (gen > 2) ? yy+2000 : yy+1900;
const age = now_year - yy + 1;
const sex = (gen % 2) ? "남자" : "여자";
console.log("%d년 %d월 %d일에 태어난 %d세 %s입니다.",yy,mm,dd,age,sex);
str = "수업시간에 배운것은 수업시간에 다 이해하고 넘어가야지 수업시간에 놓치면 따라오기 힘들다."
str = "수업시간에 배운것은 수업시간에 다 이해하고 넘어가야지 수업시간에 놓치면 따라오기 힘들다."
word = "수업시간";
flen = word.length;
find = true;
count = 0;
while(find) {
console.log(str);
p = str.indexOf(word);
find = p > -1;
if (find) {
count++;
str = str.substring(p+flen);
}
}
console.log(count);
중복되지 않는 숫자를 생성하기 위해 for문 안에서 무한반복을 위한 while문을 수행해야 합니다.
function random(n1, n2) {return parseInt(Math.random() * (n2 - n1 + 1)) + n1;}
function random(n1,n2) {
return parseInt(Math.random() * (n2-n1+1)) + n1;
}
const lotto = [];
console.log(lotto);
for (let i=0; i<6; i++) {
while(true) {
const rnd = random(1,45);
if (!lotto.includes(rnd)) {
lotto.push(rnd);
break;
}
}
}
console.log(lotto);
function random(n1,n2) {
return parseInt(Math.random() * (n2-n1+1)) + n1;
}
// 1~45 사이의 범위의 1씩 증가하는 원소가 저장되어 있는 배열 balls를 생성
const balls = new Array(45);
for (let i=0; i<balls.length; i++) {
balls[i] = i+1;
}
// 6개의 빈 칸을 갖는 배열 lotto를 생성
const lotto = new Array(6);
for (let i=0; i<lotto.length; i++) {
console.log(balls);
// balls의 index범위 안에서 임의의 위치를 선정
const rnd = random(0,balls.length-1);
// ball 배열에서 임의의 원소 하나를 추출하려 lotto 배열에 채워넣기
lotto[i] = balls[rnd];
// rnd번째 위치에서 하나의 데이터를 잘라냄
balls.splice(rnd,1);
}
console.log(lotto);
1) 마라톤 경기에 참여한 선수의 수는 1명 이상 100,000명 이하입니다.
2) completion의 길이는 participant의 길이보다 1 작습니다.
3) 참가자의 이름은 1개 이상 20개 이하의 알파벳 소문자로 이루어져 있습니다.
4) 참가자 중에는 동명이인이 있을 수 없습니다.
function solution(participant,completion) {
var answer = '';
// participant 원소 중에서 completion에 속하지 않으면 그 순간 반복 중단
/* 1) for문을 사용해 탐색하다가 break 사용
for (let i=0; i<participant.length; i++) {
const p = participant[i];
- i번째 원소가 completion에 들어있다면?
if (!completion.includes(p)) {
answer = p;
break;
}
} */
/* 2) 배열의 some함수 사용
participant.some((v,i) => {
if(!completion.includes(v)) {
answer = v;
return true;
}
}); */
// 3) find 함수 사용
answer = participant.find((v, i) => {
if (!completion.includes(v)) {
return true;
}
});
return answer;
}
console.log(solution(["leo", "kiki", "eden"],
["eden", "kiki"]));
console.log(solution(["marina", "josipa", "nikola", "vinko", "filipa"],
["josipa", "filipa", "marina", "nikola"]));
console.log(solution(["mislav", "stanko", "steave", "ana"],
["stanko", "ana", "mislav"]));