AI Chatbots Are Making Learning to Program Easier
As a developer, I've always found that the best way to learn something new is by asking questions. But sometimes, it can be hard to find someone who can explain complex concepts in a way that's easy to understand.
That's where AI chatbots come in. In this blog post, I'll share my hot take on the 3 leading AI chatbots for learning to program.
So without further ado, let's get started!
Prompt - "Explain javascript promises like I am 5"
ChatGPT - 3.5
Alright, imagine you're playing a game with your friend, and you want to get a cool toy from a vending machine. But the vending machine takes a little time to give you the toy, and you don't want to wait there doing nothing.
So, you give your friend a special promise note that says, "Hey, I'll give you a toy when I get it from the machine." Your friend can continue playing other games or doing other stuff instead of just waiting.
Now, when the vending machine finally gives you the toy, you keep your promise and give the toy to your friend. Your friend is happy because they got the toy they were promised, and you're happy because you made your friend happy!
In JavaScript, sometimes we have to do things that take some time, like fetching data from the internet or loading images. We use promises just like the promise note you gave your friend. The promise says, "Hey, I'll let you know when I'm done getting the data or image."
Bard
Imagine you're at a restaurant and you want to order a hamburger. You tell the waiter what you want, and they give you a buzzer. The buzzer will buzz when your hamburger is ready.
The buzzer is a promise. It's a way of saying, "I'll let you know when your hamburger is ready."
In JavaScript, promises are objects that represent the eventual completion or failure of an asynchronous operation. An asynchronous operation is something that takes time to finish, like loading a web page or making a network request.
// Create a promise.
let promise = new Promise(function(resolve, reject) {
// Do something that takes time.
setTimeout(function() {
// Resolve the promise with a value.
resolve("Your hamburger is ready!");
}, 3000);
});
// Attach a handler to the promise.
promise.then(function(value) {
// Do something with the value.
console.log(value);
});
Bing Chat
Imagine you want to climb a tree in the park, but you have to ask your mom for permission first. You don't know if she will say yes or no, so you have to wait for her answer. A promise is like a way of telling your mom: "Please let me know if I can climb the tree or not, and I will do something else depending on your answer."
For example, you can say: "If you say yes, I will be very happy and go to the park with my friends. If you say no, I will be sad and stay at home and play with my toys." These are the two possible outcomes of your promise: either it is fulfilled (your mom says yes) or it is rejected (your mom says no).
A promise in JavaScript is similar. It is an object that represents a task that may or may not be completed in the future. It has two functions that are called when the task is either successful or unsuccessful. These functions are called resolve and reject.
For example, you can write a promise like this:
let climbTree = new Promise(function(resolve, reject) {
// ask mom for permission
let permission = askMom();
// if permission is granted, resolve the promise
if (permission) {
resolve("Yay! I can climb the tree!");
}
// if permission is denied, reject the promise
else {
reject("Oh no! I can't climb the tree!");
}
});
Then, you can use the promise like this:
climbTree.then(function(message) {
// this function is called when the promise is resolved
console.log(message); // "Yay! I can climb the tree!"
// do something else when you are happy
}).catch(function(error) {
// this function is called when the promise is rejected
console.log(error); // "Oh no! I can't climb the tree!"
// do something else when you are sad
});
This way, you don't have to wait for your mom's answer before doing something else. You can just tell her what you want to do depending on her answer, and then move on with your life. That's how promises work in JavaScript. They make your code easier to read and manage.
Bot/Quality | Chat GPT-3.5 | Bard | Bing Chat |
---|---|---|---|
Explanation | OK | Good | Good ā |
Gave code. | No | Yes | Yes ā |
Code Relevance | No | Somewhat Yes | Yes ā |
I overall enjoyed the Bing's answer.
Peace š
Top comments (0)