简体   繁体   English

我想编写一个可以反转数字的JavaScript函数

[英]I want to write a JavaScript function that reverse a number

I'm learning JavaScript and I really want to understand why this function is not working. 我正在学习JavaScript,我真的很想了解为什么此功能无法正常工作。 Please explain me what am I doing wrong here and how I could make it work. 请在这里向我解释我在做什么错以及如何使它工作。 I really want to keep it simple like it is so I can understand what I'm doing wrong and right. 我真的很想保持它的简单,这样我就可以了解自己在做错对与错。

var input = prompt("Write a number");

function around() {
    for (var x = 0; x < input.lenght ; x++) {
        console.log(input.reverse());
    }
}

around();

This should do the trick. 这应该可以解决问题。 Strings don't have a reverse() method but arrays do: 字符串没有reverse()方法,但是数组有:

function around() {
    var input = prompt("Write a number");

    if (!parseInt(input)) {
        return null; // Or do something with bad input
    }

    return parseInt(input.split('').reverse().join(''));
}

When I read your function, it seems like it would print your reversed string n times, where n is the length of your string. 当我阅读您的函数时,似乎它将打印反转的字符串n次,其中n是字符串的长度。 In any case, it looks like you're trying to reverse a string, so let's move on. 无论如何,您似乎都想反转一个字符串,所以让我们继续。

There is no "reverse" function built-in to Javascript to reverse strings. Javascript中没有内置的“反向”功能来反向字符串。 There is, however, a way to reverse arrays (using .reverse()). 但是,有一种方法可以反转数组 (使用.reverse())。 You can split your string into an array quite easily, ie using input.split(""). 您可以很容易地将字符串拆分为一个数组,即使用input.split(“”)。 You can then reverse the array, and call .join("") on it to turn your array back into a string again. 然后,您可以反转数组,并在其上调用.join(“”)以将数组再次变回字符串。

But I can't really be sure what you want your function to do if you don't say anything about it. 但是我真的不确定如果您什么都不说,您希望函数执行什么操作。

What is returned from prompt is a String type, and reverse() Method is used for Array type. 从提示符返回的是String类型,而reverse()方法用于Array类型。

So the most simple way to reverse a string is to split it into an array of characters, reverse it, join the characters back into a string, you can then parse that into a number if you would like. 因此,反转字符串的最简单方法是将其拆分为字符数组,反转,将字符重新组合为字符串,然后可以根据需要将其解析为数字。

 var input = prompt("Write a number"); function around() { input = parseInt(input.split('').reverse().join('')); console.log(input); } around(); 

Your problem here is that input is a string, and the string prototype has no reverse method. 您的问题是输入是一个字符串,并且字符串原型没有reverse方法。 To accomplish this you'd have to do something like input.split('').reverse().join('') . 为此,您必须执行类似input.split('').reverse().join('') split('') converts a string to an array of characters. split('')将字符串转换为字符数组。 reverse() is an Array method that reverses an array in place. reverse()是一个Array方法,用于在适当位置反转数组。 join('') combines each element of an array into a string, adding the argument in between each array element (an empty string in this case). join('')将数组的每个元素组合成一个字符串,并在每个数组元素之间添加参数(在这种情况下为空字符串)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM