Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

thoni_r ex. 1-7 #24

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ bower_components

# secret results ;-)
browser/lessons/xxx

/.idea
18 changes: 14 additions & 4 deletions browser/lessons/lesson_01-base/lexical-analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @return {Array}
*/
export function tokenize(str) {
// Write code here so that it passes the tests
return str.trim().replace(/ +/g, " ").split(" ");
}

/** stem a string = turn several variants into the same
Expand All @@ -19,7 +19,7 @@ export function tokenize(str) {
* @return {String}
*/
export function stem(str) {
// Write code here so that it passes the tests
return str.toLowerCase();
}

/** parse a string into a list of stemmed token
Expand All @@ -29,7 +29,7 @@ export function stem(str) {
* @return {Array}
*/
export function parse(str) {
// Write code here so that it passes the tests
return tokenize(stem(str));
}

/** index a string into a hash {'token' : <frequency of appearance>}
Expand All @@ -38,7 +38,17 @@ export function parse(str) {
* @return {Object}
*/
export function index(str) {
// Write code here so that it passes the tests
var obj = {};
parse(str).forEach(function(token)
{
if (obj[token]) {
++obj[token];
}
else {
obj[token] = 1;
}
});
return obj;
}


Expand Down
2 changes: 1 addition & 1 deletion browser/lessons/lesson_02-chrome-dev-tools/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ context('[Lesson 2]', function () {
// what ????
// TODO understand

let copy = ( object ); // TODO change this line to use the cloneDeep() function
let copy = cloneDeep( object ); // TODO change this line to use the cloneDeep() function
object.foo.bar = 33;

expect(object).to.have.deep.property('foo.bar', 33);
Expand Down
11 changes: 10 additions & 1 deletion browser/lessons/lesson_03-type_detection/boolean-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@
* @return {Boolean}
*/
export default function convertToBoolean(value) {
// TODO write the function so it passes the tests below !
if (typeof value === 'boolean') {
return value;
}
if (value instanceof Boolean) {
return value == true;
}
if (typeof value === 'number') {
return value == 1;
}
return value == "true";
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ context('[Lesson 3]', function () {
});

// Too hard ;-) but bonus points if you do it
it.skip('should be able to do a correct conversion to boolean (round 4)', function () {
it('should be able to do a correct conversion to boolean (round 4)', function () {
const TEST_CASES = [
{
input: 0,
Expand Down
44 changes: 32 additions & 12 deletions browser/lessons/lesson_04-logger/logger.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
import _ from 'lodash';

function createFancyLogger(id) {
id = (id || 'default').toUpperCase();

function logBetter(level) {
const originalArgs = Array.from(arguments);

// TODO implement !
let newArgs = originalArgs;
// TODO....
if (id) {
id = id.toUpperCase();
}
else {
id = "default";
}

/* eslint-disable no-undefined */
return {
log: undefined,
info: undefined,
warn: undefined,
error: undefined,
log: function(arg)
{
const originalArgs = Array.from(arguments);

let newArgs = originalArgs;
newArgs[0] = getTimestamp() + " - " + id + " - ";
if (typeof arg === 'object') {
newArgs.push(arg);
}
else {
newArgs[0] += arg;
}

console.log.apply(console, newArgs);
},
info: function(arg)
{
console.info(getTimestamp() + " - " + id + " - " + arg);
},
warn: function(arg)
{
console.warn(getTimestamp() + " - " + id + " - " + arg);
},
error: function(arg)
{
console.error(getTimestamp() + " - " + id + " - " + arg);
}
};
}

Expand Down
11 changes: 10 additions & 1 deletion browser/lessons/lesson_05-timeouts/debounce.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@

export default function debounce(callback, waitMs) {

var id = null;

return function() {
// ...
var args = arguments;
if (id) {
clearTimeout(id)
}
id = setTimeout(function()
{
callback.apply(window, args);
}, waitMs);
};
}

Expand Down
5 changes: 4 additions & 1 deletion browser/lessons/lesson_06-wrapping_up/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ function updateResults() {

let index = LexicalAnalyser.index(text);

let elements;
let elements = Object.keys(index).map(function(idx)
{
return '<tr><td>' + idx + '</td><td>' + index[idx] + '</td></tr>';
});
/* TODO
elements = [
'<tr><td>Hello</td><td>2</td></tr>',
Expand Down
18 changes: 15 additions & 3 deletions browser/lessons/lesson_07-async-callback/find-agency.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@ import tryUntil from './try-until';

export default function findAgency (DB, image, cb) {
tryUntil([
// TODO find by agencyName
// TODO find by credit
// TODO fallback to 'Unknown'
// TODO find by agencyName
function(cb)
{
DB.findAgency({name: image.agencyName}, cb);
},
// TODO find by credit
function(cb)
{
DB.findAgency({name: image.credit}, cb);
},
// TODO fallback to 'Unknown'
function(cb)
{
DB.findAgency({name: 'Unknown'}, cb);
}
], cb);
}
19 changes: 19 additions & 0 deletions browser/lessons/lesson_07-async-callback/try-until.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@

export default function tryUntil(candidateFnArray, callback) {
/* ... */
var i = 0;
function test()
{
candidateFnArray[i](function(err, res)
{
if (err || res) {
callback(err, res);
}
else {
if (++i < candidateFnArray.length) {
test();
}
else {
callback(null, null);
}
}
})
}
test();
}