简体   繁体   English

使用递归交错两个数组(卡片组)......我能够在没有递归的情况下解决它,但想知道我做错了什么

[英]Interleave two arrays (card decks) using recursion... I was able to solve it without recursion but want to know what I am doing wrong

I am attempting to write a function that takes two arrays as inputs, representing the top and bottom halves of a deck of cards, and shuffles them together using recursion.我正在尝试编写一个函数,它将两个数组作为输入,代表一副纸牌的上半部分和下半部分,并使用递归将它们洗牌在一起。 I am attempting return a single array containing the elements from both input arrays interleaved, like so:我正在尝试返回一个数组,其中包含来自交错的两个输入数组的元素,如下所示:

  • the first element should be the first element of the first input array第一个元素应该是第一个输入数组的第一个元素
  • the second element should be the first element of the second input array,第二个元素应该是第二个输入数组的第一个元素,
  • the third element should be the second element of the first input array,第三个元素应该是第一个输入数组的第二个元素,
  • the fourth element should be the second element of the second array,第四个元素应该是第二个数组的第二个元素,

...etc. ...等等。

I want to append any remaining elements to the end of the array.我想将任何剩余的元素附加到数组的末尾。

This is how I solved it without recursion:这就是我在没有递归的情况下解决它的方法:

function shuffleCards(topHalf, bottomHalf) {
  let returnArray = [];
  for (let i = 0; i < Math.max(topHalf.length, bottomHalf.length); i++) {
    if (topHalf[i]) {
      returnArray.push(topHalf[i]);
    }
    if (bottomHalf[i]) {
      returnArray.push(bottomHalf[i]);
    }
  }
  return returnArray
}

and this is my attempt with recursion:这是我的递归尝试:

function shuffleCards(topHalf, bottomHalf) {
  let results = [];
  if (topHalf.length) {
    results.push(topHalf[0]
    } 
  if (bottomHalf.length) {
      results.push(bottomHalf[0])
    }
  return results.concat(shuffleCards(topHalf.slice(1), bottomHalf.slice(1)));
}

I keep getting the syntax error "missing ) after argument list" but I am fairly certain all of the parenthesis are in the write place.我在参数列表后不断收到语法错误“缺少)”,但我相当确定所有括号都在写入位置。

Any tips?有小费吗?

Thanks!谢谢!

Beside missing parenthesis, you could take only the first item from the first half and call the function with swapped arrays.除了缺少括号之外,您只能从前半部分中取出第一项并使用交换数组调用该函数。

 function shuffleCards([value, ...topHalf], bottomHalf) { return value === undefined ? [] : [value, ...shuffleCards(bottomHalf, topHalf)]; } console.log(...shuffleCards([1, 2, 3, 4], [5, 6, 7, 8]));

function shuffleCards(topHalf, bottomHalf,results = []) {
    if(topHalf.length===0&&bottomHalf.length===0)return results;
    if (topHalf.length!==0) {
    results.push(topHalf[0])
    } 
    if (bottomHalf.length!==0) {
      results.push(bottomHalf[0])
    }
    return shuffleCards(topHalf.slice(1), bottomHalf.slice(1),results);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 递归的子集和 - 我做错了什么? - Subset sum with recursion - what am I doing wrong? 我究竟做错了什么。 我想显示没有时间戳的日期但添加 50 天 - What am I doing wrong. I want to display the date without timestamp but add 50 days 我不知道我做错了什么,我想在页面滚动时更改导航栏的背景颜色 - I don't know what i am doing wrong, I want to change the background color of the navbar when the page gets scrolled 拼接数组(数组)Javascript(我做错了什么) - Splice array(of arrays) Javascript (what am i doing wrong) 我做错了什么? - What i am doing wrong? 无法使用 react 和 redux 在待办事项列表中添加待办事项。 我究竟做错了什么? - Not able to add todo in a todo-list using react and redux. What am I doing wrong? AngularJS指令两个数据绑定 - 我做错了什么? - AngularJS Directive Two data binding - What am I doing wrong? 比较两个输入字段-我在做什么错? - Comparing two input fields - What am I doing wrong? 两个总和数组 - 蛮力 - 我做错了什么? - Two Sum Array - Brute Force - what am I doing wrong? 我无法在以下代码中设置Global变量。 我究竟做错了什么? - I am not able to set the Global variable in the following code. What am i doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM