ChatGPT can teach you how to code
ChatGPT can teach you JavaScript, and I'm going to show you how
I hope you havenāt forgotten me!
Itās WellPaidGeek from Twitter. I havenāt sent an email out in over a year, but now I have switched to Substack (all subscribers moved across) and will be sending out emails covering learning to code and maximising your tech career, with a focus on JavaScript. And maybe TypeScript, if Iām feeling adventurous.
Anyway, intro out of the way, letās talk ChatGPT. It has been the talk of the internet recently. If you havenāt heard about it, it is a language based AI. This basically means it can understand human language, which makes it able to answer questions, almost as well as a human. Itās been loaded up with what seems to be all of the information on the internet, meaning itās an expert on wellā¦ most things. It works by providing responses to āpromptsā given to it by you. Prompts are basically questions, or instructions.
TLDR; ChatGPT is an API you can ask questions, and it comes up with really good answers on a wide range of topics. Check it out:
You can ask it anything from āWhen did World War II beginā to, āWrite 2000 words on the benefits of capitalismā to āGive me the HTML and CSS for the about me page of a portfolio siteā and it comes up with pretty good responses for all of those.
Recently, Iāve been using it to help me learn to program in Java (I have been doing Java*Script* for many years, now itās time to tackle backend programming in Java).
The reason itās able to teach you to code and is better than googling for this purpose is:
It can give you very specific code, rather than getting more or less code than you need when you google
You just get the info you want, you donāt have to wade through extraneous info like with a webpage in Google search results
It understands the code it gives you so well it can explain it to you. This part is huge. It can break down step by step what the code is doing, just like a tutor. Take that google!
Now Iāll show you how ChatGPT can teach you JavaScript, but it can be applied to any coding language.
Firstly, ChatGPT cannot teach you to code on itās own. First you will have to watch part of a tutorial, to pick up some basic knowledge. The next step is putting the knowledge into practice, and this is where many people fall over. With ChatGPT as your personal tutor, you donāt have to!
In order to really learn how to code, you need to put knowledge into practice. To do this, I recommend CodeWars:
It will give you coding challenges, with increasing levels if difficulty. It has a built in editor and tests that verify your code.
I found this challenge:
Return the number (count) of vowels in the given string.
We will consider a, e, i, o, u as vowels for this Kata (but not y).
The input string will only consist of lower case letters and/or spaces.
You could just get ChatGPT to tell you how to do this by formulating it as a prompt:
Write a JavaScript function that will return the number (count) of vowels in the given string.
We will considerĀ a,Ā e,Ā i,Ā o,Ā uĀ as vowels for this Kata (but notĀ y).
The input string will only consist of lower case letters and/or spaces.
It writes a good function in response, which is amazing. You can see how much more specific the code is than if we did a google search! It even gives a breakdown of how the function works.
But just asking it to do all the work means we donāt learn much.
The way Iāve been using it to learn Java is as follows:
I work out a basic methodology for solving the problem.
I donāt know enough Java to write the solution, so I ask ChatGPT how to do the bits I donāt know how to do.
I combine all of this into the final solution.
So letās imagine a high level pseudocode solution for this problem, then we can ask the Ai how to do bits of it in JavaScipt.
The solution would be something like:
Loop through all the characters in the string.
For each character that is a vowel, increment a result count number.
Return the result count number.
If you can get this far on your own, ChatGPT can help you code that solution.
Letās ask ChatGPT how to loop through the characters in a string:
Loop through the characters in a string in JavaScript
It comes up with a few options, I like this one the best:
for (let char of str) {
console.log(char);
}
For each char, we need to check if it is a vowel. We could have multiple ifs, but would be easier if we had a list (array of vowels) and checked if the char was in the list.
In javaScript, check if a string is in a list
ChatGPT tells me to use an array of strings for my list, then do
myList.includes(str)
to check if the item is in the list. Now we can put it all together:
function getCount(str) {
}
const vowels = ['a', 'e', 'i', 'o', 'u'];
let count = 0;
for (let char of str) {
if (vowels.includes(char)) {
count++;
}
return count;
}
And CodeWars says that works!
Computers teaching us how to program computers. What strange times we live in.