freeCodeCamp/curriculum/challenges/portuguese/10-coding-interview-prep/rosetta-code/longest-string-challenge.md

102 lines
2.1 KiB
Markdown
Raw Normal View History

---
id: 5e6dd14192286d95fc43046e
title: Longest string challenge
challengeType: 5
forumTopicId: 385275
dashedName: longest-string-challenge
---
# --description--
In this challenge, you have to find the strings that are the longest among the given strings.
# --instructions--
Write a function that takes an array of strings and returns the strings that have a length equal to the longest length.
# --hints--
`longestString` should be a function.
```js
assert(typeof longestString == 'function');
```
`longestString(["a", "bb", "ccc", "ee", "f", "ggg"])` should return a array.
```js
assert(Array.isArray(longestString(['a', 'bb', 'ccc', 'ee', 'f', 'ggg'])));
```
`longestString(["a", "bb", "ccc", "ee", "f", "ggg"])` should return `["ccc", "ggg"]'`.
```js
assert.deepEqual(longestString(['a', 'bb', 'ccc', 'ee', 'f', 'ggg']), [
'ccc',
'ggg'
]);
```
`longestString(["afedg", "bb", "sdccc", "efdee", "f", "geegg"])` should return `["afedg", "sdccc", "efdee", "geegg"]`.
```js
assert.deepEqual(
longestString(['afedg', 'bb', 'sdccc', 'efdee', 'f', 'geegg']),
['afedg', 'sdccc', 'efdee', 'geegg']
);
```
`longestString(["a", "bhghgb", "ccc", "efde", "fssdrr", "ggg"])` should return `["bhghgb", "fssdrr"]`.
```js
assert.deepEqual(
longestString(['a', 'bhghgb', 'ccc', 'efde', 'fssdrr', 'ggg']),
['bhghgb', 'fssdrr']
);
```
`longestString(["ahgfhg", "bdsfsb", "ccc", "ee", "f", "ggdsfg"])` should return `["ahgfhg", "bdsfsb", "ggdsfg"]`.
```js
assert.deepEqual(
longestString(['ahgfhg', 'bdsfsb', 'ccc', 'ee', 'f', 'ggdsfg']),
['ahgfhg', 'bdsfsb', 'ggdsfg']
);
```
`longestString(["a", "bbdsf", "ccc", "edfe", "gzzzgg"])` should return `["gzzzgg"]`.
```js
assert.deepEqual(longestString(['a', 'bbdsf', 'ccc', 'edfe', 'gzzzgg']), [
'gzzzgg'
]);
```
# --seed--
## --seed-contents--
```js
function longestString(strings) {
}
```
# --solutions--
```js
function longestString(strings) {
var mx = 0;
var result = []
strings.forEach(function (e) {
if (e.length > mx) {
mx = e.length
result = [e]
} else if (e.length == mx)
result.push(e)
})
return result
}
```