-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconverter.js
More file actions
69 lines (64 loc) · 2.68 KB
/
converter.js
File metadata and controls
69 lines (64 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'use strict'
let btn_convert = document.querySelector('.app__btn-convert');
btn_convert.addEventListener('click', convert);
function convert() {
// Declare obj for input value and further output value
let io = {
input: document.querySelector('.app__input').value,
output: ''
}
// Get main element
let app = document.querySelector('.app');
// Delete previous result
if (document.querySelector('.app-table')) document.querySelector('.app-table').remove();
if (document.querySelector('.app__output')) document.querySelector('.app__output').remove();
// Coin counting function. Splite cents into different coins
function split_by_coins(number) {
let penny, nickel, dime, quarter, balance;
quarter = Math.floor(number / 25);
balance = number % 25;
dime = Math.floor(balance / 10);
balance = balance % 10;
nickel = Math.floor(balance / 5);
balance = balance % 5;
penny = Math.floor(balance / 1);
return `<table class="app-table">
<tr class="app-table__row">
<td class="app-table__cell">Coins</td>
<td class="app-table__cell">Count</td>
</tr>
<tr class="app-table__row">
<td class="app-table__cell">Quarters (25 ¢)</td>
<td class="app-table__cell">${quarter}</td>
</tr>
<tr class="app-table__row">
<td class="app-table__cell">Dimes (10 ¢)</td>
<td class="app-table__cell">${dime}</td>
</tr>
<tr class="app-table__row">
<td class="app-table__cell">Nickels (5 ¢)</td>
<td class="app-table__cell">${nickel}</td>
</tr>
<tr class="app-table__row">
<td class="app-table__cell">Pennies (1 ¢)</td>
<td class="app-table__cell">${penny}</td>
</tr>
</table>`;
}
// Check input value. If input value not number or negative number
if (!(Number.isFinite(+io.input)) || io.input.indexOf('-') != -1) io.output = `<p class="app__output">This is incorrect value</p>`;
else {
// Declare variable for cents
let cents;
// If input value not integer, but the balance is over 99 cents
if (io.input.indexOf('.') != -1 && io.input.indexOf('.') < io.input.length-3) {
io.output = `<p class="app__output">This is incorrect value</p>`;
} else {
// Convert dollars to cents. Math.round() used for rounding numbers 0.55 - 0.58
cents = Math.round(+io.input * 100);
io.output = `<p class="app__output">Total cents: ${cents}</p>` + split_by_coins(cents);
}
}
// Render output
app.insertAdjacentHTML('beforeend', io.output);
}