본문 바로가기

Python_WEB/JavaScript

[freecodecamp]JavaScript 정렬

반응형

컴퓨터 과학에서 큐는 항목이 순서대로 유지되는 추상 데이터 구조입니다.

새 항목은 대기열 뒤쪽에 추가 할 수 있으며 이전 항목은 대기열 앞쪽에서 제거합니다.

배열 (arr)과 숫자 (항목)를 인수로 취하는 함수 nextInLine을 작성하십시오.

배열 끝에 숫자를 더한 다음 배열의 첫 번째 요소를 제거합니다.

nextInLine 함수는 제거 된 요소를 반환해야합니다.

 

Q>

1. nextInLine ([], 5)는 숫자를 반환해야합니다.

2. nextInLine ([], 1)은 1을 반환해야합니다.

3. nextInLine ([2], 1)은 2를 반환해야합니다.

4. nextInLine ([5,6,7,8,9], 1)은 5를 반환해야합니다.

5. nextInLine (testArr, 10) 이후 testArr [4]는 10이어야합니다.

 

A>

function nextInLine(arr, item) {
    // Only change code below this line
    arr.push(item);
    var removed = arr.shift();
    return removed;
    // Only change code above this line
}

// Setup
var testArr = [1, 2, 3, 4, 5];

// Display code
console.log('Before: ' + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log('After: ' + JSON.stringify(testArr));

 

https://www.freecodecamp.org/

 

freeCodeCamp.org

Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.

www.freecodecamp.org

 

반응형