DEV Community

Cover image for I asked ChatGPT to generate 10 easy Javascript challenges and here is what I got...
zuzexx
zuzexx

Posted on

I asked ChatGPT to generate 10 easy Javascript challenges and here is what I got...

After resisting ChatGPT for quite some time I finally bit the bullet and started using it. I wanted to see what all the fuss is about. Turns out, ChatGPT can be a really good coding companion, if you know how to use it and do not take it's answers at face value. I decided to use it mostly for generating new challenges for me to test my skills and practice Javascript.

Here are the first 10 challenges it generated for me. these challenges were generated based on the assumption that the user knows about the basics of JS, variables, data types, operators, control structures, functions and objects.

I want to compare the way I save the challenge with the way ChatGPT saves the challenge. I think the tasks are quite easy, so there will not be much explanation.

1. Challenge

Create a program that takes in two numbers as input and uses arithmetic operators to perform basic calculations (addition, subtraction, multiplication, division, and modulo) on them. The program should output the result of each calculation.

My solution:

const calculation = (num1, num2) => {
  console.log(`addition: ${num1 + num2}`);
  console.log(`substraction: ${num1 - num2}`);
  console.log(`multiplication: ${num1 * num2}`);
  console.log(`division: ${num1 / num2}`);
  console.log(`modulo: ${num1 % num2}`);
};

calculation(1, 2);
calculation(6, 3);
calculation(10, 13);

Enter fullscreen mode Exit fullscreen mode

ChatGPT solution:


function performCalculations(num1, num2) {
  let addition = num1 + num2;
  let subtraction = num1 - num2;
  let multiplication = num1 * num2;
  let division = num1 / num2;
  let modulo = num1 % num2;
  console.log("Addition: " + addition);
  console.log("Subtraction: " + subtraction);
  console.log("Multiplication: " + multiplication);
  console.log("Division: " + division);
  console.log("Modulo: " + modulo);
}

let num1 = parseInt(prompt("Enter the first number:"));
let num2 = parseInt(prompt("Enter the second number:"));
performCalculations(num1, num2);

Enter fullscreen mode Exit fullscreen mode

2. Challenge

Write a program that takes in a string and uses string methods to perform various operations on it (such as finding the length, index of a character, concatenating two strings, etc.). The program should output the result of each operation.

My solution:

const stringOperations = (str1, str2) => {
  console.log(`String1 length: ${str1.length}`);
  console.log(`String2 length: ${str2.length}`);
  console.log(`Slice string: ${str1.slice(0, 3)}`);
  console.log(`Substring: ${str1.substring(0, 2)}`);
  console.log(`Substr: ${str2.substr(0, 4)}`);
  console.log(`Replace: ${str1.replace(/world/i, "Earth")}`);
  console.log(`Uppercase: ${str2.toUpperCase()}`);
  console.log(`Lowercase: ${str2.toLowerCase()}`);
  console.log(`Concat: ${str1.concat(" ", str2)}`);
};

stringOperations("Hello world", "Javascript rocks");
Enter fullscreen mode Exit fullscreen mode

ChatGPT solution:

let inputString = prompt("Enter a string:");

let length = inputString.length;
console.log("Length of the string: " + length);

let index = inputString.indexOf("e");
console.log("Index of the letter 'e': " + index);

let concatenatedString = inputString.concat(" Appended String");
console.log("Concatenated String: " + concatenatedString);

let substring = inputString.substring(4, 9);
console.log("Substring: " + substring);

let splitString = inputString.split(" ");
console.log("Split String: " + splitString);

let upperCase = inputString.toUpperCase();
console.log("Uppercase: " + upperCase);

let lowerCase = inputString.toLowerCase();
console.log("Lowercase: " + lowerCase);

Enter fullscreen mode Exit fullscreen mode

3. Challenge

Create a program that uses a for loop and an array to print out all the elements of the array in reverse order.

My solution:

const reverseArray = (array) => {
  for (let i = array.length - 1; i >= 0; i--) {
    console.log(array[i]);
  }
};

reverseArray(["r", "e", "v", "e", "r", "s", "e"]);
Enter fullscreen mode Exit fullscreen mode

ChatGPT solution:

let myArray = [1, 2, 3, 4, 5];

for (let i = myArray.length - 1; i >= 0; i--) {
  console.log(myArray[i]);
}

Enter fullscreen mode Exit fullscreen mode

4. Challenge

Write a program that uses a while loop to print out all the even numbers between 1 and 10.

My solution:

const evenNumbers = () => {
  let i = 1;
  while (i <= 10) {
    if (i % 2 === 0) {
      console.log(i);
    }
    i++;
  }
};
Enter fullscreen mode Exit fullscreen mode

ChatGPT solution:

let num = 1;

while (num <= 10) {
  if (num % 2 === 0) {
    console.log(num);
  }
  num++;
}

Enter fullscreen mode Exit fullscreen mode

5. Challenge

Create a program that uses an if-else statement to check if a number is positive or negative. The program should output "positive" if the number is greater than 0, and "negative" if the number is less than 0.

My solution:

const checkNumber = (num) => {
  if (num > 0) console.log("Positive");
  else if (num < 0) console.log("Negative");
  else console.log("Number 0 is not positive nor negative");
};
checkNumber(1);
checkNumber(0);
checkNumber(-1);
Enter fullscreen mode Exit fullscreen mode

ChatGPT solution:

let num = parseInt(prompt("Enter a number:"));

if (num > 0) {
  console.log("Positive");
} else if (num < 0) {
  console.log("Negative");
} else {
  console.log("Zero")
}

Enter fullscreen mode Exit fullscreen mode

6. Challenge

Write a program that takes in a number and uses a switch statement to check if the number is greater than, less than, or equal to 10. The program should output "Greater than 10", "Less than 10", or "Equal to 10" depending on the input number.

My solution:

let num = -11;
switch (true) {
  case num > 10:
    console.log(`${num} is bigger than 10`);
    break;
  case num < 10:
    console.log(`${num} is smaller than 10`);
    break;
  case num === 10:
    console.log(`${num} is 10`);
    break;
}
Enter fullscreen mode Exit fullscreen mode

ChatGPT solution:

let num = parseInt(prompt("Enter a number:"));

switch (true) {
  case num > 10:
    console.log("Greater than 10");
    break;
  case num < 10:
    console.log("Less than 10");
    break;
  case num === 10:
    console.log("Equal to 10");
    break;
}

Enter fullscreen mode Exit fullscreen mode

7. Challenge

  1. Create a program that defines an object with different properties (strings, numbers, booleans) and methods. The program should use the object to perform different actions, such as displaying the properties, changing the values of the properties, and executing the methods.

My solution:

let myDog = {
  name: "Fido",
  age: 3,
  isFemale: false,
  legs: 4,
  displayDog: function () {
    if (this.isFemale === false)
      console.log(
        `I have a dog named ${this.name} who is ${this.age} years old. It's sex is male and it has ${this.legs} legs.`
      );
    else
      console.log(
        `I have a dog named ${this.name} who is ${this.age} years old. It's sex is female and it has ${this.legs} legs.`
      );
  },
  changeName: function (newName) {
    this.name = newName;
  },
  changeAge: function (newAge) {
    this.age = newAge;
  },
  changeSex: function (newIsFemale) {
    this.isFemale = newIsFemale;
  },
  changeLegs: function (newLegs) {
    this.legs = newLegs;
  },
};
Enter fullscreen mode Exit fullscreen mode

ChatGPT solution:

let myObject = {
  name: "John",
  age: 30,
  isStudent: false,
  displayProperties: function() {
    console.log("Name: " + this.name);
    console.log("Age: " + this.age);
    console.log("Is Student: " + this.isStudent);
  },
  changeName: function(newName) {
    this.name = newName;
  },
  incrementAge: function() {
    this.age++;
  }
};


Enter fullscreen mode Exit fullscreen mode

8. Challenge

Write a program that defines a function that takes in two arguments and returns the result of a comparison between them. The program should use the function to compare different data types (strings, numbers, booleans)

My solution:

const compareTypes = (type1, type2) => {
  if (type1 === type2) console.log("Values are the same");
  else console.log("Values are not the same");
};
Enter fullscreen mode Exit fullscreen mode

ChatGPT solution:

function compareValues(val1, val2) {
  if (val1 === val2) {
    return "Values are equal";
  } else {
    return "Values are not equal";
  }
}
Enter fullscreen mode Exit fullscreen mode

9. Challenge

Create a program that uses a for loop and an object to iterate through the properties of the object and print their values.

My solution:

const dog = {
  name: "Fido",
  age: 3,
  isFemale: false,
  legs: 4,
};
const iterateObject = () => {
  for (const key in dog) {
    console.log(`${key}: ${dog[key]}`);
  }
};
iterateObject();
Enter fullscreen mode Exit fullscreen mode

ChatGPT solution:

let myObject = {
  name: "John",
  age: 30,
  isStudent: false,
  hobbies: ['reading', 'traveling']
};

for (let prop in myObject) {
  console.log(myObject[prop]);
}

Enter fullscreen mode Exit fullscreen mode

10. Challenge

Write a program that uses a while loop and an array to find the first occurrence of a specific element in the array. The program should return the index of the element or -1 if the element is not found.

My solution:

const findElement = (array, compare) => {
  let i = 0;
  while (i < array.length) {
    i++;
    if (compare == array[i]) return console.log(i);
  }
  return console.log(-1);
};
Enter fullscreen mode Exit fullscreen mode

ChatGPT solution:

let myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let target = 5;
let index = -1;
let i = 0;

while (i < myArray.length && index === -1) {
  if (myArray[i] === target) {
    index = i;
  }
  i++;
}

console.log(index);

Enter fullscreen mode Exit fullscreen mode

Top comments (0)