Stardog JavaScript Exercise 1

by Cody Bennett

HTML

<link rel="stylesheet" href="//cdn.jsdelivr.net/jasmine/1.3.1/jasmine.css">
<script src="//cdn.jsdelivr.net/jasmine/1.3.1/jasmine.js"></script>
<script src="//cdn.jsdelivr.net/jasmine/1.3.1/jasmine-html.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/showdown.min.js"></script>
<!-- NOTE: THERE IS NO NEED TO EDIT ANY HTML FOR THIS TASK. -->
<link href="https://fonts.googleapis.com/css?family=Lato:400,700&display=swap" rel="stylesheet">
<h1>Stardog JavaScript Exercise 1</h1>
<div id="exercise_prompt"></div>

CSS

/* NOTE: THERE IS NO NEED TO EDIT CSS FOR THIS TASK. */
body {
  font-family: 'Lato', sans-serif;
  color: #f1f1f1;
  background-color: rgb(32, 38, 46);
  line-height: 1.5;
  padding: 20px;
}

code {
  background-color: rgb(29, 33, 40);
  color: rgb(216, 89, 38);
  display: inline-block;
}

#HTMLReporter {
  background-color: #fff;
}

JavaScript

// YOUR TASK IS TO REWRITE THIS FUNCTION SO THAT TESTS PASS.
// See the instructions and test results to the right.
function mapObject(object, callback) {
	const keys = Object.keys(object);
  const values = Object.values(object);
  
  const output = {};
  
  keys.forEach((key, index) => {
  	output[key] = callback(values[index]);
  });
  
  return output;
}

function mapArray(array, callback) {
	const output = [];
	
  array.forEach(value => {
  	output.push(callback(value));
  });
  
  return output;
}

const map = (input, callback) => {
	const isObject = typeof input === 'object';
  
  const output = isObject
  	? mapObject(input, callback)
    : mapArray(input, callback);
	
  return output;
};

/**
 * BEGIN CODE REQUIRED FOR TESTS. DO NOT REMOVE.
 */
const markdownConverter = new showdown.Converter();
const promptMarkdown = `
## Mappable Arrays *and* Objects

For as long as JavaScript engines have implemented the ES5 specification, there
has been a \`map\` method available on all \`Array\` objects. This method accepts a
callback function and an optional context argument. When \`map\` is called, the
callback function is called on each element of the array, and the return value
of the callback function is put into a new array that is ultimately returned as
the result of \`map\`. Here is an example:

\`\`\`javascript
const mappedArray = [1, 2, 3, 4].map((originalValue) => originalValue * 2);
// mappedArray => [2, 4, 6, 8]
\`\`\`

It is sometimes convenient to have a function like this that will work on both
\`Array\` objects _and_ plain old JavaScript objects (POJOs). When given an
\`Array\` object, this function would do exactly what the \`.map\` method does
above. When given a POJO, this function would return a new object with the same
keys, but where the values of those keys are the results of applying the
callback to those values. It would go something like this:

\`\`\`javascript
const mappedArray2 = map([1, 2, 3, 4], (originalValue) => originalValue * 2);
//...