javascript - 如何在三元运算符中转换此代码
问题描述
我有这段代码,我使用递归来控制一些数字,而不使用 for 循环。
let counter = 0;
function test() {
if(counter <= 50) {
c.log(counter);
++counter;
test();
} else {
return;
}
}
test(80);
有没有办法用三元运算符在一行中解决这个问题,而不使用这样的外部函数:
let counter = 0;
function doSomething() {
c.log(counter);
++counter;
test();
}
function test() {
counter <= 50 ? doSomething() : null;
}
test(80);
解决方案
function test() {
counter <= 50 ? (c.log(counter), ++counter, test()) : null;
}
test(80);
推荐阅读
- javafx - 方法 setOnAction((
action) -> {}) 对于 ChoiceBox 类型未定义 - javascript - 碰撞发生时如何修复玩家健康的随机减法?
- python - 使用 pandas 的给定组的所有项目的笛卡尔积
- python-3.x - 如何从 excel 或 csv 文件中获取输入并使用 python 和 selenium 从网页中一一获取数据?
- react-native - 具有反应原生线性梯度的可重用组件
- powershell - Powershell中如何查看环境变量提供的所有命令
- android - 如何在协调器布局中将 FrameView 放置在工具栏上
- shaka - Shaka Player 全屏启动视频
- python-2.7 - 如何修改输出文件以获得我想要的格式
- sql-server - 如何在 SQL Server 中将行转换为列?