Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Interview-Preparation-Kit/Sorting/mark-and-toys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @title Mark and Toys
* @difficulty Easy
* @link https://www.hackerrank.com/challenges/mark-and-toys/problem
*/

const ascendingCompare = (a, b) => a - b;

const maximumToys = (prices, budget) => {
const sorted = prices.sort(ascendingCompare);
let sumPrice = 0;

for (const idx in sorted) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array 에서 in  을 사용한 이유가 있나요.

Copy link
Owner

@yeonjuan yeonjuan Aug 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-a-bad-idea
array 에 대한 in 사용은 오용을 이르킬 가능성이 많습니다...
이를 대체할수 있는 함수가 많습니다 every, some 과 같은..

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in 외에 인덱스값을 사용할 수 있는 방법이 있나요?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forEach가 있습니다.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그냥 for 문도 있씁니다.

if (sorted.isOwnProperty(idx)) {
sumPrice += sorted[idx];
if (sumPrice > budget) {
return idx;
}
}
}

return prices.length;
}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ Generates the README.md.
| --- | --- | --- |
| Easy | [2D Array - DS](https://www.hackerrank.com/challenges/2d-array/problem) | [Solution](./Interview-Preparation-Kit/Arrays/2d-array-ds.js)|
| Easy | [Left Rotation](https://www.hackerrank.com/challenges/ctci-array-left-rotation/problem) | [Solution](./Interview-Preparation-Kit/Arrays/left-rotation.js)|
#### Sorting
| Difficulty | Problem | Solution |
| --- | --- | --- |
| Easy | [Mark and Toys](https://www.hackerrank.com/challenges/mark-and-toys/problem) | [Solution](./Interview-Preparation-Kit/Sorting/mark-and-toys.js)|
#### Strings
| Difficulty | Problem | Solution |
| --- | --- | --- |
#### Warm-up-Challenges
| Difficulty | Problem | Solution |
| --- | --- | --- |
Expand Down