Javascript string conversion and array sorting

I am trying to make a call to a JavaScript learning website.

Instructions:

It was decided to assign "weight" to the figures. The weight of the number will now be from the sum of its digits. For example, 99 would have a "weight" of 18, 100 would have a "weight" of 1, so 100 would be up to 99 in the list. Given the string with the weights of the FFC members in normal order, can you give this string sorted by the "weights" of those numbers?

Example: a = "56 65 74 100 99 68 86 180 90", ordered by the weights of the numbers becomes: "100 180 90 56 65 74 68 86 99" When two numbers have the same "weight", let's classify them as if they would be strings, not numbers: 100 to 180, because its "weight" (1) is less than that of 180 (9) and 180 - up to 90, because, having the same "weight" (9), it precedes as a string. All numbers in the list are positive numbers and the list can be empty.

My attempt below:

function orderWeight(strng) {

  console.log(strng);
  if (strng === "") {
    return strng;
  } else {
    var arr = strng.split(" ").sort();
    console.log(arr);
    var keys = [];

    for (var i = 0; i < arr.length; i++) {
      var weight = 0;
      var current = arr[i];
      //loop through the array adding the digits of each number
      for (var j = 0; j < arr[i].length; j++) {
        var sNum = parseInt(arr[i].charAt[j]);
        weight += sNum;
        console.log(weight);
      }
      keys.push({
        weight: weight,
        number: current
      });
      console.log(keys);
    }
  }
};
      

Run codeHide result


The result of my code:

input string "103 123 4444 99 2000"
console.log(arr); = [ '103', '123', '2000', '4444', '99' ]
console.log(weight); = NaN

      

I understand that I will need to do more after I receive this part of the code, but if someone can just point me to make this part do what I want, I would very much admit it to her.

0


source to share


3 answers


You are using parentheses instead of parentheses when calling charAt

.

Change this:

var sNum = parseInt(arr[i].charAt[j]);

      



in

var sNum = parseInt(arr[i].charAt(j));

      

+5


source


Calculate the weight of each string by converting it to an integer and taking the sum of the digits:

var weight = 0,
    x = parseInt(parts[i], 10);
while (x != 0) {
  var digit = x % 10;
  weight += digit;
  x = (x - digit) / 10;
}

      

Then create objects containing each value and the string it came from:

sorted[i] = { weight: weight, string: parts[i] };

      

You can now sort objects using a compare function that uses weights if they are different and standard string comparisons otherwise:



sorted.sort(function (a, b) {
  if (a.weight != b.weight) {
    return a.weight - b.weight;
  }
  return a.string.localeCompare(b.string);
});

      

Finally, extract the lines and print them to show the result.

Demonstration:

function print(s) {
  document.write(s + '<br />');
}

function solve(s) {
  print('input: ' + s);
  var parts = s.split(' '),
      n = parts.length,
      sorted = new Array(n);
  for (var i = 0; i < n; ++i) {
    var weight = 0,
        x = parseInt(parts[i], 10);
    while (x != 0) {
      var digit = x % 10;
      weight += digit;
      x = (x - digit) / 10;
    }
    sorted[i] = { weight: weight, string: parts[i] };
  }
  sorted.sort(function (a, b) {
    if (a.weight != b.weight) {
      return a.weight - b.weight;
    }
    return a.string.localeCompare(b.string);
  });
  var newParts = new Array(n);
  for (var i = 0; i < n; ++i) {
    newParts[i] = sorted[i].string;
  }
  print('output: ' + newParts.join(' '));
}

solve("56 65 74 100 99 68 86 180 90");
      

Run codeHide result


+2


source


I would make a function to calculate the weights and then a custom sort function to sort your array.

Here's an example of how it works.

var input = "56 65 74 100 99 68 86 180 90";
var a = input.split(" ");
a.sort(function(x, y) {
  var xWeight = getWeight(x);
  var yWeight = getWeight(y);
  if (xWeight === yWeight)
    return x > y;
  else
    return xWeight > yWeight;
});

function getWeight(x) {
  var weight = 0;
  var arr = x.split("");
  arr.forEach(function(i) {
    weight += parseInt(i, 10);
  });
}

console.log(a);
      

Run codeHide result


+1


source







All Articles