chore: import upstream snapshot with attribution
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
This commit is contained in:
+100
@@ -0,0 +1,100 @@
|
||||
---
|
||||
id: a0b5010f579e69b815e7c5d6
|
||||
title: Search and Replace
|
||||
challengeType: 1
|
||||
forumTopicId: 16045
|
||||
dashedName: search-and-replace
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Perform a search and replace on the sentence using the arguments provided and return the new sentence.
|
||||
|
||||
First argument is the sentence to perform the search and replace on.
|
||||
|
||||
Second argument is the word that you will be replacing (before).
|
||||
|
||||
Third argument is what you will be replacing the second argument with (after).
|
||||
|
||||
**Note:** Preserve the case of the first character in the original word when you are replacing it. For example if you mean to replace the word `Book` with the word `dog`, it should be replaced as `Dog`
|
||||
|
||||
# --hints--
|
||||
|
||||
`myReplace("Let us go to the store", "store", "mall")` should return the string `Let us go to the mall`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
myReplace('Let us go to the store', 'store', 'mall'),
|
||||
'Let us go to the mall'
|
||||
);
|
||||
```
|
||||
|
||||
`myReplace("He is Sleeping on the couch", "Sleeping", "sitting")` should return the string `He is Sitting on the couch`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
myReplace('He is Sleeping on the couch', 'Sleeping', 'sitting'),
|
||||
'He is Sitting on the couch'
|
||||
);
|
||||
```
|
||||
|
||||
`myReplace("I think we should look up there", "up", "Down")` should return the string `I think we should look down there`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
myReplace('I think we should look up there', 'up', 'Down'),
|
||||
'I think we should look down there'
|
||||
);
|
||||
```
|
||||
|
||||
`myReplace("This has a spellngi error", "spellngi", "spelling")` should return the string `This has a spelling error`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
myReplace('This has a spellngi error', 'spellngi', 'spelling'),
|
||||
'This has a spelling error'
|
||||
);
|
||||
```
|
||||
|
||||
`myReplace("His name is Tom", "Tom", "john")` should return the string `His name is John`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
myReplace('His name is Tom', 'Tom', 'john'),
|
||||
'His name is John'
|
||||
);
|
||||
```
|
||||
|
||||
`myReplace("Let us get back to more Coding", "Coding", "algorithms")` should return the string `Let us get back to more Algorithms`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
myReplace('Let us get back to more Coding', 'Coding', 'algorithms'),
|
||||
'Let us get back to more Algorithms'
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function myReplace(str, before, after) {
|
||||
return str;
|
||||
}
|
||||
|
||||
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function myReplace(str, before, after) {
|
||||
if (before.charAt(0) === before.charAt(0).toUpperCase()) {
|
||||
after = after.charAt(0).toUpperCase() + after.substring(1);
|
||||
} else {
|
||||
after = after.charAt(0).toLowerCase() + after.substring(1);
|
||||
}
|
||||
return str.replace(before, after);
|
||||
}
|
||||
```
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
---
|
||||
id: a103376db3ba46b2d50db289
|
||||
title: Spinal Tap Case
|
||||
challengeType: 1
|
||||
forumTopicId: 16078
|
||||
dashedName: spinal-tap-case
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Convert a string to spinal case. Spinal case is all-lowercase-words-joined-by-dashes.
|
||||
|
||||
# --hints--
|
||||
|
||||
`spinalCase("This Is Spinal Tap")` should return the string `this-is-spinal-tap`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(spinalCase('This Is Spinal Tap'), 'this-is-spinal-tap');
|
||||
```
|
||||
|
||||
`spinalCase("thisIsSpinalTap")` should return the string `this-is-spinal-tap`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(spinalCase('thisIsSpinalTap'), 'this-is-spinal-tap');
|
||||
```
|
||||
|
||||
`spinalCase("The_Andy_Griffith_Show")` should return the string `the-andy-griffith-show`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(
|
||||
spinalCase('The_Andy_Griffith_Show'),
|
||||
'the-andy-griffith-show'
|
||||
);
|
||||
```
|
||||
|
||||
`spinalCase("Teletubbies say Eh-oh")` should return the string `teletubbies-say-eh-oh`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(
|
||||
spinalCase('Teletubbies say Eh-oh'),
|
||||
'teletubbies-say-eh-oh'
|
||||
);
|
||||
```
|
||||
|
||||
`spinalCase("AllThe-small Things")` should return the string `all-the-small-things`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(spinalCase('AllThe-small Things'), 'all-the-small-things');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function spinalCase(str) {
|
||||
return str;
|
||||
}
|
||||
|
||||
spinalCase('This Is Spinal Tap');
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function spinalCase(str) {
|
||||
str = str.replace(/([a-z](?=[A-Z]))/g, '$1 ');
|
||||
return str.toLowerCase().replace(/\ |\_/g, '-');
|
||||
}
|
||||
```
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
---
|
||||
id: a105e963526e7de52b219be9
|
||||
title: Sorted Union
|
||||
challengeType: 1
|
||||
forumTopicId: 16077
|
||||
dashedName: sorted-union
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Write a function that takes two or more arrays and returns a new array of unique values in the order of the original provided arrays.
|
||||
|
||||
In other words, all values present from all arrays should be included in their original order, but with no duplicates in the final array.
|
||||
|
||||
The unique numbers should be sorted by their original order, but the final array should not be sorted in numerical order.
|
||||
|
||||
Check the assertion tests for examples.
|
||||
|
||||
# --hints--
|
||||
|
||||
`uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1])` should return `[1, 3, 2, 5, 4]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]), [1, 3, 2, 5, 4]);
|
||||
```
|
||||
|
||||
`uniteUnique([1, 2, 3], [5, 2, 1])` should return `[1, 2, 3, 5]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(uniteUnique([1, 2, 3], [5, 2, 1]), [1, 2, 3, 5]);
|
||||
```
|
||||
|
||||
`uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8])` should return `[1, 2, 3, 5, 4, 6, 7, 8]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8]), [
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
5,
|
||||
4,
|
||||
6,
|
||||
7,
|
||||
8
|
||||
]);
|
||||
```
|
||||
|
||||
`uniteUnique([1, 3, 2], [5, 4], [5, 6])` should return `[1, 3, 2, 5, 4, 6]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(uniteUnique([1, 3, 2], [5, 4], [5, 6]), [1, 3, 2, 5, 4, 6]);
|
||||
```
|
||||
|
||||
`uniteUnique([1, 3, 2, 3], [5, 2, 1, 4], [2, 1])` should return `[1, 3, 2, 5, 4]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(uniteUnique([1, 3, 2, 3], [5, 2, 1, 4], [2, 1]), [1, 3, 2, 5, 4]);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function uniteUnique(arr) {
|
||||
return arr;
|
||||
}
|
||||
|
||||
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function uniteUnique(arr) {
|
||||
return [].slice.call(arguments).reduce(function(a, b) {
|
||||
return [].concat(
|
||||
a,
|
||||
b.filter(function(e, currentIndex) {
|
||||
return b.indexOf(e) === currentIndex && a.indexOf(e) === -1;
|
||||
}));
|
||||
}, []);
|
||||
}
|
||||
```
|
||||
+158
@@ -0,0 +1,158 @@
|
||||
---
|
||||
id: a10d2431ad0c6a099a4b8b52
|
||||
title: Everything Be True
|
||||
challengeType: 1
|
||||
forumTopicId: 16011
|
||||
dashedName: everything-be-true
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Check if the predicate (second argument) is <dfn>truthy</dfn> on all elements of a collection (first argument).
|
||||
|
||||
In other words, you are given an array collection of objects. The predicate `pre` will be an object property and you need to return `true` if its value is `truthy`. Otherwise, return `false`.
|
||||
|
||||
In JavaScript, `truthy` values are values that translate to `true` when evaluated in a Boolean context.
|
||||
|
||||
Remember, you can access object properties through either dot notation or `[]` notation.
|
||||
|
||||
# --hints--
|
||||
|
||||
`truthCheck([{name: "Quincy", role: "Founder", isBot: false}, {name: "Naomi", role: "", isBot: false}, {name: "Camperbot", role: "Bot", isBot: true}], "isBot")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(truthCheck(
|
||||
[
|
||||
{ name: "Quincy", role: "Founder", isBot: false },
|
||||
{ name: "Naomi", role: "", isBot: false },
|
||||
{ name: "Camperbot", role: "Bot", isBot: true }
|
||||
],
|
||||
"isBot"), false);
|
||||
```
|
||||
|
||||
`truthCheck([{name: "Quincy", role: "Founder", isBot: false}, {name: "Naomi", role: "", isBot: false}, {name: "Camperbot", role: "Bot", isBot: true}], "name")` should return `true`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(truthCheck(
|
||||
[
|
||||
{ name: "Quincy", role: "Founder", isBot: false },
|
||||
{ name: "Naomi", role: "", isBot: false },
|
||||
{ name: "Camperbot", role: "Bot", isBot: true }
|
||||
],
|
||||
"name"), true);
|
||||
```
|
||||
|
||||
`truthCheck([{name: "Quincy", role: "Founder", isBot: false}, {name: "Naomi", role: "", isBot: false}, {name: "Camperbot", role: "Bot", isBot: true}], "role")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(truthCheck(
|
||||
[
|
||||
{ name: "Quincy", role: "Founder", isBot: false },
|
||||
{ name: "Naomi", role: "", isBot: false },
|
||||
{ name: "Camperbot", role: "Bot", isBot: true }
|
||||
],
|
||||
"role"), false);
|
||||
```
|
||||
|
||||
`truthCheck([{name: "Pikachu", number: 25, caught: 3}, {name: "Togepi", number: 175, caught: 1}], "number")` should return `true`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(truthCheck(
|
||||
[
|
||||
{ name: "Pikachu", number: 25, caught: 3 },
|
||||
{ name: "Togepi", number: 175, caught: 1 },
|
||||
],
|
||||
"number"), true);
|
||||
```
|
||||
|
||||
`truthCheck([{name: "Pikachu", number: 25, caught: 3}, {name: "Togepi", number: 175, caught: 1}, {name: "MissingNo", number: NaN, caught: 0}], "caught")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(truthCheck(
|
||||
[
|
||||
{ name: "Pikachu", number: 25, caught: 3 },
|
||||
{ name: "Togepi", number: 175, caught: 1 },
|
||||
{ name: "MissingNo", number: NaN, caught: 0 },
|
||||
],
|
||||
"caught"), false);
|
||||
```
|
||||
|
||||
`truthCheck([{name: "Pikachu", number: 25, caught: 3}, {name: "Togepi", number: 175, caught: 1}, {name: "MissingNo", number: NaN, caught: 0}], "number")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(truthCheck(
|
||||
[
|
||||
{ name: "Pikachu", number: 25, caught: 3 },
|
||||
{ name: "Togepi", number: 175, caught: 1 },
|
||||
{ name: "MissingNo", number: NaN, caught: 0 },
|
||||
],
|
||||
"number"), false);
|
||||
```
|
||||
|
||||
`truthCheck([{name: "Quincy", username: "QuincyLarson"}, {name: "Naomi", username: "nhcarrigan"}, {name: "Camperbot"}], "username")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(truthCheck(
|
||||
[
|
||||
{ name: "Quincy", username: "QuincyLarson" },
|
||||
{ name: "Naomi", username: "nhcarrigan" },
|
||||
{ name: "Camperbot" }
|
||||
],
|
||||
"username"), false);
|
||||
```
|
||||
|
||||
`truthCheck([{name: "freeCodeCamp", users: [{name: "Quincy"}, {name: "Naomi"}]}, {name: "Code Radio", users: [{name: "Camperbot"}]}, {name: "", users: []}], "users")` should return `true`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(truthCheck(
|
||||
[
|
||||
{ name: "freeCodeCamp", users: [{ name: "Quincy" }, { name: "Naomi" }] },
|
||||
{ name: "Code Radio", users: [{ name: "Camperbot" }] },
|
||||
{ name: "", users: [] },
|
||||
],
|
||||
"users"), true);
|
||||
```
|
||||
|
||||
`truthCheck([{id: 1, data: {url: "https://freecodecamp.org", name: "freeCodeCamp"}}, {id: 2, data: {url: "https://coderadio.freecodecamp.org/", name: "CodeRadio"}}, {id: null, data: {}}], "data")` should return `true`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(truthCheck(
|
||||
[
|
||||
{ id: 1, data: { url: "https://www.freecodecamp.org", name: "freeCodeCamp" } },
|
||||
{ id: 2, data: { url: "https://coderadio.freecodecamp.org/", name: "CodeRadio" } },
|
||||
{ id: null, data: {} },
|
||||
],
|
||||
"data"), true);
|
||||
```
|
||||
|
||||
`truthCheck([{id: 1, data: {url: "https://freecodecamp.org", name: "freeCodeCamp"}}, {id: 2, data: {url: "https://coderadio.freecodecamp.org/", name: "CodeRadio"}}, {id: null, data: {}}], "id")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(truthCheck(
|
||||
[
|
||||
{ id: 1, data: { url: "https://www.freecodecamp.org", name: "freeCodeCamp" } },
|
||||
{ id: 2, data: { url: "https://coderadio.freecodecamp.org/", name: "CodeRadio" } },
|
||||
{ id: null, data: {} },
|
||||
],
|
||||
"id"), false);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function truthCheck(collection, pre) {
|
||||
return pre;
|
||||
}
|
||||
|
||||
truthCheck([{name: "Quincy", role: "Founder", isBot: false}, {name: "Naomi", role: "", isBot: false}, {name: "Camperbot", role: "Bot", isBot: true}], "isBot");
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function truthCheck(collection, pre) {
|
||||
return collection.every(function(e) { return e[pre]; });
|
||||
}
|
||||
```
|
||||
+206
@@ -0,0 +1,206 @@
|
||||
---
|
||||
id: a2f1d72d9b908d0bd72bb9f6
|
||||
title: Make a Person
|
||||
challengeType: 1
|
||||
forumTopicId: 16020
|
||||
dashedName: make-a-person
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Fill in the object constructor with the following methods below:
|
||||
|
||||
```js
|
||||
getFirstName()
|
||||
getLastName()
|
||||
getFullName()
|
||||
setFirstName(first)
|
||||
setLastName(last)
|
||||
setFullName(first, last)
|
||||
```
|
||||
|
||||
Run the tests to see the expected output for each method. These methods must be the only available means of interacting with the object. Each test will declare a new `Person` instance as `new Person('Bob', 'Ross')`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should not change the function signature.
|
||||
|
||||
```js
|
||||
assert.match(code, /const\s+Person\s*=\s*function\s*\(\s*first\s*,\s*last\s*\)\s*{/);
|
||||
```
|
||||
|
||||
You should not reassign the `first` parameter.
|
||||
|
||||
```js
|
||||
assert.notMatch(code, /\bfirst\s*=\s*/);
|
||||
```
|
||||
|
||||
You should not reassign the `last` parameter.
|
||||
|
||||
```js
|
||||
assert.notMatch(code, /\blast\s*=\s*/);
|
||||
```
|
||||
|
||||
No properties should be added. `Object.keys(Person).length` should always return 6.
|
||||
|
||||
```js
|
||||
const _person = new Person('Bob', 'Ross');
|
||||
_person.setFirstName('Haskell');
|
||||
_person.setLastName('Curry');
|
||||
_person.setFullName('John', 'Smith');
|
||||
assert.lengthOf(Object.keys(_person), 6);
|
||||
```
|
||||
|
||||
You should be able to instantiate your `Person` object.
|
||||
|
||||
```js
|
||||
const _person = new Person('Bob', 'Ross');
|
||||
assert.instanceOf(_person, Person);
|
||||
```
|
||||
|
||||
Your `Person` object should not have a `firstName` property.
|
||||
|
||||
```js
|
||||
const _person = new Person('Bob', 'Ross');
|
||||
assert.notProperty(_person, 'firstName');
|
||||
```
|
||||
|
||||
Your `Person` object should not have a `lastName` property.
|
||||
|
||||
```js
|
||||
const _person = new Person('Bob', 'Ross');
|
||||
assert.notProperty(_person, 'lastName');
|
||||
```
|
||||
|
||||
The `.getFirstName()` method should return the string `Bob`.
|
||||
|
||||
```js
|
||||
const _person = new Person('Bob', 'Ross');
|
||||
assert.strictEqual(_person.getFirstName(), 'Bob');
|
||||
```
|
||||
|
||||
The `.getLastName()` should return the string `Ross`.
|
||||
|
||||
```js
|
||||
const _person = new Person('Bob', 'Ross');
|
||||
assert.strictEqual(_person.getLastName(), 'Ross');
|
||||
```
|
||||
|
||||
The `.getFullName()` method should return the string `Bob Ross`.
|
||||
|
||||
```js
|
||||
const _person = new Person('Bob', 'Ross');
|
||||
assert.strictEqual(_person.getFullName(), 'Bob Ross');
|
||||
```
|
||||
|
||||
The `.getFullName()` method should return the string `Haskell Ross` after calling `.setFirstName('Haskell')`.
|
||||
|
||||
```js
|
||||
const _person = new Person('Bob', 'Ross');
|
||||
_person.setFirstName('Haskell');
|
||||
assert.strictEqual(_person.getFullName(), 'Haskell Ross');
|
||||
```
|
||||
|
||||
The `.getFullName()` method should return the string `Bob Curry` after calling `.setLastName('Curry')`.
|
||||
|
||||
```js
|
||||
const _person = new Person('Bob', 'Ross');
|
||||
_person.setLastName('Curry');
|
||||
assert.strictEqual(_person.getFullName(), 'Bob Curry');
|
||||
```
|
||||
|
||||
The `.getFullName()` method should return the string `Haskell Curry` after calling `.setFullName('Haskell', 'Curry')`.
|
||||
|
||||
```js
|
||||
const _person = new Person('Bob', 'Ross');
|
||||
_person.setFullName('Haskell', 'Curry');
|
||||
assert.strictEqual(_person.getFullName(), 'Haskell Curry');
|
||||
```
|
||||
|
||||
The `.getFirstName()` method should return the string `Haskell` after calling `.setFullName('Haskell', 'Curry')`.
|
||||
|
||||
```js
|
||||
const _person = new Person('Bob', 'Ross');
|
||||
_person.setFullName('Haskell', 'Curry');
|
||||
assert.strictEqual(_person.getFirstName(), 'Haskell');
|
||||
```
|
||||
|
||||
The `.getLastName()` method should return the string `Curry` after calling `.setFullName('Haskell', 'Curry')`.
|
||||
|
||||
```js
|
||||
const _person = new Person('Bob', 'Ross');
|
||||
_person.setFullName('Haskell', 'Curry');
|
||||
assert.strictEqual(_person.getLastName(), 'Curry');
|
||||
```
|
||||
|
||||
The `.getFullName()` method should return the string `Emily Martinez de la Rosa` after calling `.setFullName('Emily Martinez', 'de la Rosa')`.
|
||||
|
||||
```js
|
||||
const _person = new Person('Bob', 'Ross');
|
||||
_person.setFullName('Emily Martinez', 'de la Rosa');
|
||||
assert.strictEqual(_person.getFullName(), 'Emily Martinez de la Rosa');
|
||||
```
|
||||
|
||||
The `.getFirstName()` property should return the string `Emily Martinez` after calling `.setFullName('Emily Martinez', 'de la Rosa')`.
|
||||
|
||||
```js
|
||||
const _person = new Person('Bob', 'Ross');
|
||||
_person.setFullName('Emily Martinez', 'de la Rosa');
|
||||
assert.strictEqual(_person.getFirstName(), 'Emily Martinez');
|
||||
```
|
||||
|
||||
The `.getLastName()` property should return the string `de la Rosa` after calling `.setFullName('Emily Martinez', 'de la Rosa')`.
|
||||
|
||||
```js
|
||||
const _person = new Person('Bob', 'Ross');
|
||||
_person.setFullName('Emily Martinez', 'de la Rosa');
|
||||
assert.strictEqual(_person.getLastName(), 'de la Rosa');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
const Person = function(first, last) {
|
||||
this.getFullName = function() {
|
||||
return "";
|
||||
};
|
||||
return "";
|
||||
};
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const Person = function(first, last) {
|
||||
let firstName = first;
|
||||
let lastName = last;
|
||||
|
||||
this.getFirstName = function(){
|
||||
return firstName;
|
||||
};
|
||||
|
||||
this.getLastName = function(){
|
||||
return lastName;
|
||||
};
|
||||
|
||||
this.getFullName = function(){
|
||||
return firstName + " " + lastName;
|
||||
};
|
||||
|
||||
this.setFirstName = function(str){
|
||||
firstName = str;
|
||||
};
|
||||
|
||||
|
||||
this.setLastName = function(str){
|
||||
lastName = str;
|
||||
};
|
||||
|
||||
this.setFullName = function(first, last){
|
||||
firstName = first;
|
||||
lastName = last;
|
||||
};
|
||||
};
|
||||
```
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
---
|
||||
id: a3566b1109230028080c9345
|
||||
title: Sum All Numbers in a Range
|
||||
challengeType: 1
|
||||
forumTopicId: 16083
|
||||
dashedName: sum-all-numbers-in-a-range
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
We'll pass you an array of two numbers. Return the sum of those two numbers plus the sum of all the numbers between them. The lowest number will not always come first.
|
||||
|
||||
For example, `sumAll([4,1])` should return `10` because sum of all the numbers between 1 and 4 (both inclusive) is `10`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`sumAll([1, 4])` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof sumAll([1, 4]) === 'number');
|
||||
```
|
||||
|
||||
`sumAll([1, 4])` should return 10.
|
||||
|
||||
```js
|
||||
assert.deepEqual(sumAll([1, 4]), 10);
|
||||
```
|
||||
|
||||
`sumAll([4, 1])` should return 10.
|
||||
|
||||
```js
|
||||
assert.deepEqual(sumAll([4, 1]), 10);
|
||||
```
|
||||
|
||||
`sumAll([5, 10])` should return 45.
|
||||
|
||||
```js
|
||||
assert.deepEqual(sumAll([5, 10]), 45);
|
||||
```
|
||||
|
||||
`sumAll([10, 5])` should return 45.
|
||||
|
||||
```js
|
||||
assert.deepEqual(sumAll([10, 5]), 45);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function sumAll(arr) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
sumAll([1, 4]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function sumAll(arr) {
|
||||
var sum = 0;
|
||||
arr.sort(function(a,b) {return a-b;});
|
||||
for (var i = arr[0]; i <= arr[1]; i++) {
|
||||
sum += i;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
```
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
---
|
||||
id: a39963a4c10bc8b4d4f06d7e
|
||||
title: Seek and Destroy
|
||||
challengeType: 1
|
||||
forumTopicId: 16046
|
||||
dashedName: seek-and-destroy
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
You will be provided with an initial array as the first argument to the `destroyer` function, followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.
|
||||
|
||||
The function must accept an indeterminate number of arguments, also known as a variadic function. You can access the additional arguments by adding a rest parameter to the function definition or using the `arguments` object.
|
||||
|
||||
# --hints--
|
||||
|
||||
`destroyer([1, 2, 3, 1, 2, 3], 2, 3)` should return `[1, 1]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(destroyer([1, 2, 3, 1, 2, 3], 2, 3), [1, 1]);
|
||||
```
|
||||
|
||||
`destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3)` should return `[1, 5, 1]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3), [1, 5, 1]);
|
||||
```
|
||||
|
||||
`destroyer([3, 5, 1, 2, 2], 2, 3, 5)` should return `[1]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(destroyer([3, 5, 1, 2, 2], 2, 3, 5), [1]);
|
||||
```
|
||||
|
||||
`destroyer([2, 3, 2, 3], 2, 3)` should return `[]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(destroyer([2, 3, 2, 3], 2, 3), []);
|
||||
```
|
||||
|
||||
`destroyer(["tree", "hamburger", 53], "tree", 53)` should return `["hamburger"]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(destroyer(['tree', 'hamburger', 53], 'tree', 53), [
|
||||
'hamburger'
|
||||
]);
|
||||
```
|
||||
|
||||
`destroyer(["possum", "trollo", 12, "safari", "hotdog", 92, 65, "grandma", "bugati", "trojan", "yacht"], "yacht", "possum", "trollo", "safari", "hotdog", "grandma", "bugati", "trojan")` should return `[12,92,65]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
destroyer(
|
||||
[
|
||||
'possum',
|
||||
'trollo',
|
||||
12,
|
||||
'safari',
|
||||
'hotdog',
|
||||
92,
|
||||
65,
|
||||
'grandma',
|
||||
'bugati',
|
||||
'trojan',
|
||||
'yacht'
|
||||
],
|
||||
'yacht',
|
||||
'possum',
|
||||
'trollo',
|
||||
'safari',
|
||||
'hotdog',
|
||||
'grandma',
|
||||
'bugati',
|
||||
'trojan'
|
||||
),
|
||||
[12, 92, 65]
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function destroyer(arr) {
|
||||
return arr;
|
||||
}
|
||||
|
||||
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function destroyer(arr) {
|
||||
var hash = Object.create(null);
|
||||
[].slice.call(arguments, 1).forEach(function(e) {
|
||||
hash[e] = true;
|
||||
});
|
||||
return arr.filter(function(e) { return !(e in hash);});
|
||||
}
|
||||
|
||||
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
|
||||
```
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
---
|
||||
id: a3bfc1673c0526e06d3ac698
|
||||
title: Sum All Primes
|
||||
challengeType: 1
|
||||
forumTopicId: 16085
|
||||
dashedName: sum-all-primes
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A <dfn>prime number</dfn> is a whole number greater than 1 with exactly two divisors: 1 and itself. For example, 2 is a prime number because it is only divisible by 1 and 2. In contrast, 4 is not prime since it is divisible by 1, 2 and 4.
|
||||
|
||||
Rewrite `sumPrimes` so it returns the sum of all prime numbers that are less than or equal to num.
|
||||
|
||||
# --hints--
|
||||
|
||||
`sumPrimes(10)` should return a number.
|
||||
|
||||
```js
|
||||
assert.deepEqual(typeof sumPrimes(10), 'number');
|
||||
```
|
||||
|
||||
`sumPrimes(10)` should return 17.
|
||||
|
||||
```js
|
||||
assert.deepEqual(sumPrimes(10), 17);
|
||||
```
|
||||
|
||||
`sumPrimes(977)` should return 73156.
|
||||
|
||||
```js
|
||||
assert.deepEqual(sumPrimes(977), 73156);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function sumPrimes(num) {
|
||||
return num;
|
||||
}
|
||||
|
||||
sumPrimes(10);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
class PrimeSeive {
|
||||
constructor(num) {
|
||||
const seive = Array(Math.floor((num - 1) / 2)).fill(true);
|
||||
const upper = Math.floor((num - 1) / 2);
|
||||
const sqrtUpper = Math.floor((Math.sqrt(num) - 1) / 2);
|
||||
|
||||
for (let i = 0; i <= sqrtUpper; i++) {
|
||||
if (seive[i]) {
|
||||
// Mark value in seive array
|
||||
const prime = 2 * i + 3;
|
||||
// Mark all multiples of this number as false (not prime)
|
||||
const primeSquaredIndex = 2 * i ** 2 + 6 * i + 3;
|
||||
for (let j = primeSquaredIndex; j < upper; j += prime) {
|
||||
seive[j] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this._seive = seive;
|
||||
}
|
||||
|
||||
isPrime(num) {
|
||||
return num === 2
|
||||
? true
|
||||
: num % 2 === 0
|
||||
? false
|
||||
: this.isOddPrime(num);
|
||||
}
|
||||
|
||||
isOddPrime(num) {
|
||||
return this._seive[(num - 3) / 2];
|
||||
}
|
||||
};
|
||||
|
||||
function sumPrimes(num) {
|
||||
const primeSeive = new PrimeSeive(num);
|
||||
|
||||
let sum = 2;
|
||||
for (let i = 3; i <= num; i += 2) {
|
||||
if (primeSeive.isOddPrime(i)) sum += i;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
sumPrimes(10);
|
||||
```
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
---
|
||||
id: a5229172f011153519423690
|
||||
title: Sum All Odd Fibonacci Numbers
|
||||
challengeType: 1
|
||||
forumTopicId: 16084
|
||||
dashedName: sum-all-odd-fibonacci-numbers
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a positive integer `num`, return the sum of all odd Fibonacci numbers that are less than or equal to `num`.
|
||||
|
||||
The first two numbers in the Fibonacci sequence are 0 and 1. Every additional number in the sequence is the sum of the two previous numbers. The first seven numbers of the Fibonacci sequence are 0, 1, 1, 2, 3, 5 and 8.
|
||||
|
||||
For example, `sumFibs(10)` should return `10` because all odd Fibonacci numbers less than or equal to `10` are 1, 1, 3, and 5.
|
||||
|
||||
# --hints--
|
||||
|
||||
`sumFibs(1)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof sumFibs(1) === 'number');
|
||||
```
|
||||
|
||||
`sumFibs(1000)` should return 1785.
|
||||
|
||||
```js
|
||||
assert(sumFibs(1000) === 1785);
|
||||
```
|
||||
|
||||
`sumFibs(4000000)` should return 4613732.
|
||||
|
||||
```js
|
||||
assert(sumFibs(4000000) === 4613732);
|
||||
```
|
||||
|
||||
`sumFibs(4)` should return 5.
|
||||
|
||||
```js
|
||||
assert(sumFibs(4) === 5);
|
||||
```
|
||||
|
||||
`sumFibs(75024)` should return 60696.
|
||||
|
||||
```js
|
||||
assert(sumFibs(75024) === 60696);
|
||||
```
|
||||
|
||||
`sumFibs(75025)` should return 135721.
|
||||
|
||||
```js
|
||||
assert(sumFibs(75025) === 135721);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function sumFibs(num) {
|
||||
return num;
|
||||
}
|
||||
|
||||
sumFibs(4);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function sumFibs(num) {
|
||||
var a = 1;
|
||||
var b = 1;
|
||||
var s = 0;
|
||||
while (a <= num) {
|
||||
if (a % 2 !== 0) {
|
||||
s += a;
|
||||
}
|
||||
a = [b, b=b+a][0];
|
||||
}
|
||||
return s;
|
||||
}
|
||||
```
|
||||
+193
@@ -0,0 +1,193 @@
|
||||
---
|
||||
id: a5de63ebea8dbee56860f4f2
|
||||
title: Diff Two Arrays
|
||||
challengeType: 1
|
||||
forumTopicId: 16008
|
||||
dashedName: diff-two-arrays
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Compare two arrays and return a new array with any items only found in one of the two given arrays, but not both. In other words, return the symmetric difference of the two arrays.
|
||||
|
||||
**Note:** You can return the array with its elements in any order.
|
||||
|
||||
# --hints--
|
||||
|
||||
`diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5])` should return an array.
|
||||
|
||||
```js
|
||||
assert(typeof diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]) === 'object');
|
||||
```
|
||||
|
||||
`["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]` should return `["pink wool"]`.
|
||||
|
||||
```js
|
||||
assert.sameMembers(
|
||||
diffArray(
|
||||
['diorite', 'andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'],
|
||||
['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']
|
||||
),
|
||||
['pink wool']
|
||||
);
|
||||
```
|
||||
|
||||
`["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]` should return an array with one item.
|
||||
|
||||
```js
|
||||
assert(
|
||||
diffArray(
|
||||
['diorite', 'andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'],
|
||||
['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']
|
||||
).length === 1
|
||||
);
|
||||
```
|
||||
|
||||
`["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]` should return `["diorite", "pink wool"]`.
|
||||
|
||||
```js
|
||||
assert.sameMembers(
|
||||
diffArray(
|
||||
['andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'],
|
||||
['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']
|
||||
),
|
||||
['diorite', 'pink wool']
|
||||
);
|
||||
```
|
||||
|
||||
`["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]` should return an array with two items.
|
||||
|
||||
```js
|
||||
assert(
|
||||
diffArray(
|
||||
['andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'],
|
||||
['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']
|
||||
).length === 2
|
||||
);
|
||||
```
|
||||
|
||||
`["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]` should return `[]`.
|
||||
|
||||
```js
|
||||
assert.sameMembers(
|
||||
diffArray(
|
||||
['andesite', 'grass', 'dirt', 'dead shrub'],
|
||||
['andesite', 'grass', 'dirt', 'dead shrub']
|
||||
),
|
||||
[]
|
||||
);
|
||||
```
|
||||
|
||||
`["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]` should return an empty array.
|
||||
|
||||
```js
|
||||
assert(
|
||||
diffArray(
|
||||
['andesite', 'grass', 'dirt', 'dead shrub'],
|
||||
['andesite', 'grass', 'dirt', 'dead shrub']
|
||||
).length === 0
|
||||
);
|
||||
```
|
||||
|
||||
`[1, 2, 3, 5], [1, 2, 3, 4, 5]` should return `[4]`.
|
||||
|
||||
```js
|
||||
assert.sameMembers(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]), [4]);
|
||||
```
|
||||
|
||||
`[1, 2, 3, 5], [1, 2, 3, 4, 5]` should return an array with one item.
|
||||
|
||||
```js
|
||||
assert(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]).length === 1);
|
||||
```
|
||||
|
||||
`[1, "calf", 3, "piglet"], [1, "calf", 3, 4]` should return `["piglet", 4]`.
|
||||
|
||||
```js
|
||||
assert.sameMembers(diffArray([1, 'calf', 3, 'piglet'], [1, 'calf', 3, 4]), [
|
||||
'piglet',
|
||||
4
|
||||
]);
|
||||
```
|
||||
|
||||
`[1, "calf", 3, "piglet"], [1, "calf", 3, 4]` should return an array with two items.
|
||||
|
||||
```js
|
||||
assert(diffArray([1, 'calf', 3, 'piglet'], [1, 'calf', 3, 4]).length === 2);
|
||||
```
|
||||
|
||||
`[], ["snuffleupagus", "cookie monster", "elmo"]` should return `["snuffleupagus", "cookie monster", "elmo"]`.
|
||||
|
||||
```js
|
||||
assert.sameMembers(diffArray([], ['snuffleupagus', 'cookie monster', 'elmo']), [
|
||||
'snuffleupagus',
|
||||
'cookie monster',
|
||||
'elmo'
|
||||
]);
|
||||
```
|
||||
|
||||
`[], ["snuffleupagus", "cookie monster", "elmo"]` should return an array with three items.
|
||||
|
||||
```js
|
||||
assert(diffArray([], ['snuffleupagus', 'cookie monster', 'elmo']).length === 3);
|
||||
```
|
||||
|
||||
`[1, "calf", 3, "piglet"], [7, "filly"]` should return `[1, "calf", 3, "piglet", 7, "filly"]`.
|
||||
|
||||
```js
|
||||
assert.sameMembers(diffArray([1, 'calf', 3, 'piglet'], [7, 'filly']), [
|
||||
1,
|
||||
'calf',
|
||||
3,
|
||||
'piglet',
|
||||
7,
|
||||
'filly'
|
||||
]);
|
||||
```
|
||||
|
||||
`[1, "calf", 3, "piglet"], [7, "filly"]` should return an array with six items.
|
||||
|
||||
```js
|
||||
assert(diffArray([1, 'calf', 3, 'piglet'], [7, 'filly']).length === 6);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function diffArray(arr1, arr2) {
|
||||
const newArr = [];
|
||||
return newArr;
|
||||
}
|
||||
|
||||
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function diffArray(arr1, arr2) {
|
||||
if (arr1.length === 0) return arr2;
|
||||
if (arr2.length === 0) return arr1;
|
||||
|
||||
const set1 = new Set(arr1);
|
||||
const set2 = new Set(arr2);
|
||||
|
||||
const newArr = [];
|
||||
|
||||
set1.forEach(element => {
|
||||
if (!set2.has(element)) newArr.push(element);
|
||||
|
||||
});
|
||||
|
||||
set2.forEach(element => {
|
||||
if (!set1.has(element)) newArr.push(element);
|
||||
|
||||
});
|
||||
|
||||
return newArr;
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
---
|
||||
id: a5deed1811a43193f9f1c841
|
||||
title: Drop it
|
||||
challengeType: 1
|
||||
forumTopicId: 16010
|
||||
dashedName: drop-it
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given the array `arr`, iterate through and remove each element starting from the first element (the 0 index) until the function `func` returns `true` when the iterated element is passed through it.
|
||||
|
||||
Then return the rest of the array once the condition is satisfied, otherwise, `arr` should be returned as an empty array.
|
||||
|
||||
# --hints--
|
||||
|
||||
`dropElements([1, 2, 3, 4], function(n) {return n >= 3;})` should return `[3, 4]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
dropElements([1, 2, 3, 4], function (n) {
|
||||
return n >= 3;
|
||||
}),
|
||||
[3, 4]
|
||||
);
|
||||
```
|
||||
|
||||
`dropElements([0, 1, 0, 1], function(n) {return n === 1;})` should return `[1, 0, 1]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
dropElements([0, 1, 0, 1], function (n) {
|
||||
return n === 1;
|
||||
}),
|
||||
[1, 0, 1]
|
||||
);
|
||||
```
|
||||
|
||||
`dropElements([1, 2, 3], function(n) {return n > 0;})` should return `[1, 2, 3]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
dropElements([1, 2, 3], function (n) {
|
||||
return n > 0;
|
||||
}),
|
||||
[1, 2, 3]
|
||||
);
|
||||
```
|
||||
|
||||
`dropElements([1, 2, 3, 4], function(n) {return n > 5;})` should return `[]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
dropElements([1, 2, 3, 4], function (n) {
|
||||
return n > 5;
|
||||
}),
|
||||
[]
|
||||
);
|
||||
```
|
||||
|
||||
`dropElements([1, 2, 3, 7, 4], function(n) {return n > 3;})` should return `[7, 4]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
dropElements([1, 2, 3, 7, 4], function (n) {
|
||||
return n > 3;
|
||||
}),
|
||||
[7, 4]
|
||||
);
|
||||
```
|
||||
|
||||
`dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;})` should return `[3, 9, 2]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
dropElements([1, 2, 3, 9, 2], function (n) {
|
||||
return n > 2;
|
||||
}),
|
||||
[3, 9, 2]
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function dropElements(arr, func) {
|
||||
return arr;
|
||||
}
|
||||
|
||||
dropElements([1, 2, 3], function(n) {return n < 3; });
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function dropElements(arr, func) {
|
||||
while (arr.length && !func(arr[0])) {
|
||||
arr.shift();
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
```
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
---
|
||||
id: a6b0bb188d873cb2c8729495
|
||||
title: Convert HTML Entities
|
||||
challengeType: 1
|
||||
forumTopicId: 16007
|
||||
dashedName: convert-html-entities
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Convert the characters `&`, `<`, `>`, `"` (double quote), and `'` (apostrophe), in a string to their corresponding HTML entities.
|
||||
|
||||
# --hints--
|
||||
|
||||
`convertHTML("Dolce & Gabbana")` should return the string `Dolce & Gabbana`.
|
||||
|
||||
```js
|
||||
assert.match(convertHTML('Dolce & Gabbana'), /Dolce & Gabbana/);
|
||||
```
|
||||
|
||||
`convertHTML("Hamburgers < Pizza < Tacos")` should return the string `Hamburgers < Pizza < Tacos`.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
convertHTML('Hamburgers < Pizza < Tacos'),
|
||||
/Hamburgers < Pizza < Tacos/
|
||||
);
|
||||
```
|
||||
|
||||
`convertHTML("Sixty > twelve")` should return the string `Sixty > twelve`.
|
||||
|
||||
```js
|
||||
assert.match(convertHTML('Sixty > twelve'), /Sixty > twelve/);
|
||||
```
|
||||
|
||||
`convertHTML('Stuff in "quotation marks"')` should return the string `Stuff in "quotation marks"`.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
convertHTML('Stuff in "quotation marks"'),
|
||||
/Stuff in "quotation marks"/
|
||||
);
|
||||
```
|
||||
|
||||
`convertHTML("Schindler's List")` should return the string `Schindler's List`.
|
||||
|
||||
```js
|
||||
assert.match(convertHTML("Schindler's List"), /Schindler's List/);
|
||||
```
|
||||
|
||||
`convertHTML("<>")` should return the string `<>`.
|
||||
|
||||
```js
|
||||
assert.match(convertHTML('<>'), /<>/);
|
||||
```
|
||||
|
||||
`convertHTML("abc")` should return the string `abc`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(convertHTML('abc'), 'abc');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function convertHTML(str) {
|
||||
return str;
|
||||
}
|
||||
|
||||
convertHTML("Dolce & Gabbana");
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var MAP = { '&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'"': '"',
|
||||
"'": '''};
|
||||
|
||||
function convertHTML(str) {
|
||||
return str.replace(/[&<>"']/g, function(c) {
|
||||
return MAP[c];
|
||||
});
|
||||
}
|
||||
```
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
---
|
||||
id: a8d97bd4c764e91f9d2bda01
|
||||
title: Binary Agents
|
||||
challengeType: 1
|
||||
forumTopicId: 14273
|
||||
dashedName: binary-agents
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Return an English translated sentence of the passed binary string.
|
||||
|
||||
The binary string will be space separated.
|
||||
|
||||
# --hints--
|
||||
|
||||
`binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111")` should return the string `Aren't bonfires fun!?`
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
binaryAgent(
|
||||
'01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111'
|
||||
),
|
||||
"Aren't bonfires fun!?"
|
||||
);
|
||||
```
|
||||
|
||||
`binaryAgent("01001001 00100000 01101100 01101111 01110110 01100101 00100000 01000110 01110010 01100101 01100101 01000011 01101111 01100100 01100101 01000011 01100001 01101101 01110000 00100001")` should return the string `I love FreeCodeCamp!`
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
binaryAgent(
|
||||
'01001001 00100000 01101100 01101111 01110110 01100101 00100000 01000110 01110010 01100101 01100101 01000011 01101111 01100100 01100101 01000011 01100001 01101101 01110000 00100001'
|
||||
),
|
||||
'I love FreeCodeCamp!'
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function binaryAgent(str) {
|
||||
return str;
|
||||
}
|
||||
|
||||
binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function binaryAgent(str) {
|
||||
return str.split(' ').map(function(s) { return parseInt(s, 2); }).map(function(b) { return String.fromCharCode(b);}).join('');
|
||||
}
|
||||
```
|
||||
+134
@@ -0,0 +1,134 @@
|
||||
---
|
||||
id: a8e512fbe388ac2f9198f0fa
|
||||
title: Wherefore art thou
|
||||
challengeType: 1
|
||||
forumTopicId: 16092
|
||||
dashedName: wherefore-art-thou
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Make a function that looks through an array of objects (first argument) and returns an array of all objects that have matching name and value pairs (second argument). Each name and value pair of the source object has to be present in the object from the collection if it is to be included in the returned array.
|
||||
|
||||
For example, if the first argument is `[{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }]`, and the second argument is `{ last: "Capulet" }`, then you must return the third object from the array (the first argument), because it contains the name and its value, that was passed on as the second argument.
|
||||
|
||||
# --hints--
|
||||
|
||||
`whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" })` should return `[{ first: "Tybalt", last: "Capulet" }]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
whatIsInAName(
|
||||
[
|
||||
{ first: 'Romeo', last: 'Montague' },
|
||||
{ first: 'Mercutio', last: null },
|
||||
{ first: 'Tybalt', last: 'Capulet' }
|
||||
],
|
||||
{ last: 'Capulet' }
|
||||
),
|
||||
[{ first: 'Tybalt', last: 'Capulet' }]
|
||||
);
|
||||
```
|
||||
|
||||
`whatIsInAName([{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }], { "apple": 1 })` should return `[{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
whatIsInAName([{ apple: 1 }, { apple: 1 }, { apple: 1, bat: 2 }], {
|
||||
apple: 1
|
||||
}),
|
||||
[{ apple: 1 }, { apple: 1 }, { apple: 1, bat: 2 }]
|
||||
);
|
||||
```
|
||||
|
||||
`whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 })` should return `[{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
whatIsInAName(
|
||||
[{ apple: 1, bat: 2 }, { bat: 2 }, { apple: 1, bat: 2, cookie: 2 }],
|
||||
{ apple: 1, bat: 2 }
|
||||
),
|
||||
[
|
||||
{ apple: 1, bat: 2 },
|
||||
{ apple: 1, bat: 2, cookie: 2 }
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
`whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "cookie": 2 })` should return `[{ "apple": 1, "bat": 2, "cookie": 2 }]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
whatIsInAName(
|
||||
[{ apple: 1, bat: 2 }, { apple: 1 }, { apple: 1, bat: 2, cookie: 2 }],
|
||||
{ apple: 1, cookie: 2 }
|
||||
),
|
||||
[{ apple: 1, bat: 2, cookie: 2 }]
|
||||
);
|
||||
```
|
||||
|
||||
`whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }, { "bat":2 }], { "apple": 1, "bat": 2 })` should return `[{ "apple": 1, "bat": 2 }, { "apple": 1, "bat": 2, "cookie":2 }]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
whatIsInAName(
|
||||
[
|
||||
{ apple: 1, bat: 2 },
|
||||
{ apple: 1 },
|
||||
{ apple: 1, bat: 2, cookie: 2 },
|
||||
{ bat: 2 }
|
||||
],
|
||||
{ apple: 1, bat: 2 }
|
||||
),
|
||||
[
|
||||
{ apple: 1, bat: 2 },
|
||||
{ apple: 1, bat: 2, cookie: 2 }
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
`whatIsInAName([{"a": 1, "b": 2, "c": 3}], {"a": 1, "b": 9999, "c": 3})` should return `[]`
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
whatIsInAName([{ a: 1, b: 2, c: 3 }], { a: 1, b: 9999, c: 3 }),
|
||||
[]
|
||||
);
|
||||
```
|
||||
|
||||
`whatIsInAName([{"a": 1, "b": 2, "c": 3, "d": 9999}], {"a": 1, "b": 9999, "c": 3})` should return `[]`
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
whatIsInAName([{ a: 1, b: 2, c: 3, d: 9999 }], { a: 1, b: 9999, c: 3 }),
|
||||
[]
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function whatIsInAName(collection, source) {
|
||||
|
||||
}
|
||||
|
||||
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function whatIsInAName(collection, source) {
|
||||
const arr = [];
|
||||
const keys = Object.keys(source);
|
||||
collection.forEach(function(e) {
|
||||
if(keys.every(function(key) {return e[key] === source[key];})) {
|
||||
arr.push(e);
|
||||
}
|
||||
});
|
||||
return arr;
|
||||
}
|
||||
```
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
---
|
||||
id: a97fd23d9b809dac9921074f
|
||||
title: Arguments Optional
|
||||
challengeType: 1
|
||||
forumTopicId: 14271
|
||||
dashedName: arguments-optional
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Create a function that sums two arguments together. If only one argument is provided, then return a function that expects one argument and returns the sum.
|
||||
|
||||
For example, `addTogether(2, 3)` should return `5`, and `addTogether(2)` should return a function.
|
||||
|
||||
Calling this returned function with a single argument will then return the sum:
|
||||
|
||||
```js
|
||||
var sumTwoAnd = addTogether(2);
|
||||
```
|
||||
|
||||
`sumTwoAnd(3)` returns `5`.
|
||||
|
||||
If either argument isn't a valid number, return undefined.
|
||||
|
||||
# --hints--
|
||||
|
||||
`addTogether(2, 3)` should return 5.
|
||||
|
||||
```js
|
||||
assert.deepEqual(addTogether(2, 3), 5);
|
||||
```
|
||||
|
||||
`addTogether(23.4, 30)` should return 53.4.
|
||||
|
||||
```js
|
||||
assert.deepEqual(addTogether(23.4, 30), 53.4);
|
||||
```
|
||||
|
||||
`addTogether("2", 3)` should return `undefined`.
|
||||
|
||||
```js
|
||||
assert.isUndefined(addTogether('2', 3));
|
||||
```
|
||||
|
||||
`addTogether(5, undefined)` should return `undefined`.
|
||||
|
||||
```js
|
||||
assert.isUndefined(addTogether(5, undefined));
|
||||
```
|
||||
|
||||
`addTogether("https://www.youtube.com/watch?v=dQw4w9WgXcQ")` should return `undefined`.
|
||||
|
||||
```js
|
||||
assert.isUndefined(addTogether('https://www.youtube.com/watch?v=dQw4w9WgXcQ'));
|
||||
```
|
||||
|
||||
`addTogether(5)` should return a function.
|
||||
|
||||
```js
|
||||
assert.deepEqual(typeof(addTogether(5)), 'function');
|
||||
```
|
||||
|
||||
`addTogether(5)(7)` should return 12.
|
||||
|
||||
```js
|
||||
assert.deepEqual(addTogether(5)(7), 12);
|
||||
```
|
||||
|
||||
`addTogether(2)([3])` should return `undefined`.
|
||||
|
||||
```js
|
||||
assert.isUndefined(addTogether(2)([3]));
|
||||
```
|
||||
|
||||
`addTogether(2, "3")` should return `undefined`.
|
||||
|
||||
```js
|
||||
assert.isUndefined(addTogether(2, '3'));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function addTogether() {
|
||||
return false;
|
||||
}
|
||||
|
||||
addTogether(2,3);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function addTogether() {
|
||||
const first = arguments[0];
|
||||
if (typeof(first) !== 'number') {
|
||||
return undefined;
|
||||
}
|
||||
if (arguments.length === 1) {
|
||||
return function(second) {
|
||||
if (typeof(second) !== 'number') {
|
||||
return undefined;
|
||||
}
|
||||
return first + second;
|
||||
};
|
||||
}
|
||||
const second = arguments[1];
|
||||
if (typeof(second) !== 'number') {
|
||||
return undefined;
|
||||
}
|
||||
return first + second;
|
||||
}
|
||||
```
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
---
|
||||
id: aa7697ea2477d1316795783b
|
||||
title: Pig Latin
|
||||
challengeType: 1
|
||||
forumTopicId: 16039
|
||||
dashedName: pig-latin
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Pig Latin is a way of altering English Words. The rules are as follows:
|
||||
|
||||
\- If a word begins with a consonant, take the first consonant or consonant cluster, move it to the end of the word, and add `ay` to it.
|
||||
|
||||
\- If a word begins with a vowel, just add `way` at the end.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Translate the provided string to Pig Latin. Input strings are guaranteed to be English words in all lowercase.
|
||||
|
||||
# --hints--
|
||||
|
||||
`translatePigLatin("california")` should return the string `aliforniacay`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(translatePigLatin('california'), 'aliforniacay');
|
||||
```
|
||||
|
||||
`translatePigLatin("paragraphs")` should return the string `aragraphspay`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(translatePigLatin('paragraphs'), 'aragraphspay');
|
||||
```
|
||||
|
||||
`translatePigLatin("glove")` should return the string `oveglay`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(translatePigLatin('glove'), 'oveglay');
|
||||
```
|
||||
|
||||
`translatePigLatin("algorithm")` should return the string `algorithmway`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(translatePigLatin('algorithm'), 'algorithmway');
|
||||
```
|
||||
|
||||
`translatePigLatin("eight")` should return the string `eightway`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(translatePigLatin('eight'), 'eightway');
|
||||
```
|
||||
|
||||
Should handle words where the first vowel comes in the middle of the word. `translatePigLatin("schwartz")` should return the string `artzschway`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(translatePigLatin('schwartz'), 'artzschway');
|
||||
```
|
||||
|
||||
Should handle words without vowels. `translatePigLatin("rhythm")` should return the string `rhythmay`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(translatePigLatin('rhythm'), 'rhythmay');
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function translatePigLatin(str) {
|
||||
return str;
|
||||
}
|
||||
|
||||
translatePigLatin("consonant");
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function translatePigLatin(str) {
|
||||
if (isVowel(str.charAt(0))) return str + "way";
|
||||
var front = [];
|
||||
str = str.split('');
|
||||
while (str.length && !isVowel(str[0])) {
|
||||
front.push(str.shift());
|
||||
}
|
||||
return [].concat(str, front).join('') + 'ay';
|
||||
}
|
||||
|
||||
function isVowel(c) {
|
||||
return ['a', 'e', 'i', 'o', 'u'].indexOf(c.toLowerCase()) !== -1;
|
||||
}
|
||||
```
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
---
|
||||
id: ab306dbdcc907c7ddfc30830
|
||||
title: Steamroller
|
||||
challengeType: 1
|
||||
forumTopicId: 16079
|
||||
dashedName: steamroller
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Flatten a nested array. You must account for varying levels of nesting.
|
||||
|
||||
# --hints--
|
||||
|
||||
`steamrollArray([[["a"]], [["b"]]])` should return `["a", "b"]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(steamrollArray([[['a']], [['b']]]), ['a', 'b']);
|
||||
```
|
||||
|
||||
`steamrollArray([1, [2], [3, [[4]]]])` should return `[1, 2, 3, 4]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(steamrollArray([1, [2], [3, [[4]]]]), [1, 2, 3, 4]);
|
||||
```
|
||||
|
||||
`steamrollArray([1, [], [3, [[4]]]])` should return `[1, 3, 4]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(steamrollArray([1, [], [3, [[4]]]]), [1, 3, 4]);
|
||||
```
|
||||
|
||||
`steamrollArray([1, {}, [3, [[4]]]])` should return `[1, {}, 3, 4]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(steamrollArray([1, {}, [3, [[4]]]]), [1, {}, 3, 4]);
|
||||
```
|
||||
|
||||
Your solution should not use the `Array.prototype.flat()` or `Array.prototype.flatMap()` methods.
|
||||
|
||||
```js
|
||||
assert(!__helpers.removeJSComments(code).match(/\.\s*flat\s*\(/) && !__helpers.removeJSComments(code).match(/\.\s*flatMap\s*\(/));
|
||||
```
|
||||
|
||||
Global variables should not be used.
|
||||
|
||||
```js
|
||||
steamrollArray([1, {}, [3, [[4]]]])
|
||||
assert.deepEqual(steamrollArray([1, {}, [3, [[4]]]]), [1, {}, 3, 4])
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function steamrollArray(arr) {
|
||||
return arr;
|
||||
}
|
||||
|
||||
steamrollArray([1, [2], [3, [[4]]]]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function steamrollArray(arr) {
|
||||
if (!Array.isArray(arr)) {
|
||||
return [arr];
|
||||
}
|
||||
var out = [];
|
||||
arr.forEach(function(e) {
|
||||
steamrollArray(e).forEach(function(v) {
|
||||
out.push(v);
|
||||
});
|
||||
});
|
||||
return out;
|
||||
}
|
||||
```
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
---
|
||||
id: ae9defd7acaf69703ab432ea
|
||||
title: Smallest Common Multiple
|
||||
challengeType: 1
|
||||
forumTopicId: 16075
|
||||
dashedName: smallest-common-multiple
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters.
|
||||
|
||||
The range will be an array of two numbers that will not necessarily be in numerical order.
|
||||
|
||||
For example, if given 1 and 3, find the smallest common multiple of both 1 and 3 that is also evenly divisible by all numbers *between* 1 and 3. The answer here would be 6.
|
||||
|
||||
# --hints--
|
||||
|
||||
`smallestCommons([1, 5])` should return a number.
|
||||
|
||||
```js
|
||||
assert.deepEqual(typeof smallestCommons([1, 5]), 'number');
|
||||
```
|
||||
|
||||
`smallestCommons([1, 5])` should return 60.
|
||||
|
||||
```js
|
||||
assert.deepEqual(smallestCommons([1, 5]), 60);
|
||||
```
|
||||
|
||||
`smallestCommons([5, 1])` should return 60.
|
||||
|
||||
```js
|
||||
assert.deepEqual(smallestCommons([5, 1]), 60);
|
||||
```
|
||||
|
||||
`smallestCommons([2, 10])` should return 2520.
|
||||
|
||||
```js
|
||||
assert.deepEqual(smallestCommons([2, 10]), 2520);
|
||||
```
|
||||
|
||||
`smallestCommons([1, 13])` should return 360360.
|
||||
|
||||
```js
|
||||
assert.deepEqual(smallestCommons([1, 13]), 360360);
|
||||
```
|
||||
|
||||
`smallestCommons([23, 18])` should return 6056820.
|
||||
|
||||
```js
|
||||
assert.deepEqual(smallestCommons([23, 18]), 6056820);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function smallestCommons(arr) {
|
||||
return arr;
|
||||
}
|
||||
|
||||
smallestCommons([1,5]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function gcd(a, b) {
|
||||
while (b !== 0) {
|
||||
a = [b, b = a % b][0];
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
function lcm(a, b) {
|
||||
return (a * b) / gcd(a, b);
|
||||
}
|
||||
|
||||
function smallestCommons(arr) {
|
||||
arr.sort(function(a,b) {return a-b;});
|
||||
var rng = [];
|
||||
for (var i = arr[0]; i <= arr[1]; i++) {
|
||||
rng.push(i);
|
||||
}
|
||||
return rng.reduce(lcm);
|
||||
}
|
||||
```
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
---
|
||||
id: af4afb223120f7348cdfc9fd
|
||||
title: Map the Debris
|
||||
challengeType: 1
|
||||
forumTopicId: 16021
|
||||
dashedName: map-the-debris
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
According to Kepler's Third Law, the orbital period $T$ of two point masses orbiting each other in a circular or elliptic orbit is:
|
||||
|
||||
$$
|
||||
T = 2 \pi \sqrt{\frac{a^{3}}{\mu}}
|
||||
$$
|
||||
|
||||
- $a$ is the orbit's semi-major axis
|
||||
- $μ = GM$ is the standard gravitational parameter
|
||||
- $G$ is the gravitational constant,
|
||||
- $M$ is the mass of the more massive body.
|
||||
|
||||
Return a new array that transforms the elements' average altitude into their orbital periods (in seconds).
|
||||
|
||||
The array will contain objects in the format `{name: 'name', avgAlt: avgAlt}`.
|
||||
|
||||
The values should be rounded to the nearest whole number. The body being orbited is Earth.
|
||||
|
||||
The radius of the earth is 6367.4447 kilometers, and the GM value of earth is 398600.4418 km<sup>3</sup>s<sup>-2</sup>.
|
||||
|
||||
# --hints--
|
||||
|
||||
`orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}])` should return `[{name: "sputnik", orbitalPeriod: 86400}]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(orbitalPeriod([{ name: 'sputnik', avgAlt: 35873.5553 }]), [
|
||||
{ name: 'sputnik', orbitalPeriod: 86400 }
|
||||
]);
|
||||
```
|
||||
|
||||
`orbitalPeriod([{name: "iss", avgAlt: 413.6}, {name: "hubble", avgAlt: 556.7}, {name: "moon", avgAlt: 378632.553}])` should return `[{name : "iss", orbitalPeriod: 5557}, {name: "hubble", orbitalPeriod: 5734}, {name: "moon", orbitalPeriod: 2377399}]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
orbitalPeriod([
|
||||
{ name: 'iss', avgAlt: 413.6 },
|
||||
{ name: 'hubble', avgAlt: 556.7 },
|
||||
{ name: 'moon', avgAlt: 378632.553 }
|
||||
]),
|
||||
[
|
||||
{ name: 'iss', orbitalPeriod: 5557 },
|
||||
{ name: 'hubble', orbitalPeriod: 5734 },
|
||||
{ name: 'moon', orbitalPeriod: 2377399 }
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function orbitalPeriod(arr) {
|
||||
const GM = 398600.4418;
|
||||
const earthRadius = 6367.4447;
|
||||
return arr;
|
||||
}
|
||||
|
||||
orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function orbitalPeriod(arr) {
|
||||
const GM = 398600.4418;
|
||||
const earthRadius = 6367.4447;
|
||||
const TAU = 2 * Math.PI;
|
||||
return arr.map(function(obj) {
|
||||
return {
|
||||
name: obj.name,
|
||||
orbitalPeriod: Math.round(TAU * Math.sqrt(Math.pow(obj.avgAlt+earthRadius, 3)/GM))
|
||||
};
|
||||
});
|
||||
}
|
||||
```
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
---
|
||||
id: af7588ade1100bde429baf20
|
||||
title: Missing letters
|
||||
challengeType: 1
|
||||
forumTopicId: 16023
|
||||
dashedName: missing-letters
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Find the missing letter in the passed letter range and return it.
|
||||
|
||||
If all letters are present in the range, return `undefined`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`fearNotLetter("abce")` should return the string `d`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(fearNotLetter('abce'), 'd');
|
||||
```
|
||||
|
||||
`fearNotLetter("abcdefghjklmno")` should return the string `i`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(fearNotLetter('abcdefghjklmno'), 'i');
|
||||
```
|
||||
|
||||
`fearNotLetter("stvwx")` should return the string `u`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(fearNotLetter('stvwx'), 'u');
|
||||
```
|
||||
|
||||
`fearNotLetter("bcdf")` should return the string `e`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(fearNotLetter('bcdf'), 'e');
|
||||
```
|
||||
|
||||
`fearNotLetter("abcdefghijklmnopqrstuvwxyz")` should return `undefined`.
|
||||
|
||||
```js
|
||||
assert.isUndefined(fearNotLetter('abcdefghijklmnopqrstuvwxyz'));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function fearNotLetter(str) {
|
||||
return str;
|
||||
}
|
||||
|
||||
fearNotLetter("abce");
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function fearNotLetter (str) {
|
||||
for (var i = str.charCodeAt(0); i <= str.charCodeAt(str.length - 1); i++) {
|
||||
var letter = String.fromCharCode(i);
|
||||
if (str.indexOf(letter) === -1) {
|
||||
return letter;
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
```
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
---
|
||||
id: afd15382cdfb22c9efe8b7de
|
||||
title: DNA Pairing
|
||||
challengeType: 1
|
||||
forumTopicId: 16009
|
||||
dashedName: dna-pairing
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Pairs of DNA strands consist of nucleobase pairs. Base pairs are represented by the characters <em>AT</em> and <em>CG</em>, which form building blocks of the DNA double helix.
|
||||
|
||||
The DNA strand is missing the pairing element. Write a function to match the missing base pairs for the provided DNA strand. For each character in the provided string, find the base pair character. Return the results as a 2d array.
|
||||
|
||||
For example, for the input `GCG`, return `[["G", "C"], ["C","G"], ["G", "C"]]`
|
||||
|
||||
The character and its pair are paired up in an array, and all the arrays are grouped into one encapsulating array.
|
||||
|
||||
# --hints--
|
||||
|
||||
`pairElement("ATCGA")` should return `[["A","T"],["T","A"],["C","G"],["G","C"],["A","T"]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(pairElement('ATCGA'), [
|
||||
['A', 'T'],
|
||||
['T', 'A'],
|
||||
['C', 'G'],
|
||||
['G', 'C'],
|
||||
['A', 'T']
|
||||
]);
|
||||
```
|
||||
|
||||
`pairElement("TTGAG")` should return `[["T","A"],["T","A"],["G","C"],["A","T"],["G","C"]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(pairElement('TTGAG'), [
|
||||
['T', 'A'],
|
||||
['T', 'A'],
|
||||
['G', 'C'],
|
||||
['A', 'T'],
|
||||
['G', 'C']
|
||||
]);
|
||||
```
|
||||
|
||||
`pairElement("CTCTA")` should return `[["C","G"],["T","A"],["C","G"],["T","A"],["A","T"]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(pairElement('CTCTA'), [
|
||||
['C', 'G'],
|
||||
['T', 'A'],
|
||||
['C', 'G'],
|
||||
['T', 'A'],
|
||||
['A', 'T']
|
||||
]);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function pairElement(str) {
|
||||
return str;
|
||||
}
|
||||
|
||||
pairElement("GCG");
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
var lookup = Object.create(null);
|
||||
lookup.A = 'T';
|
||||
lookup.T = 'A';
|
||||
lookup.C = 'G';
|
||||
lookup.G = 'C';
|
||||
|
||||
function pairElement(str) {
|
||||
return str.split('').map(function(p) {return [p, lookup[p]];});
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user