0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

javascript関数ドリル 初級編dropRight関数の実相のアウトプット

Posted at

##dropRight関数について
   ↓
https://js-drills.com/blog/dropright/

##dropRight関数の取り組む前の状態
前のdropメソットの時とほとんど同じなんじゃないかと思っていた

dropRight関数に取り組んだ後の状態
array.length-sizeという発想がありませんでした

##dropRight関数の実装コード(答えを見る前)

function dropRight(array, size = 1) {
  if (size === 0) {
    return [...array];
  }
  if (array.length <= size) {
    return [];
  }

  const droppdedArray = [];
  for(let i = 0; i < array.length; i++) {
    droppdedArray.push( array[i] );
  }

  return droppdedArray;
}

const numbers = [1, 2, 3];

const result1 = dropRight(numbers);
console.log(result1, numbers);
// // => [1, 2]

// dropRight(numbers, 2);
const result2 = dropRight(numbers, 2);
console.log(result2, numbers);
// // => [1]

const result3 = dropRight(numbers, 5);
console.log(result3, numbers);
// => []

const result4 = dropRight(numbers, 0);
console.log(result4, numbers);
// => [1, 2, 3]

zipObject関数の実装コード(答えを見た後)

function dropRight(array, size = 1) {
  if (size === 0) {
    return [...array];
  }
  if (array.length <= size) {
    return [];
  }

  const droppdedArray = [];
  for(let i = 0; i < array.length - size; i++) {
    droppdedArray.push( array[i] );
  }

  return droppdedArray;
}

const numbers = [1, 2, 3];

const result1 = dropRight(numbers);
console.log(result1, numbers);
// // => [1, 2]

// dropRight(numbers, 2);
const result2 = dropRight(numbers, 2);
console.log(result2, numbers);
// // => [1]

const result3 = dropRight(numbers, 5);
console.log(result3, numbers);
// => []

const result4 = dropRight(numbers, 0);
console.log(result4, numbers);
// => [1, 2, 3]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?