Skip to content

SOC implemented #1

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 1 commit 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
5 changes: 5 additions & 0 deletions exercises/convertTemperature/src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const SPACE = /\s+/;
export const TEMPERATURE_INPUT = 'temperatures';
export const CONVERT_TEMPERATURE_CONTAINER = 'convertedTemperatures';
export const MESSAGE_ERROR_NOT_INTEGER =
"The '%S' contains values different of integer numbers";
42 changes: 42 additions & 0 deletions exercises/convertTemperature/src/handlers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
SPACE,
TEMPERATURE_INPUT,
CONVERT_TEMPERATURE_CONTAINER,
MESSAGE_ERROR_NOT_INTEGER,
} from './constants.js';
import convertedTemperature from './utils/utils.js';

const handlers = (event) => {
debugger; // each time the user changes the 'temperatures' input

const convertedTemperaturesContainer = document.getElementById(
CONVERT_TEMPERATURE_CONTAINER,
);

// Erase previous content
convertedTemperaturesContainer.innerHTML = '';

// Get the text input
const fahrenheitTextList = event.target.value;

let convertedTemp = convertedTemperaturesContainer.innerHTML;

// Validate it
if (fahrenheitTextList) {
var convertedTempTextList = convertedTemperature(fahrenheitTextList);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use var. It is not recommeded anymore. Use let or const.

Suggested change
var convertedTempTextList = convertedTemperature(fahrenheitTextList);
const convertedTempTextList = convertedTemperature(fahrenheitTextList);

const convertedTemperaturesLst = '';

const convertedTempList = convertedTempTextList.trim().split(SPACE);

convertedTempList.forEach((celsius) => {
// Render the result
const liString = `<li class="number-item">${celsius}</li>`;

convertedTemp = convertedTemp + liString;
});

convertedTemperaturesContainer.innerHTML = convertedTemp;
}
};

export default handlers;
59 changes: 31 additions & 28 deletions exercises/convertTemperature/src/init.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
debugger; // once when the program is initialized
import listener from './listener.js';

document.getElementById('temperatures').addEventListener('change', (event) => {
debugger; // each time the user changes the 'temperatures' input
listener();

const convertedTemperaturesContainer = document.getElementById(
'convertedTemperatures',
);
// document.getElementById(TEMPERATURE_INPUT).addEventListener('change', (event) => {
// debugger; // each time the user changes the 'temperatures' input

// Erase previous content
convertedTemperaturesContainer.innerHTML = '';
// const convertedTemperaturesContainer = document.getElementById(
// CONVERT_TEMPERATURE_CONTAINER
// );

// Get the text input
const fahrenheitTextList = event.target.value;
// // Erase previous content
// convertedTemperaturesContainer.innerHTML = '';

// Validade it
if (fahrenheitTextList) {
if (!/^[0-9\s]*$/.test(fahrenheitTextList)) {
window.alert(
`The '${fahrenheitTextList}' contains values different of integer numbers`,
);
}
// // Get the text input
// const fahrenheitTextList = event.target.value;

// Sanitize it
const fahrenheitList = fahrenheitTextList.trim().split(/\s+/);
// // Validate it
// if (fahrenheitTextList) {
// if (!/^[0-9\s]*$/.test(fahrenheitTextList)) {
// window.alert(
// MESSAGE_ERROR_NOT_INTEGER,
// );
// }

fahrenheitList.forEach((fahrenheit) => {
// Do the math
const celsius = ((fahrenheit - 32) * 5) / 9;
// // Sanitize it
// const fahrenheitList = fahrenheitTextList.trim().split(SPACE);

// Render the result
const liString = `<li class="number-item">${celsius.toFixed(2)}</li>`;
// fahrenheitList.forEach((fahrenheit) => {
// // Do the math
// const celsius = ((fahrenheit - 32) * 5) / 9;

convertedTemperaturesContainer.innerHTML =
convertedTemperaturesContainer.innerHTML + liString;
});
}
});
// // Render the result
// const liString = `<li class="number-item">${celsius.toFixed(2)}</li>`;

// convertedTemperaturesContainer.innerHTML =
// convertedTemperaturesContainer.innerHTML + liString;
// });
// }
// });
16 changes: 16 additions & 0 deletions exercises/convertTemperature/src/listener.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import handlers from './handlers.js';

import {
SPACE,
TEMPERATURE_INPUT,
CONVERT_TEMPERATURE_CONTAINER,
MESSAGE_ERROR_NOT_INTEGER,
Comment on lines +4 to +7
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this file, only the TEMPERATURE_INPUT is being used. You can remove the others.

Suggested change
SPACE,
TEMPERATURE_INPUT,
CONVERT_TEMPERATURE_CONTAINER,
MESSAGE_ERROR_NOT_INTEGER,
TEMPERATURE_INPUT,

} from './constants.js';

const listener = () => {
document
.getElementById(TEMPERATURE_INPUT)
.addEventListener('change', handlers);
};

export default listener;
24 changes: 24 additions & 0 deletions exercises/convertTemperature/src/utils/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
SPACE,
TEMPERATURE_INPUT,
CONVERT_TEMPERATURE_CONTAINER,
MESSAGE_ERROR_NOT_INTEGER,
} from '../constants.js';

export const convertedTemperature = (fahrenheitTextList) => {
if (!/^[0-9\s]*$/.test(fahrenheitTextList)) {
window.alert(MESSAGE_ERROR_NOT_INTEGER);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You defined MESSAGE_ERROR_NOT_INTEGER constant as The '%S' contains values different of integer numbers.

So, in the window.alert you must replace the %S by fahrenheitTextList content. For example:

window.alert(MESSAGE_ERROR_NOT_INTEGER.replace('%S', fahrenheitTextList));

}

// Sanitize it
const fahrenheitList = fahrenheitTextList.trim().split(SPACE);
var convertedTemperatures = '';
fahrenheitList.forEach((fahrenheit) => {
// Do the math
const celsius = ((fahrenheit - 32) * 5) / 9;

convertedTemperatures = convertedTemperatures + ' ' + celsius.toFixed(2);
});
Comment on lines +15 to +21
Copy link
Owner Author

@dannyelcf dannyelcf May 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use var. It is not recommeded anymore. Use let or const.

Tip: use map and join functions. This will avoid the empty space at the beginning of the returned convertedTemperatures.

Suggested change
var convertedTemperatures = '';
fahrenheitList.forEach((fahrenheit) => {
// Do the math
const celsius = ((fahrenheit - 32) * 5) / 9;
convertedTemperatures = convertedTemperatures + ' ' + celsius.toFixed(2);
});
const convertedTemperatures = fahrenheitList.map((fahrenheit) => {
const celsius = ((fahrenheit - 32) * 5) / 9;
return celsius.toFixed(2);
}).join(' ');

return convertedTemperatures;
};
export default convertedTemperature;
13 changes: 13 additions & 0 deletions exercises/convertTemperature/src/utils/utils.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { convertedTemperature } from './utils.js';

describe('convertedTemperature: fahrenheit converts into celsius', () => {
it('should handle input with only spaces', () => {
const returned = convertedTemperature('32');
expect(returned).toEqual(' 0.00');
});

it('should handle input with only spaces', () => {
const returned = convertedTemperature('32 64 128');
expect(returned).toEqual(' 0.00 17.78 53.33');
});
});