▶️ 숫자야구 순서도 그리기

▶️ random 함수

- Math.random()은 0이상 1 미만의 자연수를 랜덤으로 생성한다.
- 함수 뒤에 연산자를 붙여서 범위를 설정할 수 있다.
- Math.floor()는 내림, Math.ceil()은 올림, Math.round()는 반올림을 뜻한다.
Math.random()은 진짜 암호화는 아니다?
- Math.random()은 암호학적으로 완전한 무작위가 아니다. 따라서 보안과 관련된 작업(비밀번호를 생성한다거나)을 할 때는 Math.random()으로 생성된 수를 사용하면 위험하다.
- 진짜 암호화를 하고 싶다면
window.crypto.getRandomValues()
를 사용하도록 하자.
<body>
<form id="form">
<input type="text" id="input">
<button>확인</button>
</form>
<div id="logs"></div>
<script>
$form.addEventListener('submit', (e)=>{
e.preventDefault()
console.log('submit');
});
</script>
</body>
- 폼은 제출하면 페이지가 새로고침 되는게 기본적으로 세팅돼있다. 새로고침하면 도메인 뒤에 ?가 붙은걸 확인 할 수 있고, 저장돼있던 데이터가 모두 초기화된다.
- 이를 막기 위해 타겟에 기본 동작설정을 방지하는 e.preventDefault() 함수를 추가해준다.
▶️ new Set(array)
const code = '1231'
new Set(code)
- new Set() 예약어는 중복을 허용하지 않는 객체를 생성할때 쓴다.
▶️ join과 spilt
const code = [1,2,3,4]
code.join();
code.join('');
const myMail = wrasf175@naver.com
myMail.split('@')
myMail.split('@')[1].spilt['.']
- join은 배열을 separator를 기준으로 쪼개 문자열로 만들고, split은 separator를 기준으로 문자열을 쪼개 배열을 만드는 자바스크립트의 내장 메서드이다.
▶️ indexOf()
stringValue.indexOf(searchValue,[fromIndex]);
var stringValue = '자스공부 - 자바스크립트 스터디';
alert(stringValue.indexOf('자스'));
alert(stringValue.indexOf('공부'));
alert(stringValue.indexOf('리액트'));
alert(stringValue.indexOf('자바스크립트',0));
alert(stringValue.indexOf('자바스크립트',2));
alert(stringValue.indexOf('자바스크립트',10));
참고
- indexOf()는 문자열 내에서 특정한 문자열의 index 값을 리턴한다.
- stringValue에서 특정한 문자열의 위치(index)를 반환한다.
탐색하려는 문자열이 존재하지 않는다면 -1을 반환한다.
▶️ forEach와 map
const array = [1, 3, 5, 7];
array.forEach((number, index) => {
console.log(number, index);
});
- forEach는 반복문 효과를 내는 배열의 메서드이다. 인수로 함수를 넣고, 이 함수가 각각의 배열 요소들에 순서대로 적용되는 구조이다.
const array = [1, 3, 5, 7];
const newArray = array.map((number, index) => {
console.log(number, index);
return number + 1;
});
console.log(array);
console.log(newArray);
- map도 반복문 역할을 하지만, 반환값이 있다는 점에서 forEach와 다르다. map은 기존 배열의 요소를 일대일로 다른 값으로 바꿔준다.
- 단 이는 기존 배열의 값이 바뀌는 것이 아니라 새로운 배열을 만드는 것이다.