Will AI steal developers' job? We tested ChatGPT into generating a simple JS script.

George PerdikasGeorge Perdikas
3 min read

Objective

To evaluate ChatGPT's capability in generating functional code by asking it to write a JavaScript script that counts vowels in a given string input.

Methodology

The test involved:

  • Asking ChatGPT to produce a JavaScript function that counts vowels (a, e, i, o, u, case-insensitive) in a string.

  • Testing the generated code using OneCompiler, with various input strings to verify correctness.

Expected Behavior

  • ChatGPT was expected to: - Generate a JavaScript code snippet that counts vowels accurately.

  • Handle different input variations including mixed case letters, strings with numbers or symbols, strings without vowels

  • Return accurate counts for all test cases.


The Generated JavaScript Code

function countVowels(inputString) {

// Define a regex to match vowels (both lowercase and uppercase)
const vowels = /[aeiouAEIOU]/g;

// Use match to find all vowels and return the count
const matches = inputString.match(vowels);

// If there are no vowels, matches will be null
return matches ? matches.length : 0; }

// Example usage:
const testString = "Hello, World!";
const vowelCount = countVowels(testString);
console.log(Number of vowels in "${testString}":, vowelCount); ```


How this works

The expression /[aeiouAEIOU]/g ensures that all uppercase and lowercase vowels in the string will be counted. `.match()` returns an array of matched vowels, or null if no vowels are found, so "0" result will be outputed if there are no vowels.


Test results

Following format is (test case / expected vowel count / actual vowel account).
"Hello, World!" / 3 / 3
"HELLO WORLD!" / 3 / 3
"HeLlO wOrLd" / 3 / 3
"helloworld" / 3 / 3
"hellou world" / 4 / 4
"1234567890" / 0 / 0
"123456789O" / 1 / 1
"one two" / 3 / 4

Only on last case ChatGPT made a mistake. It should return three vowels but instead the function did not return the correct count.

Note : Given that on starting prompt it was defined if the script should count the sum on the vowels, how many vowels were represented on the string, on all cases (except the 2 with arithmetical string) 2 instead of 3, and 3 instead of 4, would also be a valid answer.


Conclusion

ChatGPT generated a JavaScript function to count vowels, that could run properly, without errors of failing on the code itself. On most of the cases the function handled correctly diverse input strings, including those with uppercase letters, mixed cases, and numeric characters.

For the input "one two", the function returned 4 vowels instead of the correct count of 3 (o, e, o). This discrepancy suggests a possible testing or data recording error, as the function itself, based on regex matching, should correctly count the total number of vowels.

Summing it up, the generated code is functional but on one case there was an evaluation error. A clearer prompt on counting total vowels or unique vowels could not affect the result, given that in this case, the problem was not a repeated vowel.

AI is probably not ready to steal developers' jobs. If nothing else, needs a dev to double-check some of its math...

0
Subscribe to my newsletter

Read articles from George Perdikas directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

George Perdikas
George Perdikas