编程开发新朋友 —— ChatGPT 和 NotionAI 实战

简介: 编程开发新朋友 —— ChatGPT 和 NotionAI 实战

废话少说,本篇带来借助 AI 帮助我们编程开发的 4 种场景实战,分别是:

1、AI 代码释义

2、AI 完善代码

3、AI 修正代码

4、AI 测试代码


问答来源于 ChatGPT 和 NotionAI,相信你一定能感受到 AIGC 的魅力~

喜欢三连,笔芯~


image.png


关联阅读:为什么我更推荐 Notion AI 胜于 ChatGPT ? - 掘金


AI 代码释义



⭐输入:

//解释下以下代码是干嘛的
function calculateDistance(x1, y1, x2, y2) {
  return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
}


ChatGPT



image.png


输出:

这段代码是计算两个点之间的欧几里得距离的函数。其中,(x1, y1) 和 (x2, y2) 表示两个点的坐标,函数通过使用勾股定理计算出它们之间的距离并返回结果。


NotionAI


image.png


输出:


函数calculateDistance(x1, y1, x2, y2) 用于计算两个点之间的距离,其中x1和y1表示第一个点的坐标,x2和y2表示第二个点的坐标。该函数使用勾股定理计算两点之间的距离,即通过求平方根来计算 (x1 - x2)^2 + (y1 - y2)^2 的值。


⭐输入:

//为以下函数写注释
const debounce = (func, wait) => {
    let timer
    return () => {
    clearTimeout(timer)
        timer = setTimeout(func, wait);
    }
}


ChatGPT 输出:


这是一个 JavaScript 实现的防抖动函数。防抖动函数接受两个参数:"func" 和 "wait"。"func" 参数表示在等待 "wait" 指定的时间后要执行的函数。


防抖动函数返回一个匿名函数,该匿名函数设置了一个计时器。如果计时器已存在,则会使用 "clearTimeout(timer)" 来清除它。然后,使用 "setTimeout(func, wait)" 设置新的计时器,确保即使在指定的时间段内多次调用该匿名函数,也只会在等待时间过后执行一次 "func" 函数。


NotionAI 输出:


// debounce函数用来限制函数调用的频率。

// 它返回一个匿名函数,在指定的等待时间后调用传入的函数,并且清除上一次的计时器。


AI 完善代码



⭐输入:


// 根据以下代码生成新代码
const className = "my-div";
const text = "Hello world!";


ChatGPT


image.png


输出:

const element = document.createElement("div");
element.className = className;
element.textContent = text;
document.body.appendChild(element);


NotionAI


image.png


输出:


// Styling a div with a class
<div class="my-div">Hello world!</div>


AI 修正代码



⭐输入:

// 修正以下代码错误
function List(props) {
  const items = props.items; // error: items is undefined
  return (
    <ul>
      {items.map(item => (
        <li key={item.id}>{item.name}</li>
      ))}
    </ul>
  );
}


ChatGPT


image.png


输出:

// Here is the corrected code:
function List(props) {
  const { items = [] } = props;
  return (
    <ul>
      {items.map(item => (
        <li key={item.id}>{item.name}</li>
      ))}
    </ul>
  );
}


In this code, the props object is destructured to get the items property. If props doesn't have an items property, it will default to an empty array [].

它也给 items 设置了默认值,太强了!


NotionAI


image.png


输出:

function List(props) {
  const items = props.items;
  if (items && items.length > 0) {
    return (
                <ul> {items.map(item => ( <li key={item.id}>{item.name}</li> ))} </ul>
    );
  }
  return null;
}

Notion AI 一样能修复代码,做了条件判断,太强了!


AI 测试代码



⭐输入:

// 给以下代码做测试
const testCases = [
  {length: 2, width: 3, expected: 6},
  {length: 4, width: 5, expected: 20},
  {length: 6, width: 7, expected: 42},
];


ChatGPT


image.png


输出:

Here is a sample code for testing the given test cases:


function testAreaCalculation(testCases) {
  for (let i = 0; i < testCases.length; i++) {
    const { length, width, expected } = testCases[i];
    const result = length * width;
    if (result !== expected) {
      console.error(`Test case ${i} failed: expected ${expected} but got ${result}`);
    } else {
      console.log(`Test case ${i} passed`);
    }
  }
}
testAreaCalculation(testCases);


This code will run through all the test cases and print out "Test case i passed" for each test case that passes and "Test case i failed: expected expectedValue but got result" for each test case that fails.


回答生成了一个健全的代码测试用例;


NotionAI


image.png

输出:

testCases.forEach((testCase) => {
    const area = calculateArea(testCase.length, testCase.width);
    expect(area).toBe(testCase.expected);
});


小结



最后用 Notion AI 为本篇作一个小结:

image.png

TIME.


相关文章
|
1月前
|
人工智能 搜索推荐 大数据
GitHub Copilot Enterprise登场,或将掀起编程界的ChatGPT革命!
【2月更文挑战第13天】GitHub Copilot Enterprise登场,或将掀起编程界的ChatGPT革命!
39 1
GitHub Copilot Enterprise登场,或将掀起编程界的ChatGPT革命!
|
3月前
|
人工智能 Oracle 关系型数据库
kettle开发-其实chatGPT一直在身边
kettle开发-其实chatGPT一直在身边
46 0
|
4月前
|
自然语言处理 安全 机器人
ChatGPT VS FuncGPT(慧函数),究竟是谁的开发效率高
你是否曾幻想过未来,不再需要由人类来完成代码工作,而是由机器自动编写?只需告诉机器一句话甚至一个词,它将完成所有编码、测试和运行等流程。
|
5月前
|
Linux Android开发 iOS开发
基于.Net开发的ChatGPT客户端,兼容Windows、IOS、安卓、MacOS、Linux
基于.Net开发的ChatGPT客户端,兼容Windows、IOS、安卓、MacOS、Linux
88 0
|
6月前
|
JavaScript 容器
和chatgpt学架构05-首页开发
和chatgpt学架构05-首页开发
|
4月前
|
设计模式 存储 人工智能
如何利用 ChatGPT 提升编程技能
如何利用 ChatGPT 提升编程技能
|
1月前
|
存储 JavaScript 前端开发
【ChatGPT 指令大全】怎么使用ChatGPT辅助程式开发
【ChatGPT 指令大全】怎么使用ChatGPT辅助程式开发
24 0
|
5月前
|
存储 人工智能 Java
ChatGPT API接口编程基础与使用技巧
ChatGPT API接口编程基础与使用技巧
540 0
|
2月前
|
人工智能 自然语言处理 搜索推荐
迈向AI时代:掌握Python编程与ChatGPT的结合
ChatGPT是一种基于自然语言处理技术的模型,它可以理解人类的语言并生成相应的回复。而Python编程则是一种通过编写代码来实现特定功能的技能。将ChatGPT与Python编程相结合,我们可以利用ChatGPT来帮助我们理解编程概念、编写代码以及解决编程问题。
674 2
|
6月前
|
JavaScript 前端开发
和chatgpt学架构04-路由开发
和chatgpt学架构04-路由开发