-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
127 lines (107 loc) · 3.99 KB
/
script.js
File metadata and controls
127 lines (107 loc) · 3.99 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const coinflipper = (coins) => {
const first = "heads";
const second = "tails";
let arrayOne = [];
let arrayTwo = [];
const inputElement = document.getElementById('inputValue');
const inputValue = inputElement?.value || '0';
const placeHolder = document.getElementById('place')
if (coins == 1) {
for (let index = 1; index <= inputValue; index++) {
let coolNumber = Math.floor(Math.random() *2) + 1 ;
if (coolNumber === 1) {
arrayOne.push(first)
} else {
arrayOne.push(second)
}
}
console.log(arrayOne);
placeHolder.innerHTML = arrayOne;
} else if (coins == 2) {
for (let index = 1; index <= inputValue; index++) {
arrayTwo = [];
for (let index = 0; index <= 2; index++) {
let coolNumber = Math.floor(Math.random() *2) + 1 ;
if (coolNumber === 1) {
if (index == 1) {
arrayTwo.push(`(${first}`)
} else if (index == 2) {
arrayTwo.push(`${first})`)
}
} else {
if (index == 1) {
arrayTwo.push(`(${second}`)
} else if (index == 2) {
arrayTwo.push(`${second})`)
}
}
if (arrayTwo.length > 1) {
break;
}
}
arrayOne.push(arrayTwo);
}
console.log(arrayOne);
placeHolder.innerHTML = arrayOne;
};
};
//array to store logs
let logs = [];
// override console.log to log and capture logs
function captureLogs(includeDetails = false) {
const originalLog = console.log;
console.log = function (message) {
if (logs.length === 0) {
// add timestamp at the beginning of the logs array
logs.push(
`COIN FLIP RESULTS\n`
);
}
// try to access the stackdetails for more info (optional)
let logDetails = "";
if (includeDetails) {
try {
throw new Error();
} catch (e) {
const stackLines = e.stack.split("\n");
if (stackLines.length >= 3) {
const logLocation = stackLines[2].trim();
logDetails = logLocation;
}
}
}
// add log message with optional file and line number details as new line
//logs.push(`${message}\n ${logDetails ? logDetails : ""}`);
// test formatting
logs.push(
`${message.toString().padEnd(60, ".")}${
logDetails ? logDetails.padStart(60) : ""
}`
);
originalLog.apply(console, arguments);
};
}
// I stole this
function downloadLogs() {
const logsString = logs.join("\n"); // join logs with newlines
// create a blob
const blob = new Blob([logsString], { type: "text/plain" });
// create a link
const link = document.createElement("a");
link.href = window.URL.createObjectURL(blob);
link.download = "COINFLIPRESULTS.txt"; // default filename
document.body.appendChild(link);
link.click();
// clean up => remove the link after usage
document.body.removeChild(link);
}
//USAGE EXAMPLE
// call captureLogs to start capturing console.log from here
//captureLogs(); // without details
captureLogs(true); // WITH details (filename/lines if accessible)
// button to trigger download - or call downloadLogs()
const downloadButton = document.createElement("button");
downloadButton.textContent = "Download Logs";
downloadButton.id = 'position2'
downloadButton.addEventListener("click", downloadLogs);
document.body.appendChild(downloadButton);