不知道为什么这个函数返回一个反向数组 [英] Not sure why this function returns a reversed array

查看:89
本文介绍了不知道为什么这个函数返回一个反向数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事JavaScript练习,但在弄清逻辑为何起作用时遇到了一些麻烦.它基本上具有一个称为神秘"的函数,该函数使用许多非常简单的函数,并以相反的顺序返回您提供的数组.我已经在白板前坐了一个小时,试图弄清楚背后的逻辑,但没有明白.善良的灵魂可以看看这些函数并解释神秘函数如何返回反向数组吗?谢谢!

I'm working on a JavaScript exercise and having some trouble untangling the logic as to why it works. It basically has a function called "mystery" that uses a bunch of very simple functions, and returns an array you give it but in reversed order. I've been sitting in front of a whiteboard for an hour trying to figure out the logic behind it, and not getting it. Can a kind soul take a look at these functions and explain how the mystery function returns a reversed array? Thank you!

function rest(arr) {
  return arr.slice(1);
}


function first(arr) {
  return arr[0];
}

function conj(arr, value) {
  arr.push(value);
  return arr;
}

function mystery(array) {
  if (array.length === 0) {
    return [];
  }
  return conj(mystery(rest(array)), first(array));
}

推荐答案

奥秘是一种递归函数.

它使用 rest 函数的返回值调用自身,该函数返回除第一个元素以外的所有内容.

It calls itself using the return value of the rest function, which returns everything except the first element.

它使用那个结果+ first 的结果,该结果返回第一个字符,并再次连接它们(使用 conj ),但第一个元素位于结束.

It uses the result of that + the result of first, which returns the first character, and concatenates them again (using conj), but with the first element at the end.

所以,假设您输入了[H e l l o],

So, say you put in [H e l l o],

它将返回 conj(mystery([e l l o],H)

mystery([e l l o])将返回 conj(mystery([l l o],e)

mystery([l l o])将返回 conj(mystery([l o],l)

依此类推,直到进入 mistery 的数组为空,在这种情况下递归结束,然后我们冒泡回到第一个调用.

and so on, until the array that goes into mistery is empty, in which case the recursion ends, and we bubble back up to the first call.

请注意,递归经常用于这样的练习,但是尽管它有一些特定的用途,但在许多情况下,不使用递归会更有效,因为与调用该函数相比,进行另一个函数调用的开销相对比较大.其他解决方案使用一个简单的循环来移动或交换项目.

Side note, recursion is often used for exercises like this, but although it has some specific uses, in many cases it's way more efficient to not use recursion, because the overhead of making another function call is relatively hard work, compared to other solutions using a simple loop to move or swap items around.

如果您输出一些信息,您可以看到正在发生的事情:

You can see what is happening if you output some information:

function rest(arr) {
  return arr.slice(1);
}


function first(arr) {
  return arr[0];
}

function conj(arr, value) {
  arr.push(value);
  return arr;
}

function mystery(array, level) {
  if (array.length === 0) {
    console.log('mystery level '+level+' is called with an empty array. Recursion ends here.. Stay tuned for the answer.');
    return [];
  }
  console.log('mystery level '+level+' is called with '+array+
              '. I will move '+first(array)+' to the end.');
  
  var result = conj(mystery(rest(array), level+1), first(array));
  console.log('returning '+result+' for level '+level);
  return result;
}

console.log(mystery(['H','e','l','l','o'], 0));

这篇关于不知道为什么这个函数返回一个反向数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆