Javascript  

 

 

 

 

 

Failure Cases

 

Even though I admit (actually very impressed) that chatGPT can help a lot with coding, it has not always been successful to get the code that I liked. With multiple atempt that failed, I thought that it would be good idea to keep track of such a failure cases as well and try it later when new AI or upgraded version comes out.

 

 

 

Simple Preprocessor

 

I tried to get a simple code to function like a preprocessor in C language. I didn't expect to get a fully functioning preprocessor. I just wanted to get a simple preprocess that can handle the directive [#if  #elif  #endif] and [#define]. The GUI part and overal code structure that I got from chatGPT was pretty similar to what I expected. But the implementation of stripIf(str) was not good at all.  It was far behind from the expectation and was not able to improve it to the code functioning as expected.

 

This was tried on Feb 9 2023.

Write a javascript and html with the following requirement

1. Create a table with three rows and one column at the center of the page. The width of the table is 600 pixel. The grid line of the table should be shown

2. in the first row put a text field named inputText with width same as table and height is 20 lines. The default text is "#define c1 1 \n#define c2 0 \n#if c1 \n   a=1\n   b=1\n#elif\n   a=0\n   b=0\n#endif"

3. in the second row put a button named stripIf

4. in the thrid row, put a text field named outputText with width same as table and height is 10 lines.

5. implement a function named stripIf(str). The function takes in a string str and extracts only the parts where '#if true' (i.e, #if 1) and return it. The function should understand "#define" and apply it when checking "#if" condition.

6. When the stripIf button is clicked, pass the text of inputText to stripIf(str) and print the output of the function to outputText  

7. do not use <style> tag

8. Write html and javascript in separate file

9. Define all the callback functions separately and let them be called from addEventListener

 

I revised the initial prompt a little bit as follows. I wanted to check if chatGPT can understand a generic concept as highlighed in red below as apposed to my own description, but result was even worse. It removed the whole #if blocks regardless of whether the condition is true or not.

Write a javascript and html with the following requirement

1. Create a table with three rows and one column at the center of the page. The width of the table is 600 pixel. The grid line of the table should be shown

2. in the first row put a text field named inputText with width same as table and height is 20 lines. The default text is "#define c1 1 \n#define c2 0 \n#if c1 \n   a=1\n   b=1\n#elif\n   a=0\n   b=0\n#endif"

3. in the second row put a button named stripIf

4. in the thrid row, put a text field named outputText with width same as table and height is 10 lines.

5. implement a function named stripIf(str). The function takes in a string str and preprocess C language like directives and return the preprocessed string

6. When the stripIf button is clicked, pass the text of inputText to stripIf(str) and print the output of the function to outputText  

7. do not use <style> tag

8. Write html and javascript in separate file

9. Define all the callback functions separately and let them be called from addEventListener