js基础编程-题目18 转圈游戏

整理下初学时做过的js基础编程题目和大家分享以下,如果大家觉得有用,别忘了点一下赞哦

编号1100的一百个人围成一圈,以123123的方式进行报数,数到3的人自动退出圈子,剩下的人继续报数,问最后剩下的人编号是多少?

双指针

function test(n){
  let arr = new Array(n).fill().map((_,i)=>i+1)//flag1
  let num = 1;
  let index = 0;
  while(arr.length > 2){
    if(num !== 3){
      index ++
      num ++  
    }else{
      num = 1
      arr.splice(index,1)
    }
    if(index ===arr.length){index =0}
  }
 return arr
}

flag处:我们有那些需要占位,而上下文中不使用的变量,可以使用下划线表示。

这里扩展下 稀松数组

  • 稀松数组的创建
    Array(仅一个参数) 仅输入一个数,生成有该数长度的空数组(稀松数组)
    new Array(仅一个参数)
console.log(Array(3))//[ <3 empty items> ]
console.log(new Array(3))//[ <3 empty items> ]

ES6有 Array.of 方法,针对数组传一个参数的场景做出了改变

console.log(Array.of(3))// [3]
  • 稀松数组的遍历
    注意:稀松数组是不能遍历的
const arr = Array(3)
console.log(arr.length)//3
for(let i;i<arr.length;i++){//由于不能遍历,此操作无效
  console.log("无效")
  console.log(arr)
}

我们要遍历操作稀松数组,可以想将他填充
方法一:fill() 填充数组

console.log(Array(3).fill())//[ undefined, undefined, undefined ]
console.log(Array(3).fill(1))//[ 1, 1, 1 ]

方法二:... 扩展运算符,将其转化为数组

console.log([...Array(3)])//[ undefined, undefined, undefined ]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柳晓黑胡椒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值