-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
86 lines (69 loc) · 2.78 KB
/
script.js
File metadata and controls
86 lines (69 loc) · 2.78 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
function getRndInteger(min, max) {
return Math.floor(Math.random() * (Number(max) - Number(min) + 1) + Number(min));
}
const createRandomNumbers = (min, max) => {
const randomNumbers = new Set()
const range = max - min + 1
while (randomNumbers.size < range) {
randomNumbers.add(Math.floor(Math.random() * range))
}
return [...randomNumbers]
}
const createList = (li) => {
if (document.querySelector('li') !== null) {
const OL = document.querySelector('OL');
OL.remove();
}
const getDivOutput = document.querySelector('.output');
const OL = document.createElement('OL');
getDivOutput.appendChild(OL);
const ols = document.querySelector('OL');
li.forEach(element => {
const LI = document.createElement('LI');
LI.innerHTML = element;
ols.appendChild(LI);
});
}
const removeSpaceOnString = (arraypal) => {
const stringsWithSpaceRemoved = arraypal.map(cadaum => {
return cadaum.trim();
});
return stringsWithSpaceRemoved;
};
const checkDuplicity = (arraypal) => {
const stringsWithSpaceRemoved = removeSpaceOnString(arraypal);
const newArrayWithUniqueElements = stringsWithSpaceRemoved.filter((element, index) => {
return stringsWithSpaceRemoved.indexOf(element) === index;
});
return newArrayWithUniqueElements;
}
function checkEmptyElement(itensOnArray) {
const newArrayWithOutEmptyElements = itensOnArray.filter((element) => element !== '');
return newArrayWithOutEmptyElements;
}
const randomizer = () => {
const contentText = document.querySelector('#w3review').value;
const itensOnArray = contentText.split('\n');
const newArrayWithOutEmpyElement = checkEmptyElement(itensOnArray);
const newArrayWithOutDuplicity = checkDuplicity(newArrayWithOutEmpyElement)
const arrayWithRandomicNumbers = createRandomNumbers(0, newArrayWithOutDuplicity.length - 1);
const contentTextRandomized = arrayWithRandomicNumbers.map((number) => {
return newArrayWithOutDuplicity[number];
});
createList(contentTextRandomized);
}
function generateRandomNumber() {
const fisrtNumber = document.querySelector('#firstNumber').value;
const secondNumber = document.querySelector('#secondNumber').value;
if (!Number.isInteger(parseInt(fisrtNumber)) || !Number.isInteger(parseInt(secondNumber))) {
alert('Entre somente com números inteiros.')
} else {
const randomNumber = getRndInteger(fisrtNumber, secondNumber);
document.querySelector('.outputNumber').innerText = '';
document.querySelector('.outputNumber').innerText = randomNumber;
}
};
const randomizerButton = document.querySelector('#submit');
randomizerButton.addEventListener('click', randomizer);
const generateRandomNumberButton = document.querySelector('.aleatory-number input[value=Sortear]');
generateRandomNumberButton.addEventListener('click', generateRandomNumber)