diff options
author | Andinus <andinus@nand.sh> | 2021-09-03 18:18:33 +0530 |
---|---|---|
committer | Andinus <andinus@nand.sh> | 2021-09-03 18:18:33 +0530 |
commit | 9cec9e1815f3da2da02173b5302b372a761df00f (patch) | |
tree | dedfd040e6d5d3a7c313a543b9df2aacc1ca904d /javascript | |
parent | cbc991d678e6c7ee84471c7c6561a6084bb85ccd (diff) | |
download | exercism-9cec9e1815f3da2da02173b5302b372a761df00f.tar.gz |
JS: Raindrops: Add solution
Diffstat (limited to 'javascript')
-rw-r--r-- | javascript/raindrops/.eslintrc | 14 | ||||
-rw-r--r-- | javascript/raindrops/.npmrc | 1 | ||||
-rw-r--r-- | javascript/raindrops/HELP.md | 73 | ||||
-rw-r--r-- | javascript/raindrops/LICENSE | 21 | ||||
-rw-r--r-- | javascript/raindrops/README.md | 41 | ||||
-rw-r--r-- | javascript/raindrops/babel.config.js | 15 | ||||
-rw-r--r-- | javascript/raindrops/package.json | 31 | ||||
-rw-r--r-- | javascript/raindrops/raindrops.js | 11 | ||||
-rw-r--r-- | javascript/raindrops/raindrops.spec.js | 52 |
9 files changed, 259 insertions, 0 deletions
diff --git a/javascript/raindrops/.eslintrc b/javascript/raindrops/.eslintrc new file mode 100644 index 0000000..1d44460 --- /dev/null +++ b/javascript/raindrops/.eslintrc @@ -0,0 +1,14 @@ +{ + "root": true, + "extends": "@exercism/eslint-config-javascript", + "env": { + "jest": true + }, + "overrides": [ + { + "files": [".meta/proof.ci.js", ".meta/exemplar.js", "*.spec.js"], + "excludedFiles": ["custom.spec.js"], + "extends": "@exercism/eslint-config-javascript/maintainers" + } + ] +} diff --git a/javascript/raindrops/.npmrc b/javascript/raindrops/.npmrc new file mode 100644 index 0000000..d26df80 --- /dev/null +++ b/javascript/raindrops/.npmrc @@ -0,0 +1 @@ +audit=false diff --git a/javascript/raindrops/HELP.md b/javascript/raindrops/HELP.md new file mode 100644 index 0000000..1f95812 --- /dev/null +++ b/javascript/raindrops/HELP.md @@ -0,0 +1,73 @@ +# Help + +## Running the tests + +## Setup + +Go through the setup [instructions for JavaScript][docs-exercism-javascript] to install the necessary dependencies. + +## Requirements + +Install assignment dependencies: + +```shell +# Using npm +npm install + +# Alternatively using yarn +yarn +``` + +## Making the test suite pass + +All exercises come with a test suite to help you validate your solution before submitting. +You can execute these tests by opening a command prompt in the exercise's directory, and then running: + +```bash +# Using npm +npm test + +# Alternatively using yarn +yarn test +``` + +In some test suites all tests but the first have been skipped. + +Once you get a test passing, you can enable the next one by changing `xtest` to `test`. + +## Writing custom tests + +If you wish to write additional, custom, tests, create a new file `custom.spec.js`, and submit it with your solution together with the new file: + +```shell +exercism submit numbers.js custom.spec.js +``` + +[docs-exercism-javascript]: https://exercism.io/tracks/javascript/installation + +## Submitting your solution + +You can submit your solution using the `exercism submit raindrops.js` command. +This command will upload your solution to the Exercism website and print the solution page's URL. + +It's possible to submit an incomplete solution which allows you to: + +- See how others have completed the exercise +- Request help from a mentor + +## Need to get help? + +If you'd like help solving the exercise, check the following pages: + +- The [JavaScript track's documentation](https://exercism.org/docs/tracks/javascript) +- [Exercism's support channel on gitter](https://gitter.im/exercism/support) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +To get help if you're having trouble, you can use one of the following resources: + +- [Gitter](https://gitter.im/exercism/support) is Exercism's Gitter room; go here to get support and ask questions if you face any issues with downloading or submitting your exercises. +- [/r/javascript](https://www.reddit.com/r/javascript) is the Javascript subreddit. +- [StackOverflow](https://stackoverflow.com/questions/tagged/javascript+exercism) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions. +- [Github issue tracker](https://github.com/exercism/javascript/issues) is where we track our development and maintainance of Javascript exercises in exercism. But if none of the above links help you, feel free to post an issue here. \ No newline at end of file diff --git a/javascript/raindrops/LICENSE b/javascript/raindrops/LICENSE new file mode 100644 index 0000000..90e73be --- /dev/null +++ b/javascript/raindrops/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Exercism + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/javascript/raindrops/README.md b/javascript/raindrops/README.md new file mode 100644 index 0000000..bc08f4e --- /dev/null +++ b/javascript/raindrops/README.md @@ -0,0 +1,41 @@ +# Raindrops + +Welcome to Raindrops on Exercism's JavaScript Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Your task is to convert a number into a string that contains raindrop sounds corresponding to certain potential factors. A factor is a number that evenly divides into another number, leaving no remainder. The simplest way to test if a one number is a factor of another is to use the [modulo operation](https://en.wikipedia.org/wiki/Modulo_operation). + +The rules of `raindrops` are that if a given number: + +- has 3 as a factor, add 'Pling' to the result. +- has 5 as a factor, add 'Plang' to the result. +- has 7 as a factor, add 'Plong' to the result. +- _does not_ have any of 3, 5, or 7 as a factor, the result should be the digits of the number. + +## Examples + +- 28 has 7 as a factor, but not 3 or 5, so the result would be "Plong". +- 30 has both 3 and 5 as factors, but not 7, so the result would be "PlingPlang". +- 34 is not factored by 3, 5, or 7, so the result would be "34". + +## Source + +### Created by + +- @matthewmorgan + +### Contributed to by + +- @ankorGH +- @cmccandless +- @JesseSingleton +- @ovidiu141 +- @rchavarria +- @ryanplusplus +- @SleeplessByte + +### Based on + +A variation on FizzBuzz, a famous technical interview question that is intended to weed out potential candidates. That question is itself derived from Fizz Buzz, a popular children's game for teaching division. - https://en.wikipedia.org/wiki/Fizz_buzz \ No newline at end of file diff --git a/javascript/raindrops/babel.config.js b/javascript/raindrops/babel.config.js new file mode 100644 index 0000000..7940aa0 --- /dev/null +++ b/javascript/raindrops/babel.config.js @@ -0,0 +1,15 @@ +module.exports = { + presets: [ + [ + '@babel/preset-env', + { + targets: { + node: 'current', + }, + useBuiltIns: 'entry', + corejs: '3.14', + }, + ], + ], + plugins: ['@babel/plugin-syntax-bigint'], +}; diff --git a/javascript/raindrops/package.json b/javascript/raindrops/package.json new file mode 100644 index 0000000..3d6722c --- /dev/null +++ b/javascript/raindrops/package.json @@ -0,0 +1,31 @@ +{ + "name": "@exercism/javascript-raindrops", + "description": "Exercism exercises in Javascript.", + "author": "Katrina Owen", + "private": true, + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/exercism/javascript", + "directory": "exercises/practice/raindrops" + }, + "devDependencies": { + "@babel/cli": "^7.14.8", + "@babel/core": "^7.15.4", + "@babel/plugin-syntax-bigint": "^7.8.3", + "@babel/preset-env": "^7.15.0", + "@exercism/eslint-config-javascript": "^0.4.0", + "@types/jest": "^27.0.1", + "@types/node": "^16.7.10", + "babel-jest": "^27.1.0", + "core-js": "^3.17.1", + "eslint": "^7.32.0", + "jest": "^26.6.3" + }, + "dependencies": {}, + "scripts": { + "test": "jest --no-cache ./*", + "watch": "jest --no-cache --watch ./*", + "lint": "eslint ." + } +} diff --git a/javascript/raindrops/raindrops.js b/javascript/raindrops/raindrops.js new file mode 100644 index 0000000..f8dff04 --- /dev/null +++ b/javascript/raindrops/raindrops.js @@ -0,0 +1,11 @@ +'use strict'; + +export const convert = (num) => { + let drops = ""; + if (num % 3 == 0) drops += "Pling"; + if (num % 5 == 0) drops += "Plang"; + if (num % 7 == 0) drops += "Plong"; + + if (drops.length == 0) drops += num; + return drops; +}; diff --git a/javascript/raindrops/raindrops.spec.js b/javascript/raindrops/raindrops.spec.js new file mode 100644 index 0000000..7d16076 --- /dev/null +++ b/javascript/raindrops/raindrops.spec.js @@ -0,0 +1,52 @@ +import { convert } from './raindrops'; + +describe('Raindrops', () => { + test('the sound for 1 is 1', () => expect(convert(1)).toEqual('1')); + + test('the sound for 3 is Pling', () => expect(convert(3)).toEqual('Pling')); + + test('the sound for 5 is Plang', () => expect(convert(5)).toEqual('Plang')); + + test('the sound for 7 is Plong', () => expect(convert(7)).toEqual('Plong')); + + test('the sound for 6 is Pling as it has a factor 3', () => + expect(convert(6)).toEqual('Pling')); + + test('2 to the power 3 does not make a raindrop sound as 3 is the exponent not the base', () => + expect(convert(8)).toEqual('8')); + + test('the sound for 9 is Pling as it has a factor 3', () => + expect(convert(9)).toEqual('Pling')); + + test('the sound for 10 is Plang as it has a factor 5', () => + expect(convert(10)).toEqual('Plang')); + + test('the sound for 14 is Plong as it has a factor of 7', () => + expect(convert(14)).toEqual('Plong')); + + test('the sound for 15 is PlingPlang as it has factors 3 and 5', () => + expect(convert(15)).toEqual('PlingPlang')); + + test('the sound for 21 is PlingPlong as it has factors 3 and 7', () => + expect(convert(21)).toEqual('PlingPlong')); + + test('the sound for 25 is Plang as it has a factor 5', () => + expect(convert(25)).toEqual('Plang')); + + test('the sound for 27 is Pling as it has a factor 3', () => + expect(convert(27)).toEqual('Pling')); + + test('the sound for 35 is PlangPlong as it has factors 5 and 7', () => + expect(convert(35)).toEqual('PlangPlong')); + + test('the sound for 49 is Plong as it has a factor 7', () => + expect(convert(49)).toEqual('Plong')); + + test('the sound for 52 is 52', () => expect(convert(52)).toEqual('52')); + + test('the sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7', () => + expect(convert(105)).toEqual('PlingPlangPlong')); + + test('the sound for 3125 is Plang as it has a factor 5', () => + expect(convert(3125)).toEqual('Plang')); +}); |