-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
85 lines (78 loc) · 2.65 KB
/
main.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const {
getSizeOfBook,
getHeight,
getQuantity,
getWidthPerBookPerPc,
getHeightPerBookPerPc,
} = require('./helpers')
const { MATERIAL } = require('./data')
const {
languageResult,
updatePageLanguage,
populateLanguageSelector,
} = require('./translation')
populateLanguageSelector()
document.getElementById('needInMeter').style.visibility = 'hidden'
const languageSelector = document.getElementById('languageSelector')
languageSelector.addEventListener('change', (event) => {
const { value: language } = event.target
console.log('😸😸😸😸😸😸 Change language', { language })
updatePageLanguage(language)
})
updatePageLanguage(languageSelector.value)
//How many pieces of sheets the customer needs to order
const getNeedInSheets = () => {
let result = Math.round(
1.1 *
Math.ceil(
+getQuantity() / (+getHeightPerBookPerPc() * +getWidthPerBookPerPc())
)
)
return result
}
// How many meters the customer needs to order
const getNeedInMeter = () => {
let needRunningmeter_plus10percent = Math.ceil(
(1.1 * (+getQuantity() / +getWidthPerBookPerPc()) * (+getHeight() + 2)) /
100
)
return needRunningmeter_plus10percent
}
const chooseSize = document.getElementById('sizeofbook')
chooseSize.addEventListener('change', getSizeOfBook)
// Final function to populate the result field
const calculate = () => {
const material = document.getElementById('typeOfMaterial').value
const language = languageSelector.value
let result
if (getNeedInMeter() === Infinity || getNeedInSheets() === Infinity) {
document.getElementById('needInMeter').style.visibility = 'hidden'
alert(`${languageResult[language].alert}`)
} else {
switch (material) {
case MATERIAL.SHEET_LONGGRAIN:
case MATERIAL.SHEET_SHORTGRAIN:
{
// It calculates the need for sheets if sheets are selected
document.getElementById('needInMeter').style.visibility = 'visible'
result = `${
languageResult[language].resultText
} ${getNeedInSheets()} ${languageResult[language].resultPieces}`
}
break
default:
{
// It calculates the need for running meter if a specific material is selected
document.getElementById('needInMeter').style.visibility = 'visible'
result = `${
languageResult[language].resultText
} ${getNeedInMeter()} ${languageResult[language].resultMeter}`
}
break
}
}
document.getElementById('needInMeter').value = result
console.log(`The result is: ${result}`)
}
const calculateBtn = document.querySelector('#calculate')
calculateBtn.addEventListener('click', calculate)