gpt4 book ai didi

javascript - 从堆栈或表返回数组类型 - Javascript

转载 作者:行者123 更新时间:2023-12-03 01:52:21 25 4
gpt4 key购买 nike

我的代码有问题。也许简单,也许不 - 首先让我们看看这个。

var tab = [];

do{
var i = 0;
tab.push(prompt("Write some type of array!"));
i++;

}while(confirm("Next array?"));

for (var i=0; i<tab.length; i++)
document.write(typeof(tab[i])+"<br>");
<!DOCTYPE HTML>
<HTML>
<HEAD>

<meta charset="utf-8">
<meta name="author" content="Donio3d">

</HEAD>
<BODY>



</BODY>

<script src="script.js"></script>

</HTML>

所以我想从堆栈返回一种类型的数组。一切都是字符串。

有什么办法可以做到这一点,而不用 IF 语句检查吗?

最佳答案

要获取输入的 typeof,您必须首先使用 Number.isNaN() 检查它是否是数字。为简单起见,还使用 ​​Unary plus (+) operator .

代码:

const tab = [];
const getInputTyeof = i => typeof (!Number.isNaN(+i) ? +i : i);

do {
tab.push(prompt('Write some type of array!'));
} while (confirm('Next array?'));

tab.forEach(input => document.write(getInputTyeof(input) + '<br>'));

关于javascript - 从堆栈或表返回数组类型 - Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50365880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com