# 배열 splice

splice(x,y) : x번째 원소 뒤 y개 원소를 꺼낸다.

배열의 splice 메소드를 이용하여 배열의 원소를 랜덤하게 꺼내는 코드를 구현해보자.

설명 : splice 첫번째 인자값인 기준이 되는 인덱스값을 Math.random으로 생성하고 9(배열의 길이)를 곱한다. 우리가 원하는 값은 0부터 8까지의 인덱스 값이므로 floor연산을 통해 버림연산을 해준다. 그리고 한 번 숫자를 뽑을 때마다(i가 1씩 증가할 때마다) 배열의 길이가 1씩 감소하기 때문에, 반복문 안에서 배열의 길이는 9-i가 된다.

```javascript
var body = document.body;//document.body를 통해서 화면(document)에 접근할 수 있다

var 숫자후보 = [1,2,3,4,5,6,7,8,9];
var 숫자배열 = [];

for (var i =0; i <4 ;i += 1) {
    var 뽑은것 = 숫자후보.splice(Math.floor(Math.random() * (9-i)),1);
    숫자배열.unshift(뽑은것);//앞에서부터 넣는다. 4 3 2 1 순서대로 넣음.
}
console.log(숫자배열);
var fform = document.createElement('form');
document.body.append(fform);

var 입력창 = document.createElement('input');
입력창.type = 'number';//이렇게 해줘도 입력창.value는 숫자로 되지 않는다.
fform.append(입력창);//document>body가 아니라 form 하위에 만들어줘야하기 때문에.

var btn = document.createElement('button');
btn.textContent = '입력';//태그 안에 들어가는 글자.
fform.append(btn);

var result = document.createElement('div');
document.body.append();

fform.addEventListener('submit',function(){

});
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://heunnajo.gitbook.io/zerocho-javascript/splice.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
