2020-06-30 14:21:26 +05:30

4.3 KiB
Raw Blame History

id, title, isRequired, challengeType, forumTopicId, localeTitle
id title isRequired challengeType forumTopicId localeTitle
a24c1a4622e3c05097f71d67 Where do I Belong true 5 16094 Где я живу

Description

Возвратите наименьший индекс, по которому значение (второй аргумент) должно быть вставлено в массив (первый аргумент) после его сортировки. Возвращаемое значение должно быть числом. Например, getIndexToIns([1,2,3,4], 1.5) должен возвращать 1 потому что он больше 1 (индекс 0), но меньше 2 (индекс 1). Аналогично, getIndexToIns([20,3,5], 19) должен вернуть 2 потому что, как только массив будет отсортирован, он будет выглядеть как [3,5,20] а 19 меньше 20 (индекс 2) и больше 5 ( индекс 1). Не забудьте использовать Read-Search-Ask, если вы застряли. Напишите свой собственный код.

Instructions

Tests

tests:
  - text: <code>getIndexToIns([10, 20, 30, 40, 50], 35)</code> should return <code>3</code>.
    testString: assert(getIndexToIns([10, 20, 30, 40, 50], 35) === 3);
  - text: <code>getIndexToIns([10, 20, 30, 40, 50], 35)</code> should return a number.
    testString: assert(typeof(getIndexToIns([10, 20, 30, 40, 50], 35)) === "number");
  - text: <code>getIndexToIns([10, 20, 30, 40, 50], 30)</code> should return <code>2</code>.
    testString: assert(getIndexToIns([10, 20, 30, 40, 50], 30) === 2);
  - text: <code>getIndexToIns([10, 20, 30, 40, 50], 30)</code> should return a number.
    testString: assert(typeof(getIndexToIns([10, 20, 30, 40, 50], 30)) === "number");
  - text: <code>getIndexToIns([40, 60], 50)</code> should return <code>1</code>.
    testString: assert(getIndexToIns([40, 60], 50) === 1);
  - text: <code>getIndexToIns([40, 60], 50)</code> should return a number.
    testString: assert(typeof(getIndexToIns([40, 60], 50)) === "number");
  - text: <code>getIndexToIns([3, 10, 5], 3)</code> should return <code>0</code>.
    testString: assert(getIndexToIns([3, 10, 5], 3) === 0);
  - text: <code>getIndexToIns([3, 10, 5], 3)</code> should return a number.
    testString: assert(typeof(getIndexToIns([3, 10, 5], 3)) === "number");
  - text: <code>getIndexToIns([5, 3, 20, 3], 5)</code> should return <code>2</code>.
    testString: assert(getIndexToIns([5, 3, 20, 3], 5) === 2);
  - text: <code>getIndexToIns([5, 3, 20, 3], 5)</code> should return a number.
    testString: assert(typeof(getIndexToIns([5, 3, 20, 3], 5)) === "number");
  - text: <code>getIndexToIns([2, 20, 10], 19)</code> should return <code>2</code>.
    testString: assert(getIndexToIns([2, 20, 10], 19) === 2);
  - text: <code>getIndexToIns([2, 20, 10], 19)</code> should return a number.
    testString: assert(typeof(getIndexToIns([2, 20, 10], 19)) === "number");
  - text: <code>getIndexToIns([2, 5, 10], 15)</code> should return <code>3</code>.
    testString: assert(getIndexToIns([2, 5, 10], 15) === 3);
  - text: <code>getIndexToIns([2, 5, 10], 15)</code> should return a number.
    testString: assert(typeof(getIndexToIns([2, 5, 10], 15)) === "number");
  - text: <code>getIndexToIns([], 1)</code> should return <code>0</code>.
    testString: assert(getIndexToIns([], 1) === 0);
  - text: <code>getIndexToIns([], 1)</code> should return a number.
    testString: assert(typeof(getIndexToIns([], 1)) === "number");

Challenge Seed

function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  return num;
}

getIndexToIns([40, 60], 50);

Solution

function getIndexToIns(arr, num) {
  arr = arr.sort((a, b) => a - b);

  for (let i = 0; i < arr.length; i++) {
    if (arr[i] >= num) {
      return i;
    }
  }

  return arr.length;
}

getIndexToIns([40, 60], 50);