Skip to content

exercise type caster done #6

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

Open
wants to merge 3 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: 1 addition & 1 deletion 5-testing-components/examples/3-list-loop/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ export const list = (arr, startNum = 1) => {
liEl.innerText = string;
olEl.appendChild(liEl);
}

return olEl;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const TYPE_USER_ACTION = 'user-action';
export const TYPE_CASTED_VALUE = 'casted-value';
23 changes: 21 additions & 2 deletions 6-refactoring/exercises/dom-and-events/type-caster/src/handler.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
import { typeCaster } from './utils.js';
import { typeCaster } from './util.js';
import { TYPE_USER_ACTION, TYPE_CASTED_VALUE } from '../data/constants.js';
export const castTheValue = (event) => {
debugger;
// read & process user input
const form = event.target.form;
const intendedType = form.type.value;
const stringToCast = form.value.value;

export const castTheValue = () => {};
// execute core logic
let newValue = typeCaster(stringToCast, intendedType);

// communicate result to user
document.getElementById(TYPE_CASTED_VALUE).innerHTML =
typeof newValue + ': ' + newValue;

// log action for developers
console.log('\n-- user action --');
console.log('stringToCast:', stringToCast);
console.log('intendedType:', intendedType);
console.log('newValue:', newValue);
};
11 changes: 8 additions & 3 deletions 6-refactoring/exercises/dom-and-events/type-caster/src/init.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
document.getElementById('user-action').addEventListener('click', (event) => {
import { listeners } from './listener.js';

listeners();

/*document.getElementById(TYPE_USER_ACTION ).addEventListener('click',
(event) => {
debugger;
// read & process user input
const form = event.target.form;
Expand All @@ -18,12 +23,12 @@ document.getElementById('user-action').addEventListener('click', (event) => {
}

// communicate result to user
document.getElementById('casted-value').innerHTML =
document.getElementById(TYPE_CASTED_VALUE).innerHTML =
typeof newValue + ': ' + newValue;

// log action for developers
console.log('\n-- user action --');
console.log('stringToCast:', stringToCast);
console.log('intendedType:', intendedType);
console.log('newValue:', newValue);
});
});*/
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
import { castTheValue } from './handler.js';
import { TYPE_USER_ACTION, TYPE_CASTED_VALUE } from '../data/constants.js';
export const listeners = () => {
document
.getElementById(TYPE_USER_ACTION)
.addEventListener('click', castTheValue);
};
15 changes: 14 additions & 1 deletion 6-refactoring/exercises/dom-and-events/type-caster/src/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
/**
*
*/
export const typeCaster = () => {};

export const typeCaster = (stringToCast, intendedType) => {
let newValue;
if (intendedType === 'string') {
newValue = String(stringToCast);
} else if (intendedType === 'number') {
newValue = Number(stringToCast);
} else if (intendedType === 'boolean') {
newValue = Boolean(stringToCast);
} else {
newValue = undefined;
}
return newValue;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { typeCaster } from './utils.js';
import { typeCaster } from './util.js';

describe('typeCaster: converts values to string, number or boolean', () => {
describe('correctly casts valid types', () => {
Expand Down
4 changes: 3 additions & 1 deletion 6-refactoring/exercises/state/list-items/data/state.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const state = _;
export const state = {
bulletPoint: '*',
};
28 changes: 26 additions & 2 deletions 6-refactoring/exercises/state/list-items/src/handler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
import { list } from './utils.js';
import { list } from './util.js';
import { bulletPoint } from '../data/constants.js';

export const listHandler = () => {};
export const listHandler = () => {
() => {
// read & process user input
const allInputs = [];
let acceptingInput = true;
while (acceptingInput) {
const nextInput = prompt('enter a list item');
if (nextInput !== null) {
allInputs.push(nextInput);
} else {
acceptingInput = false;
}
}

// execute core logic
let stringList = '';
for (const input of allInputs) {
stringList += `\n${bulletPoint} ${input}`;
}

// communicate result to user
const message = `all items:${stringList}`;
alert(message);
};
};
8 changes: 6 additions & 2 deletions 6-refactoring/exercises/state/list-items/src/init.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const bulletPoint = '*'; // data to refactor out of this file
import './listener';

//console.log(state.bulletPoint); // Output: '*'

/*const bulletPoint = '*'; // data to refactor out of this file

document.getElementById('list-them').addEventListener('click', () => {
// read & process user input
Expand All @@ -22,4 +26,4 @@ document.getElementById('list-them').addEventListener('click', () => {
// communicate result to user
const message = `all items:${stringList}`;
alert(message);
});
});*/
2 changes: 2 additions & 0 deletions 6-refactoring/exercises/state/list-items/src/listener.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import { listHandler } from './handler.js';

document.getElementById('list-them').addEventListener('click', listHandler);
10 changes: 9 additions & 1 deletion 6-refactoring/exercises/state/list-items/src/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
/**
*
*/
export const list = () => {};
import { state } from '../data/state.js';

export const listItems = (allInputs) => {
let stringList = '';
for (const input of allInputs) {
stringList += `\n${state.bulletPoint} ${input}`;
}
return stringList;
};
2 changes: 1 addition & 1 deletion 6-refactoring/exercises/state/list-items/src/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('list: generates a list string from an array of strings', () => {
describe('list: uses arguments correctly', () => {
it('does not modify the array argument', () => {
const arg = ['a', 'b', 'c', 'd'];
list(arg);
list(arg, ',');
expect(arg).toEqual(['a', 'b', 'c', 'd']);
});
});
Expand Down