From c2fff5181efbdba16ce0f7b1208ea6f370093a4a Mon Sep 17 00:00:00 2001 From: eliaahadi Date: Wed, 11 Oct 2017 18:15:11 -0700 Subject: [PATCH 1/2] reverse string exercise --- exercises/reverse-string/README.md | 106 ++++++++++++++++++ .../reverse-string/canonical-schema.json | 54 +++++++++ exercises/reverse-string/description.md | 1 + exercises/reverse-string/metadata.yml | 5 + exercises/reverse-string/reverse-string.js | 15 +++ .../reverse-string/reverse-string.spec.js | 51 +++++++++ 6 files changed, 232 insertions(+) create mode 100644 exercises/reverse-string/README.md create mode 100644 exercises/reverse-string/canonical-schema.json create mode 100644 exercises/reverse-string/description.md create mode 100644 exercises/reverse-string/metadata.yml create mode 100644 exercises/reverse-string/reverse-string.js create mode 100644 exercises/reverse-string/reverse-string.spec.js diff --git a/exercises/reverse-string/README.md b/exercises/reverse-string/README.md new file mode 100644 index 00000000..7b046b5b --- /dev/null +++ b/exercises/reverse-string/README.md @@ -0,0 +1,106 @@ +# Reverse String + +The introductory exercise to arrays and strings. Just reverse input string. + +This is a typical first program for beginning programming in a new language +or environment after being able to do simple tasks. + +The objectives are simple: + +- Write a function that reverses the input string. +- Run the test suite and make sure that it succeeds. +- Submit your solution and check it at the website. + +If everything goes well, you will be ready to fetch your first real exercise. + +## Tutorial + +This exercise has two files: + +- reverse-string.js +- reverse-string.spec.js + +The first file is where you will write your code. +The second is where the tests are defined. + +The tests will check whether your code is doing the right thing. +You don't need to be able to write a test suite from scratch, +but it helps to understand what a test looks like, and what +it is doing. + +Open up the test file, reverse-string.spec.js. +There is one test inside: + + xit('detects simple reverse string', function () { + var subject = new ReverseString('ant'); + var matches = subject.matches(['tna']); + + expect(matches).toEqual(['tna']); + }); + +Run the test now, with the following command on the command-line: + + jasmine reverse-string.spec.js + +The test fails, which makes sense since you've not written any code yet. + +The failure looks like this: + + 1) Reverse String says reverse string + Message: + Expected undefined to equal 'tna'. + +There's more, but this is the most important part. + +Take a look at that first line: + + 1) Reverse String says reverse string + +Now look at the test definition again: + + it('detects simple reverse string', function() { + // ... more code here ... + }); + +Update the code and then run the tests again from the command-line: + + jasmine reverse-string.spec.js + +Notice how it changes the failure message. + +Then change the implementation in reverse-string.js again, this time to make the test pass. + +When you are done, submit your solution to exercism: + + exercism submit reverse-string.js + + +## Setup + +Go through the setup instructions for JavaScript to +install the necessary dependencies: + +http://exercism.io/languages/javascript + +## Making the Test Suite Pass + +Execute the tests with: + + jasmine .spec.js + +Replace `` with the name of the current exercise. E.g., to +test the Reverse String exercise: + + jasmine reverse-string.spec.js + +In many test suites all but the first test have been skipped. + +Once you get a test passing, you can unskip the next one by +changing `xit` to `it`. + +## Source + +This is an exercise to introduce users to using Exercism and arrays and strings [https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb](https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb) + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/exercises/reverse-string/canonical-schema.json b/exercises/reverse-string/canonical-schema.json new file mode 100644 index 00000000..33cc26ec --- /dev/null +++ b/exercises/reverse-string/canonical-schema.json @@ -0,0 +1,54 @@ +{ + "exercise": "revere-string", + "version": "1.0.1", + "comments": [ + "The string argument cases possible matches are passed in as", + "individual arguments rather than arrays. Languages can include", + "these string argument cases if passing individual arguments is", + "idiomatic in that language." + ], + "cases": [ + { + "description": "no matches", + "property": "reverse-string", + "subject": "car", + "candidates": ["girl", "Japan", "bear", "candy"], + "expected": [] + }, + { + "description": "detects simple reverse string", + "property": "reverse-string", + "subject": "ant", + "candidates": ["tna"], + "expected": ["tan"] + }, + { + "description": "detects reverse-string", + "property": "reverse-string", + "subject": "listen", + "candidates": ["netsil"], + "expected": ["netsil"] + }, + { + "description": "detects anagrams case-insensitively", + "property": "reverse-string", + "subject": "NinJa", + "candidates": ["ajnin"], + "expected": ["ajnin"] + }, + { + "description": "matches() accepts string arguments", + "property": "reverse-string", + "subject": "ant", + "candidates": ["tna"], + "expected": ["tna"] + }, + { + "description": "matches() accepts single string argument", + "property": "reverse-string", + "subject": "ant", + "candidates": ["ant"], + "expected": ["tna"] + } + ] +} \ No newline at end of file diff --git a/exercises/reverse-string/description.md b/exercises/reverse-string/description.md new file mode 100644 index 00000000..e49b125e --- /dev/null +++ b/exercises/reverse-string/description.md @@ -0,0 +1 @@ +With an input string, write code to reverse the string letters and return a string with reversed letters. Be sure to include error handling for an input that isn’t a string such as number or null values for inputs. \ No newline at end of file diff --git a/exercises/reverse-string/metadata.yml b/exercises/reverse-string/metadata.yml new file mode 100644 index 00000000..5ae26bf0 --- /dev/null +++ b/exercises/reverse-string/metadata.yml @@ -0,0 +1,5 @@ +--- +blurb: "Given a string, reverse it’s letters and output a new string." +title: "reverse-string" +source: "Common introductory challenge to learn loops and strings" +source_url: "https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb" \ No newline at end of file diff --git a/exercises/reverse-string/reverse-string.js b/exercises/reverse-string/reverse-string.js new file mode 100644 index 00000000..f161a16d --- /dev/null +++ b/exercises/reverse-string/reverse-string.js @@ -0,0 +1,15 @@ +// +// This is a stub file for the 'Reverse String' exercise. It's been provided as a +// convenience to get you started writing code faster. +// Make sure to look at reverse-string.spec.js--that should give you some hints about what is +// expected here. + +var ReverseString = function () {}; + +ReverseString.prototype.reverse = function () { +// +// YOUR CODE GOES HERE +// +}; + +module.exports = ReverseString; diff --git a/exercises/reverse-string/reverse-string.spec.js b/exercises/reverse-string/reverse-string.spec.js new file mode 100644 index 00000000..e6cfbbf2 --- /dev/null +++ b/exercises/reverse-string/reverse-string.spec.js @@ -0,0 +1,51 @@ +var ReverseString = require('./reverse-string'); + +describe('ReverseString', function () { + it('no matches', function () { + var subject = new ReverseString('car'); + var matches = subject.matches([ 'girl', 'Japan', 'bear', 'candy']); + + expect(matches).toEqual([]); + }); + + xit('detects simple reverse string', function () { + var subject = new ReverseString('ant'); + var matches = subject.matches(['tna']); + + expect(matches).toEqual(['tna']); + }); + + + + xit('detects anagram', function () { + var subject = new ReverseString('listen'); + var matches = subject.matches(['netsil']); + + expect(matches).toEqual(['netsil']); + }); + + + + xit('detects anagrams case-insensitively', function () { + var subject = new ReverseString('NinJa'); + var matches = subject.matches(['ajnin']); + + expect(matches).toEqual(['ajnin']); + }); + + + + xit('matches() accepts string arguments', function () { + var subject = new ReverseString('ant'); + var matches = subject.matches('tna'); + + expect(matches).toEqual(['tna']); + }); + + xit('matches() accepts single string argument', function () { + var subject = new ReverseString('ant'); + var matches = subject.matches('tna'); + + expect(matches).toEqual(['tna']); + }); +}); \ No newline at end of file From 5eeadc843a806e3d6bbc4d87c1452ac745858647 Mon Sep 17 00:00:00 2001 From: eliaahadi Date: Wed, 11 Oct 2017 23:32:33 -0700 Subject: [PATCH 2/2] added example and deleted 2 files --- exercises/reverse-string/description.md | 1 - exercises/reverse-string/example.js | 14 ++++++++++++++ exercises/reverse-string/metadata.yml | 5 ----- 3 files changed, 14 insertions(+), 6 deletions(-) delete mode 100644 exercises/reverse-string/description.md create mode 100644 exercises/reverse-string/example.js delete mode 100644 exercises/reverse-string/metadata.yml diff --git a/exercises/reverse-string/description.md b/exercises/reverse-string/description.md deleted file mode 100644 index e49b125e..00000000 --- a/exercises/reverse-string/description.md +++ /dev/null @@ -1 +0,0 @@ -With an input string, write code to reverse the string letters and return a string with reversed letters. Be sure to include error handling for an input that isn’t a string such as number or null values for inputs. \ No newline at end of file diff --git a/exercises/reverse-string/example.js b/exercises/reverse-string/example.js new file mode 100644 index 00000000..2b9180d6 --- /dev/null +++ b/exercises/reverse-string/example.js @@ -0,0 +1,14 @@ +'use strict'; + +function reverseString(s) {} + +ReverseString.prototype.convert = function (s) { + + var rString = ''; + for (var i=s.length-1; i>=0; i--){ + rString += s[i]; + } + return rString; +}; + +module.exports = ReverseString; diff --git a/exercises/reverse-string/metadata.yml b/exercises/reverse-string/metadata.yml deleted file mode 100644 index 5ae26bf0..00000000 --- a/exercises/reverse-string/metadata.yml +++ /dev/null @@ -1,5 +0,0 @@ ---- -blurb: "Given a string, reverse it’s letters and output a new string." -title: "reverse-string" -source: "Common introductory challenge to learn loops and strings" -source_url: "https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb" \ No newline at end of file