Skip to content

Commit fbaf2ce

Browse files
committed
Add method to not repeat examples
1 parent 9eeb2f3 commit fbaf2ce

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

content/examples.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export default [
22
{
3+
id: 'encrypt',
34
languageId: 'python',
45
format: 'reST',
56
code: `def encrypt_char(c):
@@ -13,6 +14,7 @@ export default [
1314
return ltrs[new_index]`,
1415
},
1516
{
17+
id: 'maximum',
1618
languageId: 'python',
1719
format: 'reST',
1820
code: `def maximum_subarray():
@@ -34,6 +36,7 @@ export default [
3436
return`,
3537
},
3638
{
39+
id: 'TSFirstNode',
3740
languageId: 'typescript',
3841
format: 'JSDoc',
3942
code: `export const getFirstNodeByValue = (node: TreeNode | null, value: string): TreeNode | null => {
@@ -53,6 +56,7 @@ export default [
5356
}`,
5457
},
5558
{
59+
id: 'binarySearch',
5660
languageId: 'javascript',
5761
format: 'JSDoc',
5862
code: `function binarySearch(array, target, start = 0, end = array.length - 1) {
@@ -72,6 +76,7 @@ export default [
7276
};`,
7377
},
7478
{
79+
id: 'orgToken',
7580
languageId: 'javascript',
7681
format: 'Google',
7782
code: `async function getOrganizationFromToken(token) {

pages/index.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ function classNames(...classes: any) {
9393
export default function Example() {
9494
const [code, setCode] = useState('');
9595
const [outputDisplay, setOutputDisplay] = useState('');
96+
const [lastExampleId, setLastExampleId] = useState('');
9697
const [selectedLanguage, setSelectedLanguage] = useState(languagesDropdown[0]);
9798
const [selectedFormat, setSelectedFormat] = useState(formats[0]);
9899
const [commentsEnabled, setCommentsEnabled] = useState(true);
@@ -126,7 +127,8 @@ export default function Example() {
126127
};
127128

128129
const onGenerateExample = () => {
129-
const randomExample = EXAMPLES[Math.floor(Math.random() * EXAMPLES.length)];
130+
const filteredExamples = EXAMPLES.filter((example) => example.id !== lastExampleId);
131+
const randomExample = filteredExamples[Math.floor(Math.random() * filteredExamples.length)];
130132
const foundLanguage = languagesDropdown.find(
131133
(languageOption) => languageOption.id === randomExample.languageId,
132134
);
@@ -138,6 +140,7 @@ export default function Example() {
138140
setSelectedLanguage(foundLanguage);
139141
setSelectedFormat(foundFormat);
140142
setCode(randomExample.code);
143+
setLastExampleId(randomExample.id);
141144
};
142145

143146
const onClickGenerate = async () => {

0 commit comments

Comments
 (0)