toString inside js function returns undefined and 0 value

toString inside js function returns undefined and 0 value

我有为 Codewars 测试编写的函数:

Your task is to make a function that can take any non-negative integer as a argument and return it with it's digits in descending order. Descending order means that you take the highest digit and place the next highest digit immediately after it.

这是我的函数:

function descendingOrder(n) {
  // convert number into string
  var toStr = n.toString();
  console.log("converted to string:");
  console.log(n);

  // split string at "decimal" point
  strArray = toStr.split(".");
  console.log("split string");
  console.log(strArray);

  // create new array from decimal numbers
  strArray.splice(0,1);
  console.log("Array after split: " + strArray);

  // split into array
  for (var i=0; i<strArray.length; i++) {
    var splitStrArray = strArray[i].split("");
  }

  console.log("new split str Array:");
  console.log(splitStrArray);

  // loop array and: 1) convert to number 2) push to new array of numbers
  var numArray = [];
  for (var j=0; j<splitStrArray.length; j++) {
    numArray.push(Number(splitStrArray[j]));
  }

  // sort in descending order
  numArray.sort(function(a,b){return b-a});
  console.log("new array of numbers:");
  console.log(numArray);
}

descendingOrder(1.45312798);

第一题: 查找未定义的结果我收到以下错误:

TypeError: Cannot read property 'length' of undefined at descendingOrder at Object.exports.runInThisContext

我知道未定义的值来自于将 n.toString 的结果赋给一个变量。

我尝试了以下方法:

var toStr = '';
toStr = n.toString();

但无济于事。


第二题: 有一个输出值 0 正在通过我的函数

在发布此问题之前,我做了某些 MDN 和其他问题。非常欢迎对我关于 Codewars 挑战的逻辑发表评论或批评。在此先感谢您的帮助。

是这样的吗?

function descendingOrder(n) {
  console.log(n);

  // convert number into string
  var toStr = n.toString();

  // split string into array
  strArray = toStr.split("");

  var t = strArray.indexOf('.') + 1;

  // get decimal digits
  var newStr = strArray.splice(t);

  // arrange values by descending order
  newStr = newStr.sort().reverse();

  var sortedNo = strArray.splice(0, t).concat(newStr).join('');

  // print the sorted value
  console.log(sortedNo);
}

descendingOrder(1.45312798);

这个函数完成所有工作:

function result(n) {
    var digs=[];
    if (n.toString().indexOf('.')>-1) {
        var m=n.toString().slice(0,n.toString().indexOf('.'));
    } else {
        var m=n.toString();
    }
    for (var i=0;i<m.length;i++) {
        digs.push(parseInt(m[i]));
    }
    return parseInt(digs.sort().reverse().join(''));
}
console.log(result(16435.43)) // 65431
console.log(result(16433153)) // 65433311

不是问题的答案,但我认为它可以更短。你说 非负整数 但后来在你的代码中使用 1.45312798,所以我使用了你的示例值。不需要太多就可以让它只使用一个整数(会短很多)。

var n = 1.45312798;

function sortNumb(n) {
    var p = n.toString().split(".");
    var s = (p[1]) ? "."+ p[1].split("").sort().reverse().join("") : "";
    console.log(p[0]+s);
}
sortNumb(n);

带有一些 ES6 的两个紧凑版本

//a utility
var sortNumericDecending = (a,b) => b-a;

//simple version that works with positive ints
function descendingOrder(n){
    return +String(n).split("").sort(sortNumericDecending).join("");
}

//takes floats and orders the decimal places
function descendingOrder(n){
    var [int, decimals] = String(+n).split(".");
    return decimals? 
        +(int + "." + decimals.split("").sort(sortNumericDecending).join("")):
        n;
}

//or the same without Array destructuring
function descendingOrder(n){
    var parts = String(+n).split(".");
    return parts.length > 1? 
        +(parts[0] + "." + parts[1].split("").sort(sortNumericDecending).join("")):
        n;
}

除了你的问题,还不清楚你想要哪个。