Files
.github
api-server
client
config
curriculum
challenges
_meta
arabic
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
boo-who.english.md
chunky-monkey.english.md
confirm-the-ending.english.md
convert-celsius-to-fahrenheit.english.md
factorialize-a-number.english.md
falsy-bouncer.english.md
find-the-longest-word-in-a-string.english.md
finders-keepers.english.md
mutations.english.md
repeat-a-string-repeat-a-string.english.md
return-largest-numbers-in-arrays.english.md
reverse-a-string.english.md
slice-and-splice.english.md
title-case-a-sentence.english.md
truncate-a-string.english.md
where-do-i-belong.english.md
basic-data-structures
basic-javascript
debugging
es6
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-information-security-and-quality-assurance
08-coding-interview-prep
09-certificates
portuguese
russian
spanish
formattingConversion
math-challenges
requiresTests
schema
test
.babelrc
.editorconfig
.npmignore
.travis.yml
CHANGELOG.md
LICENSE.md
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.js
gulpfile.js
index.js
lib.js
md-translation.js
package-entry.js
package-lock.json
package.json
utils.js
docs
guide
mock-guide
search-indexing
tools
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.node-inspectorrc
.prettierignore
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
LICENSE.md
README.french.md
README.italian.md
README.korean.md
README.md
SECURITY.md
azure-pipelines.yml
change_volumes_owner.sh
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
lerna.json
libcimp_index_js.patch
package-lock.json
package.json
patch_npm_and_install.sh
sample.env

105 lines
3.9 KiB
Markdown
Raw Normal View History

---
id: a24c1a4622e3c05097f71d67
title: Where do I Belong
isRequired: true
challengeType: 5
---
## Description
<section id='description'>
Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted. The returned value should be a number.
For example, <code>getIndexToIns([1,2,3,4], 1.5)</code> should return <code>1</code> because it is greater than <code>1</code> (index 0), but less than <code>2</code> (index 1).
Likewise, <code>getIndexToIns([20,3,5], 19)</code> should return <code>2</code> because once the array has been sorted it will look like <code>[3,5,20]</code> and <code>19</code> is less than <code>20</code> (index 2) and greater than <code>5</code> (index 1).
Remember to use <a href="http://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514" target="_blank">Read-Search-Ask</a> if you get stuck. Write your own code.
</section>
## Instructions
<section id='instructions'>
</section>
## Tests
<section id='tests'>
```yml
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");
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
function getIndexToIns(arr, num) {
// Find my place in this sorted array.
return num;
}
getIndexToIns([40, 60], 50);
```
</div>
</section>
## Solution
<section id='solution'>
```js
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);
```
</section>