DEV Community

Anthony Nanfito
Anthony Nanfito

Posted on • Originally published at ananfito.hashnode.dev

Using ChatGPT as a Coding Buddy

Intro

Hello, world! Welcome to another blog post for Anthony’s Techie Thoughts! If you’re new here, my name is Anthony and I’m studying programming to make a career change from mathematics teacher to a frontend web developer. In my blog posts, I reflect on my journey and share things I’m learning along the way. You can find me on GitHub, LinkedIn, Hashnode, DEV.to, and CodeNewbie.

In this blog post, I’ll offer a quick reflection on my progress this week and share some thoughts on how I used ChatGPT as a coding buddy.

Weekly Reflection

This past week has been productive for me. This is mostly because I’m currently on a mid-term break from work and I have extra time I can dedicate to my studies. Which I am thoroughly enjoying. Here’s a quick list of my accomplishments this week:

Using ChatGPT as a ‘coding buddy’

On a whim, I decided to make use of ChatGPT as a ‘coding buddy’ by asking it questions when I got stuck or needed a sounding board. As a self-taught dev, this is one of the things I’ve struggled with the most. Sometimes I wish I could just tap someone on the shoulder and say, “Hey, what do I need to make this work?” Or “This is what I’m thinking for this problem. Is that the right approach?” However, this isn’t always an easy option so this week I tried something different: I asked ChatGPT.

For example, I was working on a function earlier this week for my Learning Journal project where I needed it to return the first three elements of an array, but I couldn’t figure out what method I needed to use. Initially, I thought I could use the .filter() method, but further down you’ll see how ChatGPT corrected me on that idea. First I asked it, “How do I write a function in JavaScript to return the first 3 items in an array?” Here is the response I received:

Image of chat conversation with ChatGPT. QUERY: write a function in javascript that will return the first three elements from an array.  RESPONSE: You can write a function in JavaScript that returns the first three elements from an array using the slice() method. Here's an example:  javascript  function getFirstThreeElements(arr) {   return arr.slice(0, 3); }  This function takes an array arr as an argument and returns a new array containing the first three elements of arr.  Here's an example of how to use this function:  javascript  const myArray = [1, 2, 3, 4, 5, 6]; const firstThreeElements = getFirstThreeElements(myArray); console.log(firstThreeElements); // Output: [1, 2, 3]  In this example, the getFirstThreeElements() function is called with the myArray array as an argument. The function returns a new array containing the first three elements of myArray, which are then printed to the console.

When I asked my ‘coding buddy’ this question the first thing it mentions is the .slice() method and I was like, “Oh! duh! Of course, why didn’t I think of that?” As requested, it did write an entire function for returning the first 3 elements of an array, but I didn’t need the entire function. I just needed that little hint to use .slice() instead of .filter(). When I asked it if I could use the .filter() method it explained why it wasn’t possible and why I should use the .slice() method instead:

Image of chat conversation with ChatGPT. QUERY: Is it possible to write return the first three elements of an array using the filter() method in JavaScript?  RESPONSE: The filter() method in JavaScript is not suitable for returning the first three elements of an array because it is designed to filter an array based on some condition and returns a new array containing only the elements that pass that condition. It does not provide a way to select a specific number of elements from the beginning of an array.  Instead, you can use the slice() method to return the first three elements of an array, as shown in the previous example:  javascript  function getFirstThreeElements(arr) {   return arr.slice(0, 3); }  const myArray = [1, 2, 3, 4, 5, 6]; const firstThreeElements = getFirstThreeElements(myArray); console.log(firstThreeElements); // Output: [1, 2, 3]  The slice() method can be used to select a range of elements from an array, starting from the first element and ending at the specified index. In this example, we used slice(0, 3) to select the first three elements of the myArray array.

(NOTE: While writing this blog post, I noticed that my initial question here has a typo in the first part of the question. I’m not sure why my brain decided to write “Is it possible to write return …” but ChatGPT seems to have correctly interpreted this to mean “Is it possible to write a function that returns the first …” This is interesting, but could also just be a random occurrence. In the big picture, I’m not sure if ChatGPT is always good at interpreting human miscommunication.)

This second explanation helped me realize the error in my initial approach and redirect toward a solution that worked. Of course, this is information could have obtained from someone in a Discord community, a forum, or even through reading through webpages and documentation, but those come with a longer time commitment. Here, in a few minutes, I got some advice on the direction I needed to go as well as some examples to reference. In fact, in this case, I just needed a nudge in the right direction. As a self-taught dev, this is particularly useful because I can’t just lean over to the next cubicle and ask a coworker quick questions like these. But, for now, I can ask ChatGPT and it (might) give me an answer to ‘nudge’ me in the right direction.

Closing thoughts on ChatGPT

As impressive as it ChatGPT is (and other AI software like it), we’ve already seen examples of where it will hallucinate and present incorrect information. With that in mind, I do not think it is a good idea to ask it to do a specific task for you.

For example, I’m sure I could have given it more information and context about how and why I needed a function to return the first three elements of an array, but that would have defeated the purpose of studying. To me, that is the equivalent of having someone else do your work for you. Which doesn’t help you become a better developer.

I tell my students, “the person who holds the pencil learns the math.” Meaning, the person who is sitting there going through the steps (and perhaps struggling a bit) to solve the problem is the one who will learn and remember it. In this case, the person who types on the keyboard learns to code.

All that said, I do think this could become another tool within our developer toolbox. We have a plethora of resources already at our deposal: freeCodeCamp, Stackoverflow, W3 Schools, YouTube, Discord Communities, and more! This is an addition to that and perhaps it might fill in some gaps that the others don’t provide or don’t meet the needs of the learner. I think this is how I’ll use it moving forward (and continue to use it as long as it proves useful). It’s one tool in a vast toolbox of tools to help me learn how to code and get better at achieving my goal of making a career switch.

Thanks for reading

Thanks so much for reading this post. Have you used ChatGPT in your studies? If so, how have you been using it? Has it been helpful? Leave your thoughts in the comments below. Hearing your thoughts makes this more of a conversation and helps everyone learn.

Top comments (0)