Randell Dawson 04f18e43f6 fix(curriculum): Remove unnecessary assert message argument from English Coding Interview Prep challenges - 02 (#36412)
* fix: removed assert msg argument

* fix: removed msgs surrounded by 2 single quotes

* fix: removed missing 2 assert msg arguments

* fix: remove msg surrounded by two single quotes

* fix: removed unnecessary assert msg args

* fix; remove msgs surrounded by double quotes

* fix: removed unnecessary assert msg args

* fix: remove unnecessary assert msg args

* fix: removed unnecessary assert msg arg

* fix: removed unnecessary assert msg args

* fix: removed unnecessary assert msg arg

* fix: removed unnecessary assert msg args

* fix: removed unnecessary assert msg args

* fix: removed unnecessary assert msg args

* fix: removed unnecessary assert msg args

* fix: removed unnecessary assert msg args

* fix: removed unnecessary assert msg arg

* fix: removed unnecessary assert msg args

* fix: Restore expected values to assertions

* fix: remove assertion message

Co-authored-by: Vivek Agrawal <vivekmittalagrawal@gmail.com>
2019-07-26 14:24:52 +02:00

2.6 KiB

title, id, challengeType
title id challengeType
Compare a list of strings 596e457071c35c882915b3e4 5

Description

Given a list of arbitrarily many strings, implement a function for each of the following conditions:
  • test if they are all lexically equal
  • test if every string is lexically less than the one after it (i.e. whether the list is in strict ascending order)

Instructions

Tests

tests:
  - text: <code>allEqual</code> is a function.
    testString: assert(typeof allEqual === 'function');
  - text: <code>azSorted</code> is a function.
    testString: assert(typeof azSorted === 'function');
  - text: <code>allEqual(["AA", "AA", "AA", "AA"])</code> returns true.
    testString: assert(allEqual(testCases[0]));
  - text: <code>azSorted(["AA", "AA", "AA", "AA"])</code> returns false.
    testString: assert(!azSorted(testCases[0]));
  - text: <code>allEqual(["AA", "ACB", "BB", "CC"])</code> returns false.
    testString: assert(!allEqual(testCases[1]));
  - text: <code>azSorted(["AA", "ACB", "BB", "CC"])</code> returns true.
    testString: assert(azSorted(testCases[1]));
  - text: <code>allEqual([])</code> returns true.
    testString: assert(allEqual(testCases[2]));
  - text: <code>azSorted([])</code> returns true.
    testString: assert(azSorted(testCases[2]));
  - text: <code>allEqual(["AA"])</code> returns true.
    testString: assert(allEqual(testCases[3]));
  - text: <code>azSorted(["AA"])</code> returns true.
    testString: assert(azSorted(testCases[3]));
  - text: <code>allEqual(["BB", "AA"])</code> returns false.
    testString: assert(!allEqual(testCases[4]));
  - text: <code>azSorted(["BB", "AA"])</code> returns false.
    testString: assert(!azSorted(testCases[4]));

Challenge Seed

function allEqual(arr) {
  // Good luck!
  return true;
}

function azSorted(arr) {
  // Good luck!
  return true;
}

After Test

const testCases = [['AA', 'AA', 'AA', 'AA'], ['AA', 'ACB', 'BB', 'CC'], [], ['AA'], ['BB', 'AA']];

Solution

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;
}