Skip to content
Snippets Groups Projects
Commit 60d83c8e authored by Brady James Garvin's avatar Brady James Garvin
Browse files

Initial commit.

parents
No related branches found
No related tags found
No related merge requests found
Showing
with 346 additions and 0 deletions
# Disable line-ending conversions for this repository.
* -text
# dependencies
/node_modules
# testing
/coverage
# production
/build
# environments
.env.local
.env.development.local
.env.test.local
.env.production.local
# logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# misc
*~
.DS_Store
[submodule "eslint-config"]
path = eslint-config
url = git@git.unl.edu:soft-core/soft-260/eslint-config.git
# JavaScript Practice
A collection of small JavaScript exercises for practice when learning the
language.
# Quick Start
Recursively clone this repository and `cd` into the root folder:
```
$ git clone --recursive git@git.unl.edu:soft-core/soft-260/javascript-practice.git
$ cd javascript-practice
```
(If you forget `--recursive` when cloning, you can `cd` into your clone and run
`git submodule update --init --recursive` instead.)
Install dependencies:
```
$ npm install
```
Optionally run the linter and the test suite:
```
$ npm run lint
$ npm run test
```
# Exercises
In the `src` folder there are 19 folders lettered `a` to `s`, each named for
some feature of the JavaScript language and containing an exercise on using that
feature. Within each folder are three files:
* An ordinary `*.js` file. This file will contain a small piece of starter
code and comments with instructions on what code to write where the comment
appears. You can edit this file to solve the exercise.
* A `*.key.js` file. This file contains a solution to the exercise in case
you get stuck.
* A `*.test.js` file. This file contains test cases to check your solution.
In case the instructions are unclear, you can also read these test cases to
better understand what the code under test should do.
Once you have attempted an exercise, you can save your changes to the `*.js`
file and then test your work with a command like
```
$ npm run test-once:a
```
where the letter after the colon is the letter of the exercise. These test
scripts are also available from the "NPM SCRIPTS" tray in VSCode.
To check your code style, use the command
```
$ npm run lint
```
Or run the `lint` task from the "NPM SCRIPTS" tray in VSCode. Note that the
linter may report some style errors for exercises that you have yet to complete.
Subproject commit a85278e2bd62f637800bc32fa6b372bc2855546e
{
"folders": [
{
"path": "."
}
],
"settings": {
"files.eol": "LF",
"files.exclude": {
"**/node_modules": true
},
"files.trimFinalNewlines": true,
"files.trimTrailingWhitespace": true
}
}
# dependencies
/node_modules
# testing
/coverage
# production
/build
# environments
.env.local
.env.development.local
.env.test.local
.env.production.local
# logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# misc
*~
.DS_Store
Source diff could not be displayed: it is too large. Options to address this: view the blob.
{
"name": "@unlsoft/javascript-practice",
"version": "1.0.0",
"description": "A collection of small JavaScript exercises for practice when learning the language.",
"type": "module",
"private": true,
"license": "UNLICENSED",
"scripts": {
"lint:js": "eslint --max-warnings 0 ./src",
"lint": "run-s --continue-on-error lint:**",
"test-once:a": "node --experimental-vm-modules node_modules/jest/bin/jest.js -t \"\\`formatAsTriple\\` function\"",
"test-once:b": "node --experimental-vm-modules node_modules/jest/bin/jest.js -t \"\\`classify\\` function\"",
"test-once:c": "node --experimental-vm-modules node_modules/jest/bin/jest.js -t \"\\`decodeCountingNumber\\` function\"",
"test-once:d": "node --experimental-vm-modules node_modules/jest/bin/jest.js -t \"\\`countDigits\\` function\"",
"test-once:e": "node --experimental-vm-modules node_modules/jest/bin/jest.js -t \"\\`triangularNumber\\` function\"",
"test-once:f": "node --experimental-vm-modules node_modules/jest/bin/jest.js -t \"\\`sum\\` function\"",
"test-once:g": "node --experimental-vm-modules node_modules/jest/bin/jest.js -t \"first \\`Book\\` class\"",
"test-once:h": "node --experimental-vm-modules node_modules/jest/bin/jest.js -t \"second \\`Book\\` class\"",
"test-once:i": "node --experimental-vm-modules node_modules/jest/bin/jest.js -t \"\\`Temperature\\` class\"",
"test-once:j": "node --experimental-vm-modules node_modules/jest/bin/jest.js -t \"first \\`PlanarVector\\` class\"",
"test-once:k": "node --experimental-vm-modules node_modules/jest/bin/jest.js -t \"second \\`PlanarVector\\` class\"",
"test-once:l": "node --experimental-vm-modules node_modules/jest/bin/jest.js -t \"\\`Swappable\\` class\"",
"test-once:m": "node --experimental-vm-modules node_modules/jest/bin/jest.js -t \"\\`countDistinctElements\\` function\"",
"test-once:n": "node --experimental-vm-modules node_modules/jest/bin/jest.js -t \"\\`findMostCommonCharacter\\` function\"",
"test-once:o": "node --experimental-vm-modules node_modules/jest/bin/jest.js -t \"\\`summarize\\` function\"",
"test-once:p": "node --experimental-vm-modules node_modules/jest/bin/jest.js -t \"\\`formatArithmeticProgression\\` function\"",
"test-once:q": "node --experimental-vm-modules node_modules/jest/bin/jest.js -t \"\\`countArguments\\` function\"",
"test-once:r": "node --experimental-vm-modules node_modules/jest/bin/jest.js -t \"\\`Accumulator\\` class\"",
"test-once:s": "node --experimental-vm-modules node_modules/jest/bin/jest.js -t \"\\`makeBetweenFilter\\` function\"",
"test-once": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watchAll"
},
"dependencies": {
"npm-run-all": "^4.1.5"
},
"devDependencies": {
"@unlsoft/eslint-config": "file:../eslint-config",
"eslint": "^8.20.0",
"jest": "^29.6.2",
"jest-environment-node": "^29.6.2"
},
"eslintConfig": {
"extends": "@unlsoft"
},
"jest": {
"clearMocks": true,
"collectCoverageFrom": [
"src/**/*.js",
"!src/testing/**/*.js"
],
"resetMocks": false,
"restoreMocks": false,
"testEnvironment": "jest-environment-node",
"transform": {}
},
"//": [
"See https://github.com/facebook/jest/issues/9430 for information on Jest+ES6."
]
}
export function formatAsTriple(x, y, z) {
// INSTRUCTIONS: Given three coordinates, return a template literal that
// formats them as a three-coordinate triple. For example, formatAsTriple(4,
// 5, 6) should return the string '(4, 5, 6)'.
//
// Resources:
// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals>
// <https://javascript.info/types#string>
// <https://javascript.info/string#quotes>
}
export function formatAsTriple(x, y, z) {
return `(${x}, ${y}, ${z})`;
}
import { formatAsTriple } from './templateLiterals.js';
describe('the `formatAsTriple` function', () => {
test('correctly formats the triple (0, 0, 0)', () => {
expect(formatAsTriple(0, 0, 0)).toBe('(0, 0, 0)');
});
test('correctly formats the triple (1, 2, 3)', () => {
expect(formatAsTriple(1, 2, 3)).toBe('(1, 2, 3)');
});
test('correctly formats the triple (-1, 2, -3)', () => {
expect(formatAsTriple(-1, 2, -3)).toBe('(-1, 2, -3)');
});
});
export function classify(number) {
let description = 'not a';
// INSTRUCTIONS: Use a chained conditional to set the variable `description`
// to 'negative' when number is negative, 'zero' when number is zero, and
// 'positive' when number is positive. If number is none of those (for
// example, if number is NaN), leave the description as it is.
//
// Resources:
// <https://javascript.info/ifelse#several-conditions-else-if>
// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else>
return `${description} number`;
}
export function classify(number) {
let description = 'not a';
if (number < 0) {
description = 'negative';
} else if (number === 0) {
description = 'zero';
} else if (number > 0) {
description = 'positive';
}
return `${description} number`;
}
import { classify } from './conditionals.js';
describe('the `classify` function', () => {
test('correctly classifies a negative infinite number', () => {
expect(classify(-Infinity)).toBe('negative number');
});
test('correctly classifies a negative finite number', () => {
expect(classify(-4)).toBe('negative number');
});
test('correctly classifies zero', () => {
expect(classify(0)).toBe('zero number');
});
test('correctly classifies a positive finite number', () => {
expect(classify(4)).toBe('positive number');
});
test('correctly classifies a positive infinite number', () => {
expect(classify(Infinity)).toBe('positive number');
});
test('correctly classifies a NaN', () => {
expect(classify(NaN)).toBe('not a number');
});
});
/* eslint-disable no-magic-numbers */
export function decodeCountingNumber(word) {
let meaning = undefined;
// INSTRUCTIONS: Use a switch statement to set meaning to 0, 1, 2, 3, or 4 if
// word is 'zero', 'one', two', 'three', or 'four', respectively. Leave
// meaning undefined in the default case.
//
// Resources:
// <https://javascript.info/switch>
// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch>
return `The word "${word}" is associated with the meaning ${meaning}.`;
}
/* eslint-disable no-magic-numbers */
export function decodeCountingNumber(word) {
let meaning = undefined;
switch (word) {
case 'zero':
meaning = 0;
break;
case 'one':
meaning = 1;
break;
case 'two':
meaning = 2;
break;
case 'three':
meaning = 3;
break;
case 'four':
meaning = 4;
break;
default:
}
return `The word "${word}" is associated with the meaning ${meaning}.`;
}
import { decodeCountingNumber } from './switchStatements.js';
describe('the `decodeCountingNumber` function', () => {
test('correctly interprets the word "zero"', () => {
expect(decodeCountingNumber('zero')).toBe('The word "zero" is associated with the meaning 0.');
});
test('correctly interprets the word "one"', () => {
expect(decodeCountingNumber('one')).toBe('The word "one" is associated with the meaning 1.');
});
test('correctly interprets the word "two"', () => {
expect(decodeCountingNumber('two')).toBe('The word "two" is associated with the meaning 2.');
});
test('correctly interprets the word "three"', () => {
expect(decodeCountingNumber('three')).toBe('The word "three" is associated with the meaning 3.');
});
test('correctly interprets the word "four"', () => {
expect(decodeCountingNumber('four')).toBe('The word "four" is associated with the meaning 4.');
});
test('correctly interprets the word "five"', () => {
expect(decodeCountingNumber('five')).toBe('The word "five" is associated with the meaning undefined.');
});
});
export function countDigits(positiveInteger) {
let result = 0;
let remainingDigits = positiveInteger;
// INSTRUCTIONS: Write a loop here that runs as long as `remainingDigits` is
// still positive, and on each iteration increments result and updates
// `remainingDigits` to `Math.floor(remainingDigits / 10)`.
//
// Resources:
// <https://javascript.info/while-for#the-while-loop>
// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while>
// <https://javascript.info/operators#increment-decrement>
// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Increment>
// <https://javascript.info/number#rounding>
// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor>
return result;
}
export function countDigits(positiveInteger) {
let result = 0;
let remainingDigits = positiveInteger;
while (remainingDigits > 0) {
++result;
remainingDigits = Math.floor(remainingDigits / 10);
}
return result;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment