|
| 1 | +// This file is generated by ChatGPT |
| 2 | + |
| 3 | +// eslint-disable-next-line no-console |
| 4 | +const log = console.log |
| 5 | + |
| 6 | +// Define a class using ES6 class syntax |
| 7 | +class Person { |
| 8 | + constructor(name, age) { |
| 9 | + this.name = name |
| 10 | + this.age = age |
| 11 | + } |
| 12 | + |
| 13 | + // Define a method within the class |
| 14 | + sayHello() { |
| 15 | + log(`Hello, my name is ${this.name} and I am ${this.age} years old.`) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +// Create an array of objects |
| 20 | +const people = [ |
| 21 | + new Person('Alice', 30), |
| 22 | + new Person('Bob', 25), |
| 23 | + new Person('Charlie', 35), |
| 24 | +] |
| 25 | + |
| 26 | +// Use the forEach method to iterate over the array |
| 27 | +people.forEach((person) => { |
| 28 | + person.sayHello() |
| 29 | +}) |
| 30 | + |
| 31 | +// Use a template literal to create a multiline string |
| 32 | +const multilineString = ` |
| 33 | + This is a multiline string |
| 34 | + that spans multiple lines. |
| 35 | +` |
| 36 | + |
| 37 | +// Use destructuring assignment to extract values from an object |
| 38 | +const { name, age } = people[0] |
| 39 | +log(`First person in the array is ${name} and they are ${age} years old.`, multilineString) |
| 40 | + |
| 41 | +// Use the spread operator to create a new array |
| 42 | +const numbers = [1, 2, 3] |
| 43 | +const newNumbers = [...numbers, 4, 5] |
| 44 | +log(newNumbers) |
| 45 | + |
| 46 | +// Use a try-catch block for error handling |
| 47 | +try { |
| 48 | + // Attempt to parse an invalid JSON string |
| 49 | + JSON.parse('invalid JSON') |
| 50 | +} |
| 51 | +catch (error) { |
| 52 | + console.error('Error parsing JSON:', error.message) |
| 53 | +} |
| 54 | + |
| 55 | +// Use a ternary conditional operator |
| 56 | +const isEven = num => num % 2 === 0 |
| 57 | +const number = 7 |
| 58 | +log(`${number} is ${isEven(number) ? 'even' : 'odd'}.`) |
| 59 | + |
| 60 | +// Use a callback function with setTimeout for asynchronous code |
| 61 | +setTimeout(() => { |
| 62 | + log('This code runs after a delay of 2 seconds.') |
| 63 | +}, 2000) |
| 64 | + |
| 65 | +let a, b, c, d, foo |
| 66 | + |
| 67 | +if (a |
| 68 | + || b |
| 69 | + || c || d |
| 70 | + || (d && b)) { |
| 71 | + foo() |
| 72 | +} |
0 commit comments