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

Exercise state list items #19

Open
wants to merge 9 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
3 changes: 3 additions & 0 deletions 6-refactoring/exercises/state/list-items/data/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


export const bulletPoint = '*';
20 changes: 17 additions & 3 deletions 6-refactoring/exercises/state/list-items/src/handler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
import { list } from './utils.js';
import { bulletPoint } from '../data/constants.js';
import { generateList, displayResult, getUserInput } from './utils.js'; // Import utility functions
import { bulletPoint } from './constants.js'; // Import constant bulletPoint

export const listHandler = () => {};
/*export function handleListButtonClick() {
// read & process user input
const allInputs = getUserInput();

// execute core logic
const stringList = generateList(allInputs, bulletPoint);

// communicate result to user
const message = `all items:${stringList}`;
displayResult(message);
}*/
const stringList = list(allInputs);

// Add event listener
document.getElementById('list-them').addEventListener('click', handleListButtonClick);
15 changes: 13 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,15 @@
const bulletPoint = '*'; // data to refactor out of this file
import { setupEventListener } from './listeners.js';

// This is where you would initialize your application
// For example, setting up initial state or performing any setup tasks

// Call the function to set up event listeners
setupEventListener();




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

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

const setupEventListener = () => {
document.getElementById('list-them').addEventListener('click', () => {
const allInputs = getUserInput();
const stringList = generateList(allInputs, bulletPoint);
const message = `All items:${stringList}`;
displayResult(message);
});
}
27 changes: 26 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,29 @@
/**
*
*/
export const list = () => {};
export function generateList(allInputs, bulletPoint) {
let stringList = '';
for (const input of allInputs) {
stringList += `\n${bulletPoint} ${input}`;
}
return stringList;
}

export function displayResult(message) {
alert(message);
}

export function getUserInput() {
const allInputs = [];
let acceptingInput = true;
while (acceptingInput) {
const nextInput = prompt('Enter a list item');
if (nextInput !== null) {
allInputs.push(nextInput);
} else {
acceptingInput = false;
}
}
return allInputs;
}

25 changes: 19 additions & 6 deletions 6-refactoring/exercises/state/list-items/src/util.spec.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,46 @@
import { list } from './util.js';
import { generateList, displayResult, getUserInput } from './util.js'; // Import correct functions from util.js

describe('list: generates a list string from an array of strings', () => {
describe('list: correctly lists items', () => {
it('an empty array returns an empty string', () => {
const expected = '';
const actual = list([]);
const actual = generateList([]); // Call generateList function
expect(actual).toEqual(expected);
});

it('can list a single item', () => {
const expected = '\n- hi';
const actual = list(['hi'], '-');
const actual = generateList(['hi'], '-'); // Call generateList function
expect(actual).toEqual(expected);
});

it('can list many items', () => {
const expected = '\n+ a\n+ b\n+ c\n+ d\n+ e';
const actual = list(['a', 'b', 'c', 'd', 'e'], '+');
const actual = generateList(['a', 'b', 'c', 'd', 'e'], '+'); // Call generateList function
expect(actual).toEqual(expected);
});

it('can use different bullet points', () => {
const expected = '\n* a\n* b\n* c\n* d\n* e';
const actual = list(['a', 'b', 'c', 'd', 'e'], '*');
const actual = generateList(['a', 'b', 'c', 'd', 'e'], '*'); // Call generateList function
expect(actual).toEqual(expected);
});
});

describe('list: generates a list string from an array of strings', () => {
describe('list: uses arguments correctly', () => {
it('does not modify the array argument', () => {
// Mock the prompt function
global.prompt = jest.fn(() => null);

const arg = ['a', 'b', 'c', 'd'];
list(arg);
const result = getUserInput(arg);
expect(arg).toEqual(['a', 'b', 'c', 'd']);

// Remove the mock to avoid affecting other tests
global.prompt.mockRestore();
});
});
});

});
10 changes: 9 additions & 1 deletion 6-refactoring/exercises/state/no-copies/data/state.js
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
export const state = _;

const state = {
data: {
noCopies: [],
}
};

export default state;

14 changes: 12 additions & 2 deletions 6-refactoring/exercises/state/no-copies/src/handler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { saveNoCopies } from './utils.js';
import { saveNoCopies } from './util.js';
import { state } from '../data/state.js';

export const saveNoCopiesHandler = () => {};
export const saveNoCopiesHandler = () => {


// communicate result to user
const message = saveNoCopies();
alert(message);

// log interaction
console.log(data);
};
export default saveNoCopiesHandler;
40 changes: 20 additions & 20 deletions 6-refactoring/exercises/state/no-copies/src/init.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
// data to refactor out of this file
const data = {
noCopies: [],
};
import callEventListener from "./listener.js";

document.getElementById('no-copies-button').addEventListener('click', () => {
// read & process user input
let userInput = null;
while (userInput === null) {
userInput = prompt('enter a string to save');
}
callEventListener();

// execute core logic
const alreadySaved = data.noCopies.includes(userInput);
if (!alreadySaved) {
data.noCopies.push(userInput);
}
// document.getElementById('no-copies-button').addEventListener('click', () => {
// // read & process user input
// let userInput = null;
// while (userInput === null) {
// userInput = prompt('enter a string to save');
// }

// communicate result to user
const message = data.noCopies.join(', ');
alert(message);
// // execute core logic
// const alreadySaved = data.noCopies.includes(userInput);
// if (!alreadySaved) {
// data.noCopies.push(userInput);
// }

// log interaction
console.log(data);
});
// // communicate result to user
// const message = data.noCopies.join(', ');
// alert(message);

// // log interaction
// console.log(data);
// });
13 changes: 12 additions & 1 deletion 6-refactoring/exercises/state/no-copies/src/listener.js
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
import { saveNoCopiesHandler } from './handler.js';
import saveNoCopiesHandler from './handler.js';


const callEventListener = () => {
// document
// .getElementById(TEMPERATURE_INPUT)
// .addEventListener('change', handlers);
document.getElementById('no-copies-button').addEventListener('click', saveNoCopiesHandler);

};

export default callEventListener;
18 changes: 17 additions & 1 deletion 6-refactoring/exercises/state/no-copies/src/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
/**
*
*/
export const saveNoCopies = () => {};
import state from '../data/state.js';

export const saveNoCopies = () => {
let userInput = null;
while (userInput === null) {
userInput = prompt('enter a string to save');
}

// execute core logic
const alreadySaved = state.data.noCopies.includes(userInput);
if (!alreadySaved) {
state.data.noCopies.push(userInput);
}

// communicate result to user
return state.data.noCopies.join(', ');
};
2 changes: 1 addition & 1 deletion 6-refactoring/exercises/state/no-copies/src/util.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { saveNoCopies } from './utils.js';
import { saveNoCopies } from './util.js';

describe('saveNoCopies: ', () => {
describe('adds a new items that are not in the array', () => {
Expand Down
18 changes: 18 additions & 0 deletions exercises/convertTemperature/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Convert Temperatures</title>

<link href="./styles.css" rel="stylesheet" />
</head>
<body>
<div id="user-interface">
<input id="temperatures" type="text" value="" />

<ul id="convertedTemperatures"></ul>
</div>

<script type="module" src="./src/init.js"></script>
</body>
</html>
15 changes: 15 additions & 0 deletions exercises/convertTemperature/src/data/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Events
export const ON_CHANGE = 'change';

// Selectors
export const TEMPERATURES_INPUT = 'temperatures';
export const CONVERT_TEMPERATURE_CONTAINER = 'convertedTemperatures';

// Regex
export const SPACE_REGEX = /\s+/;
export const NUMBERS_SPACES_REGEX = /^[0-9\s]*$/;
export const PLACEHOLDER_REGEX = /{([0-9]+)}/g;

// Messages
export const MESSAGE_ERROR_NOT_INTEGER =
"The '{0}' contains values different of integer numbers";
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {
CONVERT_TEMPERATURE_CONTAINER,
MESSAGE_ERROR_NOT_INTEGER,
} from '../data/constants.js';
import { fahrenheitToCelcius } from '../logic/convert-temperatures.js';
import { parseToNumberArray } from '../utils/parsers.js';
import { replacePlaceholders } from '../utils/strings.js';
import { containsOnlyNumbersAndSpaces } from '../utils/validators.js';

const prepareOutputContainer = () => {
const convertedTemperaturesContainer = document.getElementById(
CONVERT_TEMPERATURE_CONTAINER,
);
convertedTemperaturesContainer.innerHTML = '';
return convertedTemperaturesContainer;
};

const renderOutputContainer = (outputContainer, celsiusList) => {
celsiusList.forEach((celsius) => {
const liString = `<li class="number-item">${celsius.toFixed(2)}</li>`;
outputContainer.innerHTML = outputContainer.innerHTML + liString;
});
};

const validateTextInput = (text) => {
if (!text) {
throw new Error();
}

if (!containsOnlyNumbersAndSpaces(text)) {
throw new Error(replacePlaceholders(MESSAGE_ERROR_NOT_INTEGER, text));
}
};

const convertTemperatures = (fahrenheitTextList) => {
validateTextInput(fahrenheitTextList);
const fahrenheitList = parseToNumberArray(fahrenheitTextList);
const celsiusList = fahrenheitList.map((fahrenheit) => {
return fahrenheitToCelcius(fahrenheit);
});
return celsiusList;
};

/**
* Add an 'change' event listener to the element whose id is 'temperatures'.
*
* @param {Event} event - the 'change' event.
*/
export const convertTemperaturesHandler = (event) => {
try {
// Input
const convertedTemperaturesContainer = prepareOutputContainer();

// Logic
const fahrenheitTextList = event.target.value;
const celsiusList = convertTemperatures(fahrenheitTextList);

// Output
renderOutputContainer(convertedTemperaturesContainer, celsiusList);
} catch (error) {
if (error.message) {
window.alert(error.message);
}
}
};
Loading