IndigoDreams
IndigoDreams

Reputation: 71

JavaScript - Are numbers ordered Kata

I'm a recent coding bootcamp student, I graduated on Friday. Whilst i'm looking for my first career opportunity, I've been looking back at some of the early katas we were given to develop our problem solving skills, and I have to say I've been stumped by a few of them. I've written the tests for these problems and used TDD to try and solve them. The language is Javascript.

Can anyone kindly point me in the right direction with this. I've written 4 tests, it's passing 3 out of the 4, I can't get it to pass the 3rd test, and I'm not sure what else to try.

Kata Instructions: the areOrdered function should take an array of numbers as an input. It should return true if all the numbers are in ascending order and false if they are not. An empty array should return false.

My tests:

const areOrdered = require("../katas/are-ordered");
const { expect } = require("chai");

describe.only("areOrdered", () => {
  it("returns a boolean", () => {
    const nums = [];
    const actualResults = areOrdered(nums);
    const expectedResults = false;
    expect(actualResults).to.equal(expectedResults);
  });

  it("returns false if passed an empty array", () => {
    const nums = [];
    const actualResults = areOrdered(nums);
    const expectedResults = false;
    expect(actualResults).to.equal(expectedResults);
  });

  it("returns true if the numbers array is in ascending order", () => {
    const nums = [1, 2, 3];
    const actualResults = areOrdered(nums);
    const expectedResults = true;
    expect(actualResults).to.equal(expectedResults);
  });
  it("returns false if the numbers array is not in ascending order", () => {
    const nums = [3, 1, 0, 2];
    const actualResults = areOrdered(nums);
    const expectedResults = false;
    expect(actualResults).to.equal(expectedResults);
  });
});

My solution:

function areOrdered(nums) {
  if (nums.length === 0) {
    return false;
  }

  for (let i = 0; i < nums.length; i++) {
    if (!nums[i] < nums[i] + 1) {
      return false;
    } else if (nums[i] < nums[i] + 1) {
      return true;
    }
  }
}

Results:

areOrdered

✓ returns a boolean
✓ returns false if passed an empty array
1) returns true if the numbers array is in ascending order
✓ returns false if the numbers array is not in ascending order

3 passing (11ms) 1 failing

1) areOrdered returns true if the numbers array is in ascending order:

  AssertionError: expected false to equal true
  + expected - actual

  -false
  +true 

Upvotes: 4

Views: 164

Answers (2)

ifbamoq
ifbamoq

Reputation: 437

I would do that like this:

function areOrdered(nums) {
  if (nums.length === 0) return false;

  let sorted = nums.concat().sort((a, b) => a-b);
  let isSorted = true;
  for(let i in sorted)
    if(sorted[i] !== nums[i] && isSorted) isSorted = false;
  return isSorted;
}

Just copy array and sort with Array.sort() and then compare it to original one.

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386644

You could iterate from the second item and check the item before and the actual item.

Return false if the values are not in order.

At the end return true.

function areOrdered(nums) {
    if (nums.length === 0) return false;

    for (let i = 1; i < nums.length; i++) {
        if (nums[i - 1] >= nums[i]) return false;
    }
    return true;
}

console.log(areOrdered([]));           // false
console.log(areOrdered([1, 2, 3]));    //  true
console.log(areOrdered([3, 1, 0, 2])); // false

Upvotes: 1

Related Questions