2018-10-04 14:37:37 +01:00
---
title: Compare a list of strings
id: 596e457071c35c882915b3e4
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302235
2018-10-04 14:37:37 +01:00
---
## Description
< section id = 'description' >
2019-03-01 17:10:50 +09:00
Given a < a href = "https://en.wikipedia.org/wiki/List_(abstract_data_type)" title = "wp: List_(abstract_data_type)" target = "_blank" > list< / a > of arbitrarily many strings, implement a function for each of the following conditions:
2019-02-25 13:36:09 +09:00
< ul >
< li > test if they are all lexically equal< / li >
< li > test if every string is lexically less than the one after it (i.e. whether the list is in strict ascending order)< / li >
< / ul >
2018-10-04 14:37:37 +01:00
< / section >
## Instructions
< section id = 'instructions' >
< / section >
## Tests
< section id = 'tests' >
```yml
tests:
2019-11-20 07:01:31 -08:00
- text: < code > allEqual</ code > should be a function.
2019-07-26 05:24:52 -07:00
testString: assert(typeof allEqual === 'function');
2019-11-20 07:01:31 -08:00
- text: < code > azSorted</ code > should be a function.
2019-07-26 05:24:52 -07:00
testString: assert(typeof azSorted === 'function');
2019-11-20 07:01:31 -08:00
- text: < code > allEqual(["AA", "AA", "AA", "AA"])</ code > should return true.
2019-07-26 05:24:52 -07:00
testString: assert(allEqual(testCases[0]));
2019-11-20 07:01:31 -08:00
- text: < code > azSorted(["AA", "AA", "AA", "AA"])</ code > should return false.
2019-07-26 05:24:52 -07:00
testString: assert(!azSorted(testCases[0]));
2019-11-20 07:01:31 -08:00
- text: < code > allEqual(["AA", "ACB", "BB", "CC"])</ code > should return false.
2019-07-26 05:24:52 -07:00
testString: assert(!allEqual(testCases[1]));
2019-11-20 07:01:31 -08:00
- text: < code > azSorted(["AA", "ACB", "BB", "CC"])</ code > should return true.
2019-07-26 05:24:52 -07:00
testString: assert(azSorted(testCases[1]));
2019-11-20 07:01:31 -08:00
- text: < code > allEqual([])</ code > should return true.
2019-07-26 05:24:52 -07:00
testString: assert(allEqual(testCases[2]));
2019-11-20 07:01:31 -08:00
- text: < code > azSorted([])</ code > should return true.
2019-07-26 05:24:52 -07:00
testString: assert(azSorted(testCases[2]));
2019-11-20 07:01:31 -08:00
- text: < code > allEqual(["AA"])</ code > should return true.
2019-07-26 05:24:52 -07:00
testString: assert(allEqual(testCases[3]));
2019-11-20 07:01:31 -08:00
- text: < code > azSorted(["AA"])</ code > should return true.
2019-07-26 05:24:52 -07:00
testString: assert(azSorted(testCases[3]));
2019-11-20 07:01:31 -08:00
- text: < code > allEqual(["BB", "AA"])</ code > should return false.
2019-07-26 05:24:52 -07:00
testString: assert(!allEqual(testCases[4]));
2019-11-20 07:01:31 -08:00
- text: < code > azSorted(["BB", "AA"])</ code > should return false.
2019-07-26 05:24:52 -07:00
testString: assert(!azSorted(testCases[4]));
2018-10-04 14:37:37 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
2019-02-26 17:07:07 +09:00
function allEqual(arr) {
2018-10-04 14:37:37 +01:00
// Good luck!
return true;
}
2019-02-26 17:07:07 +09:00
function azSorted(arr) {
2018-10-04 14:37:37 +01:00
// Good luck!
return true;
}
```
< / div >
### After Test
< div id = 'js-teardown' >
```js
2018-10-20 21:02:47 +03:00
const testCases = [['AA', 'AA', 'AA', 'AA'], ['AA', 'ACB', 'BB', 'CC'], [], ['AA'], ['BB', 'AA']];
2018-10-04 14:37:37 +01:00
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
function allEqual(a) {
let out = true;
let i = 0;
while (++i < a.length ) {
out = out & & (a[i - 1] === a[i]);
} return out;
}
function azSorted(a) {
let out = true;
let i = 0;
while (++i < a.length ) {
out = out & & (a[i - 1] < a [ i ] ) ;
} return out;
}
```
< / section >