JSFiddle - React, Tailwind, and code Playground

by tonytlwu

HTML

<h1>Test Results</h1>
<ul>
</ul>

CSS

* { font-family: sans-serif; line-height: 150% }
li { margin-bottom: 20px }
li.success { color: green }
li.error { color: red }
li code { font-family: monospace; font-weight: bold; }

JavaScript

// ------------------------------------------
// FILL IN YOUR ANSWERS HERE AND CLICK RUN!
// TIME TAKEN: x MINUTES
// ------------------------------------------

function mul(x, y) {
  let result = 0;
  let a = x < 0 ? -x : x;
  let b = y < 0 ? -y : y;
  
  // TW: Could just check if x or y is 0 and return 0 if that is the case

	// TW: The last test case is taking too long to execute because of this
  for (i=0; i<a;i++) {
    result += b;
  }

  if (x < 0 && y > 0 || x > 0 && y < 0) result = -result
  return result;
}

// --------------------------------------------
// TESTS BELOW - PLEASE ADD YOUR OWN TEST CASES
// --------------------------------------------

expect('mul(2,3)', mul(2, 3), 6);
expect('mul(2,3)', mul(2, 3), 6);
expect('mul(0,3)', mul(0, 3), 0);
expect('mul(-2,3)', mul(-2, 3), -6);
expect('mul(2,-3)', mul(2, -3), -6);
expect('mul(-2,-3)', mul(-2, -3), 6);
expect('mul(1,999999999)', mul(1, 999999999), 999999999);
expect('mul(999999999,1)', mul(999999999, 1), 999999999); // TW: This is taking way too long than it needs to

// ------------------------------------------

function expect(name, a, b) {
  if (a !== b) {
    $('ul').append('<li class="error">Testing <code>' + name + '</code> ...<br />Error: <code>' + a + '</code> is the wrong answer</li>');
  } else {
    $('ul').append('<li class="success">Testing <code>' + name + '</code> ...<br>Right: <code>' + a + '</code> is the right answer</li>');
  }
}