-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcodex.html
115 lines (97 loc) · 4.46 KB
/
codex.html
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
<!--
We're not actually using this for anything but were curious to try OpenAI's
Codex on the problem of making an interactive spinner. It fell on its face the
first couple things we tried but when we tried this:
"Make a spinner that when you click a button spins the spinner and it lands on
either YES or NO"
it generated the code in this file, which is kind of a version of that
sentence.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="icon" href="https://cdn.glitch.global/e605b8ff-1b81-42a6-81a5-17eabcdd19d1/slot-machine-32.ico?v=1666412745792"/>
<title>OpenAI's Codex Made This</title>
</head>
<body>
<script>
var spinner = document.createElement('div');
spinner.innerHTML = '<button>Spin</button>';
spinner.style.border = '1px solid black';
spinner.style.width = '100px';
spinner.style.height = '100px';
spinner.style.display = 'inline-block';
spinner.style.textAlign = 'center';
spinner.style.verticalAlign = 'middle';
spinner.style.lineHeight = '100px';
spinner.style.fontSize = '20px';
spinner.style.fontWeight = 'bold';
spinner.style.borderRadius = '50%';
spinner.style.boxShadow = '0 0 10px black';
spinner.style.cursor = 'pointer';
spinner.style.userSelect = 'none';
spinner.style.webkitUserSelect = 'none';
spinner.style.mozUserSelect = 'none';
spinner.style.msUserSelect = 'none';
spinner.style.oUserSelect = 'none';
spinner.style.transition = 'all 0.5s';
spinner.style.webkitTransition = 'all 0.5s';
spinner.style.mozTransition = 'all 0.5s';
spinner.style.msTransition = 'all 0.5s';
spinner.style.oTransition = 'all 0.5s';
spinner.style.transform = 'rotate(0deg)';
spinner.style.webkitTransform = 'rotate(0deg)';
spinner.style.mozTransform = 'rotate(0deg)';
spinner.style.msTransform = 'rotate(0deg)';
spinner.style.oTransform = 'rotate(0deg)';
spinner.onclick = function() {
var random = Math.random();
var degrees = random * 360;
spinner.style.transform = 'rotate(' + degrees + 'deg)';
spinner.style.webkitTransform = 'rotate(' + degrees + 'deg)';
spinner.style.mozTransform = 'rotate(' + degrees + 'deg)';
spinner.style.msTransform = 'rotate(' + degrees + 'deg)';
spinner.style.oTransform = 'rotate(' + degrees + 'deg)';
if (random < 0.5) {
spinner.innerHTML = 'YES';
} else {
spinner.innerHTML = 'NO';
}
};
document.body.appendChild(spinner);
</script>
</body>
</html>
<!-- PS: The above was before ChatGPT.
Check out what ChatGPT can do, as of 2022-12-03:
Prompt: Write a Javascript function that takes an array of strings and list of non-negative weights and returns a random element from the list of strings, weighted by the weights. For example, if the strings are ["apple", "banana", "cherry"] and the weights are [1, 1, 1] then it returns either "apple" or "banana" or "cherry" with equal probability. If the weights were [1, 2, 1] then it would return "banana" with 50% probability and the other two with 25% probability each.
Answer: Here is one way to implement a function that takes an array of strings and an array of weights and returns a random element from the list of strings, weighted by the weights:
function weightedRandom(strings, weights) {
// make sure the arrays are not empty and have the same number of elements
if (strings.length === 0 || weights.length === 0 || strings.length !== weights.length) {
return null;
}
// generate a random number between 0 and the sum of the weights
let totalWeight = weights.reduce((acc, weight) => acc + weight, 0);
let random = Math.random() * totalWeight;
// find the string with the corresponding weight
let weightSum = 0;
for (let i = 0; i < strings.length; i++) {
weightSum += weights[i];
if (random < weightSum) {
return strings[i];
}
}
// if no string is found, return null
return null;
}
/* Here is an example of how to use this function: */
let strings = ["apple", "banana", "cherry"];
let weights = [1, 1, 1];
let randomString = weightedRandom(strings, weights);
console.log(randomString); // prints "apple" or "banana" or "cherry" with equal probability
ChatGPT continues:
Note that the function uses `Math.random()` to generate a random number between 0 and 1, which it then scales by the sum of the weights to get a random number between 0 and the sum of the weights. It then iterates over the strings and weights arrays and returns the string with the corresponding weight when the random number is less than the sum of the weights up to that point.
-->