用Chatgpt来学习|闰年计算JavaScript
2023-04-28分类:人工智能 阅读()
利用Chatgpt学习用JavaScript来计算闰年的一些方法//超基本JS code
若要判断年份是否为闰年,请依照下列步骤执行:
- 如果年份被4 整除,则移至步骤2。
- 如果年份被100 整除,则移至步骤3。
- 年份被400 整除。
节录自https://learn.microsoft.com/zh-tw/office/troubleshoot/excel/determine-a-leap-year
方法1:
function isLeapYear(year) { if (year % 4 !== 0) { // If the year is not divisible by 4, it's not a leap year return false; } else if (year % 100 !== 0) { // If the year is divisible by 4 but not by 100, it's a leap year return true; } else if (year % 400 !== 0) { // If the year is divisible by 100 but not by 400, it's not a leap year return false; } else { // If the year is divisible by both 100 and 400, it's a leap year return true; } } // You can call this function with a year as an argument, like this: console.log(isLeapYear(2020)); // Output: true console.log(isLeapYear(2021)); // Output: false
方法2:
function isLeap(year) { if (year % 4 === 0) { if (year % 100 === 0) { if (year % 400 === 0) { return "It is a leap year"; } else { return "It is not a leap year"; } } else { return "It is a leap year"; } } else { return "It is not a leap year"; } }
方法3(最短):
function isLeap(year) { return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0); }
Tags: ChatGPT
标签云
-
CoinMarketCap 炒币 币圈 Rust MOVE IFO filecoin GRT near AAVE DAI Ethereum TVL 加密钱包 ERC20 区块链应用 零知识证明 区块链公司 什么是DeFi BOBA 区块链游戏 DePIN 比特币是什么 加密货币钱包 加密货币 FIL 比特币ETF 比特币挖矿 比特币减半 虚拟货币 比特币交易 加密货币投资 比特币投资 Coinw 数字货币交易所 区块链交易所 区块链开发 矿机 BitMEX OKCoin 比特币钱包 狗狗币怎么买 以太币 虚拟货币交易所 加密货币诈骗 中本聪 加密货币挖矿 BitoPro 什么是区块链 SHIB