Skip to content
This repository has been archived by the owner on Jan 22, 2024. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mtslck committed Sep 7, 2016
0 parents commit fd03144
Show file tree
Hide file tree
Showing 10 changed files with 360 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org
# Do not exlude this file in your .gitignore

root = true

[*]

# Change these settings to your own preference
indent_style = space
indent_size = 2

# We recommend you to keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
indent_size = 2
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Dependency directory
node_modules/

# Webstorm projektfile
.idea/

# OSX folder options
.DS_Store
3 changes: 3 additions & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"preset": "google"
}
91 changes: 91 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
// JSHint Default Configuration File (as on JSHint website)
// See http://jshint.com/docs/ for more details

"maxerr" : 50, // {int} Maximum error before stopping

// Enforcing
"bitwise" : true, // true: Prohibit bitwise operators (&, |, ^, etc.)
"camelcase" : false, // true: Identifiers must be in camelCase
"curly" : true, // true: Require {} for every new block or scope
"eqeqeq" : true, // true: Require triple equals (===) for comparison
"forin" : true, // true: Require filtering for..in loops with obj.hasOwnProperty()
"freeze" : true, // true: prohibits overwriting prototypes of native objects such as Array, Date etc.
"immed" : false, // true: Require immediate invocations to be wrapped in parens e.g. `(function () { } ());`
"latedef" : false, // true: Require variables/functions to be defined before being used
"newcap" : false, // true: Require capitalization of all constructor functions e.g. `new F()`
"noarg" : true, // true: Prohibit use of `arguments.caller` and `arguments.callee`
"noempty" : true, // true: Prohibit use of empty blocks
"nonbsp" : true, // true: Prohibit "non-breaking whitespace" characters.
"nonew" : false, // true: Prohibit use of constructors for side-effects (without assignment)
"plusplus" : false, // true: Prohibit use of `++` and `--`
"quotmark" : false, // Quotation mark consistency:
// false : do nothing (default)
// true : ensure whatever is used is consistent
// "single" : require single quotes
// "double" : require double quotes
"undef" : true, // true: Require all non-global variables to be declared (prevents global leaks)
"unused" : true, // Unused variables:
// true : all variables, last function parameter
// "vars" : all variables only
// "strict" : all variables, all function parameters
"strict" : true, // true: Requires all functions run in ES5 Strict Mode
"maxparams" : false, // {int} Max number of formal params allowed per function
"maxdepth" : false, // {int} Max depth of nested blocks (within functions)
"maxstatements" : false, // {int} Max number statements per function
"maxcomplexity" : false, // {int} Max cyclomatic complexity per function
"maxlen" : false, // {int} Max number of characters per line
"varstmt" : false, // true: Disallow any var statements. Only `let` and `const` are allowed.

// Relaxing
"asi" : false, // true: Tolerate Automatic Semicolon Insertion (no semicolons)
"boss" : false, // true: Tolerate assignments where comparisons would be expected
"debug" : false, // true: Allow debugger statements e.g. browser breakpoints.
"eqnull" : false, // true: Tolerate use of `== null`
"esversion" : 6, // {int} Specify the ECMAScript version to which the code must adhere.
"moz" : false, // true: Allow Mozilla specific syntax (extends and overrides esnext features)
// (ex: `for each`, multiple try/catch, function expression…)
"evil" : false, // true: Tolerate use of `eval` and `new Function()`
"expr" : false, // true: Tolerate `ExpressionStatement` as Programs
"funcscope" : false, // true: Tolerate defining variables inside control statements
"globalstrict" : false, // true: Allow global "use strict" (also enables 'strict')
"iterator" : false, // true: Tolerate using the `__iterator__` property
"lastsemic" : false, // true: Tolerate omitting a semicolon for the last statement of a 1-line block
"laxbreak" : false, // true: Tolerate possibly unsafe line breakings
"laxcomma" : false, // true: Tolerate comma-first style coding
"loopfunc" : false, // true: Tolerate functions being defined in loops
"multistr" : false, // true: Tolerate multi-line strings
"noyield" : false, // true: Tolerate generator functions with no yield statement in them.
"notypeof" : false, // true: Tolerate invalid typeof operator values
"proto" : false, // true: Tolerate using the `__proto__` property
"scripturl" : false, // true: Tolerate script-targeted URLs
"shadow" : false, // true: Allows re-define variables later in code e.g. `var x=1; x=2;`
"sub" : false, // true: Tolerate using `[]` notation when it can still be expressed in dot notation
"supernew" : false, // true: Tolerate `new function () { ... };` and `new Object;`
"validthis" : false, // true: Tolerate using this in a non-constructor function

// Environments
"browser" : true, // Web Browser (window, document, etc)
"browserify" : false, // Browserify (node.js code in the browser)
"couch" : false, // CouchDB
"devel" : true, // Development/debugging (alert, confirm, etc)
"dojo" : false, // Dojo Toolkit
"jasmine" : false, // Jasmine
"jquery" : false, // jQuery
"mocha" : true, // Mocha
"mootools" : false, // MooTools
"node" : true, // Node.js
"nonstandard" : false, // Widely adopted globals (escape, unescape, etc)
"phantom" : false, // PhantomJS
"prototypejs" : false, // Prototype and Scriptaculous
"qunit" : false, // QUnit
"rhino" : false, // Rhino
"shelljs" : false, // ShellJS
"typed" : false, // Globals for typed array constructions
"worker" : false, // Web Workers
"wsh" : false, // Windows Scripting Host
"yui" : false, // Yahoo User Interface

// Custom Globals
"globals" : {} // additional predefined global variables
}
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# <i class="fa fa-laptop"></i> Buggig kod
<ul class="fa-ul fa-border exercise-info">
<li><i class="fa-li fa fa-signal level-a"></i>B-nivå</li>
<li><i class="fa-li fa fa-github"></i><a href="https://github.com/1dv021/exercise-bugsy.git">https://github.com/1dv021/exercise-bugsy.git</a></li>
</ul>

><i class="fa fa-warning">&nbsp;</i> __VIKTIGT!__ Innan du börjar arbeta med övningsuppgiften är det viktigt att du följer guiden [Att komma igång med en övningsuppgift](../../guider/att-komma-igang-med-en-ovningsuppgift/README.md) för att lägga till övningsuppgiftens repo till ditt repo för övningsuppgifter.
Hämta hem [övningsuppgiftens repo](https://github.com/1dv021/exercise-tiny-funcs.git). Öppna filen `src\bugsy.js` och gör de ändringar som krävs för att koden i funktionen `getGangster`ska testa grönt.

<i class="fa fa-warning">&nbsp;</i> __OBS!__ Projektet innehåller funktioner med kod. Koden i funktionerna, bortsett från funktione `getGangster`, får under inga omständigheter ändras.

## Funktioner

### getGangster

Funktionen är redan implementerad, d.v.s. den innehåller redan kod. Tyvärr är koden inte korrekt i alla avseenden varför den inte klarar testerna. Din uppgift är att skriva om koden i funktionen så att den klarar samtliga tester.

Denna funktion parameter i form av en sträng innehållande initialer tillhörande en misstänkt gangster. Funktionen med hjälp av initialerna leta reda på gangsterns fullständiga namn och returnera det. Hittas inget namn som matchar initialerna ska funktionen returnera strängen `'No suspect found!'`.

Till din hjälp finns i namnrymden `MostWanted` två funktioner `getNames` och `getInitials` som beskrivs nedan.

### MostWanted.getNames

Funktionen returnerar en så kallad _generator_-objekt, som ger namn för namn från en lista gangstrars namn med hjälp av metoden `next()` och egenskapen `value`.

```
let MostWanted = require('./MostWanted');
let names = MostWanted.getNames(); // ger generator-objektet med alla namn
let name = names.next().value; // ger det första namnet 'Bugsy Malone'
name = names.next().value; // ger det andra namnet 'Frank Nitti'
...
name = names.next().value; // finns det inga fler namn returneras undefined
```

### MostWanted.getInitials

Funktionen returnerar ett fullständigt namns initialer.

```
let MostWanted = require('./MostWanted');
let name = 'Ellen Nu';
let initials = MostWanted.getInitials(name); // ger 'EN'
```

## <i class="fa fa-lightbulb-o"></i> Tips
Genom att köra testerna som kommer med övningsuppgiften kan du undersöka om koden du skrivit löst uppgiften (i alla fall enligt testerna...).

Funktioner, metoder, etc. som *kan* komma till användning beroende hur du väljer att lösa uppgiften.

- [while](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while)
- [if...else](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else)
- ([for...of](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of))

## <i class="fa fa-flask"></i> Lösningsförslag
<ul class="fa-ul fa-border exercise-info">
<li><i class="fa-li fa fa-github"></i><a href="https://github.com/1dv021/exercise-solution-proposals/tree/master/part-1/bugsy">https://github.com/1dv021/exercise-solution-proposals/tree/master/part-1/bugsy</a></li>
</ul>
17 changes: 17 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Starting point of the application.
*
* @author Mats Loock
* @version 1.0.1
*/

'use strict';

let bugsy = require('./src/bugsy');

try {
let result = bugsy.getGangster('AC');
console.log(result);
} catch (e) {
console.error('ERROR:' , e.message);
}
45 changes: 45 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "exercise-bugsy",
"version": "1.0.0",
"main": "app.js",
"homepage": "http://coursepress.lnu.se/kurs/grundlaggande-programmering/",
"contributors": [
{
"name": "John Häggerud",
"email": "[email protected]"
},
{
"name": "Johan Leitet",
"email": "[email protected]"
},
{
"name": "Jacob Lindehoff",
"email": "[email protected]"
},
{
"name": "Mats Loock",
"email": "[email protected]"
}
],
"license": "CC-BY-4.0",
"repository": {
"type": "git",
"url": "https://github.com/1dv021/exercise-bugsy.git"
},
"engines": {
"node": ">=0.12.0"
},
"dependencies": {},
"devDependencies": {
"chai": "^3.5.0",
"jscs": "^3.0.7",
"jshint": "^2.9.3",
"mocha": "^3.0.2"
},
"directories": {
"test": "test"
},
"scripts": {
"test": "mocha --recursive ./test"
}
}
40 changes: 40 additions & 0 deletions src/MostWanted.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

let MostWanted = (function() {

return {

/**
*
*/
getNames: function*() {
yield 'Bugsy Malone';
yield 'Frank Nitti';
yield 'Stephanie St. Clare';
yield 'Al Capone';
yield 'Helen Gills';
yield 'Johnny Torrio';
yield 'Bonnie Parker';
},

/**
* Returns the initials from a name.
*
* @param {String} name The name to get the initials from.
* @returns {String}
*/
getInitials: function(name) {
if (typeof name === 'string' && !name.trim() ||
typeof name === 'undefined' ||
name === null) {
throw new Error('name must be a string that\'s not empty.');
}

return name.split(' ').reduce(function(prev, next) {
return prev += next.charAt(0);
}, '');
}
};
})();

module.exports = MostWanted;
39 changes: 39 additions & 0 deletions src/bugsy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Module for searching for a gangster.
*
* @author John Häggerud
* @author Mats Loock
* @version 1.0.1
*/

'use strict';

let MostWanted = require('./MostWanted');

/**
* Returns the first gangster whoes full name matching the initials.
*
* @param {String} initials The initials of the full name to be found.
* @returns {String} The first full name found matching the initials.
*/
let getGangster = function(initials) {

// TODO: Fix the bug(s) in this function to get the tests to pass.

let names = MostWanted.getNames();
let theSuspect;
let name;
let initialsOfTheName;

do {
name = names.next().value;
initialsOfTheName = MostWanted.getInitials(name);
if (initials === initialsOfTheName) {
theSuspect = name;
}
} while (name);

return theSuspect;
};

module.exports.getGangster = getGangster;
36 changes: 36 additions & 0 deletions test/bugsy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Tests for the bugsy module.
*
* @author John Häggerud
* @author Mats Loock
* @version 1.0.1
*/

'use strict';

let bugsy = require('../src/bugsy');
let expect = require('chai').expect;

describe('Test the function getGangster', function() {

it('getGangster(\'AC\'); should return \'Al Capone\'', function(done) {
expect(bugsy.getGangster('AC')).to.eql('Al Capone');
done();
});

it('getGangster(\'SSC\'); should return \'Stephanie St. Clare\'', function(done) {
expect(bugsy.getGangster('SSC')).to.eql('Stephanie St. Clare');
done();
});

it('getGangster(\'FN\'); should return \'Frank Nitti\'', function(done) {
expect(bugsy.getGangster('FN')).to.eql('Frank Nitti');
done();
});

it('getGangster(\'ÖÖ\'); should return \'No suspect found!\'', function(done) {
expect(bugsy.getGangster('ÖÖ')).to.eql('No suspect found!');
done();
});

});

0 comments on commit fd03144

Please sign in to comment.