We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
用 JavaScript 写一个函数,输入 int 型,返回整数逆序后的字符串。如:输入整型 1234,返回字符串“4321”。要求必须使用递归函数调用,不能用全局变量,输入函数必须只有一个参数传入,必须返回字符串。
方法一: 依次将最后一位插入最前端
function fun(num){ let num1 = num / 10; //用/10来减少位数 let num2 = num % 10; //用%10来取最后一位 if(num1<1){ //一位直接返回 return num; }else{ num1 = Math.floor(num1) //向下取整 return `${num2}${fun(num1)}` // 5{fun(1234)} 54{fun(123)} 依次类推 } } var a = fun(12345) console.log(a) //54321 console.log(typeof a) //string
方法二:依次第一位插入最后
function numberReverse(num) { const str = num.toString() return str.length === 1 ? str : numberReverse(str.substring(1)) + str.substring(0, 1) } numberReverse(12345) // "54321"
The text was updated successfully, but these errors were encountered:
No branches or pull requests
用 JavaScript 写一个函数,输入 int 型,返回整数逆序后的字符串。如:输入整型 1234,返回字符串“4321”。要求必须使用递归函数调用,不能用全局变量,输入函数必须只有一个参数传入,必须返回字符串。
方法一: 依次将最后一位插入最前端
方法二:依次第一位插入最后
The text was updated successfully, but these errors were encountered: