Learning JavaScript from ChatGPT. TL;DR — Great, but Can’t Replace Expert… | by Eric Elliott | JavaScript Scene | Mar, 2023

TL;DR — Great, but Can’t Replace Expert Mentors, Yet!

Actual Photo of ChatGPT Teaching Puppies to Code (Just Kidding it’s Midjourney)
const constVar = 'constant';
let letVar = 'let';
var varVar = 'var';

// Observe the behavior when you try to reassign each variable:
constVar = 'newConstant';
letVar = 'newLet';
varVar = 'newVar';

function add(a, b) {
// Your code here
}

// Test your function with these inputs:
console.log(add(5, 3)); // Expected output: 8
console.log(add(-2, 10)); // Expected output: 8

function add(a, b) {
return a + b;
}

console.log(add(5, 3)); // Actual output: 8
console.log(add(-2, 10)); // Actual output: 8

function findMax(numbers) {
// Your code here
}

// Test your function with these inputs:
console.log(findMax([1, 5, 9, 3])); // Expected output: 9
console.log(findMax([-2, -5, -1, -8])); // Expected output: -1

function findMax(numbers) {
return Math.max(…numbers);
}

// Test your function with these inputs:
console.log(findMax([1, 5, 9, 3])); // Actual output: 9
console.log(findMax([-2, -5, -1, -8])); // Actual output: -1

function createPerson(name, age) {
// Your code here
}

// Test your function with these inputs:
console.log(createPerson('Alice', 30)); // Expected output: { name: 'Alice', age: 30 }
console.log(createPerson('Bob', 42)); // Expected output: { name: 'Bob', age: 42 }

See also ChatGPT Alternatives You Can Try Right Now
function createPerson(name, age) {
return {name, age};
}

// Test your function with these inputs:
console.log(createPerson('Alice', 30)); // Actual output: { name: 'Alice', age: 30 }
console.log(createPerson('Bob', 42)); // Actual output: { name: 'Bob', age: 42 }

function wait(milliseconds) {
// Your code here
}

// Test your function with these inputs:
wait(1000).then(() => console.log('1 second has passed'));
wait(2000).then(() => console.log('2 seconds have passed'));

function wait(milliseconds) {
return new Promise((resolve) => {
setTimeout(() => { resolve(); }, milliseconds);
});
}

// Test your function with these inputs:
wait(1000).then(() => console.log('1 second has passed'));
wait(2000).then(() => console.log('2 seconds have passed'));

function sumOfSquares(numbers) {
// Your code here
}

// Test your function with these inputs:
console.log(sumOfSquares([1, 2, 3])); // Expected output: 14
console.log(sumOfSquares([4, 5, 6])); // Expected output: 77

function sumOfSquares(numbers) {
return numbers.reduce((acc, num) => acc + num * num, 0);
}

// Test your function with these inputs:
console.log(sumOfSquares([1, 2, 3])); // Actual output: 14
console.log(sumOfSquares([4, 5, 6])); // Actual output: 77

function double(x) {
return x * 2;
}

function square(x) {
return x * x;
}

function doubleAndSquare(x) {
return square(double(x));
}

console.log(doubleAndSquare(2)); // Expected output: 16

function addOne(x) {
return x + 1;
}

function addOneAndSquare(x) {
// Your code here
}

function addOneAndSquare(x) {
return square(addOne(x));
}

// Test your function with these inputs:
console.log(addOneAndSquare(2)); // Actual output: 9
console.log(addOneAndSquare(4)); // Actual output: 25

See also ChatGPT Declines Request for Poem Admiring Trump, But Biden Query Is Successful
function compose(...fns) {
return (x) => {
return fns.reduceRight((acc, fn) => {
return fn(acc);
}, x);
};
}
function pipe(...fns) {
return (x) => {
return fns.reduce((acc, fn) => {
return fn(acc);
}, x);
};
}
const addOneAndSquare = compose(square, addOne);
console.log(addOneAndSquare(2)); // Output: 9
console.log(addOneAndSquare(4)); // Output: 25
const addOneAndSquarePipe = pipe(addOne, square);
console.log(addOneAndSquarePipe(2)); // Output: 9
console.log(addOneAndSquarePipe(4)); // Output: 25
const compose = (…fns) => x => fns.reduceRight((y, f) => f(y), x);
const compose = (...fns) => (x) => fns.reduceRight((y, f) => f(y), x);
const pipe = (...fns) => (x) => fns.reduce((y, f) => f(y), x);
function withAuthentication(WrappedComponent) {
return function (props) {
if (props.isAuthenticated) {
return <WrappedComponent {...props} />;
} else {
return <div>Please log in to access this component.</div>;
}
};
}
const DashboardWithAuth = compose(withAuthentication)(Dashboard);
const ProfileWithAuth = compose(withAuthentication)(Profile);
See also NVIDIA CEO Jensen Huang comments on ChatGPT and artificial intelligence
function withLayout(WrappedComponent) {
return function (props) {
return (
<div className="layout">
<Header />
<WrappedComponent {...props} />
<Footer />
</div>
);
};
}

function withLogging(WrappedComponent) {
return function (props) {
console.log(`Rendering ${WrappedComponent.name} component`);
return <WrappedComponent {...props} />;
};
}

const withProviders = compose(withAuthentication, withLayout, withLogging);

const DashboardWithProviders = withProviders(Dashboard);
const ProfileWithProviders = withProviders(Profile);

Add Comment