Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【2021-07-07】用 JavaScript 写一个函数,输入 int 型,返回整数逆序后的字符串。如:输入整型 1234,返回字符串“4321”。要求必须使用递归函数调用,不能用全局变量,输入函数必须只有一个参数 #2

Open
superDCH opened this issue Jul 7, 2021 · 1 comment

Comments

@superDCH
Copy link
Owner

superDCH commented Jul 7, 2021

function reverse(number,str=''){
  let target=String(number)
  str += target.substr(-1)
  if(target.length){
    return reverse(target.substr(0,target.length-1),str)
  }else{
    return str
  }
}

console.log(reverse(1))  //1
console.log(reverse(1234)) //4321
console.log(reverse(1234.1234)) //4321.4321
@superDCH superDCH changed the title 【2021-077-07】用 JavaScript 写一个函数,输入 int 型,返回整数逆序后的字符串。如:输入整型 1234,返回字符串“4321”。要求必须使用递归函数调用,不能用全局变量,输入函数必须只有一个参数 【2021-07-07】用 JavaScript 写一个函数,输入 int 型,返回整数逆序后的字符串。如:输入整型 1234,返回字符串“4321”。要求必须使用递归函数调用,不能用全局变量,输入函数必须只有一个参数 Jul 7, 2021
@F-star
Copy link

F-star commented Jul 7, 2021

function reverse(str) {
  str = '' + str
  if (str.length <= 1) return str
  return reverse(str.slice(1)) + str[0]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants