-2

The following line in the swapDigits function below does not seem to work to convert the number a negative number.

swappedNumber = Number(numberStringArray.join("")) * -1;

If I multiply a positive number by -1 in the console it correctly makes the number negative but that does not seem to work in the code below. How can I make the swappedNumber negative? Thank you.

// swapDigits 

const swapDigits = (number) => {
  let swappedNumber = 0;

  if (number < 0) {
    number = Math.abs(number);

    const numberString = number.toString();
    console.log(numberString);
    const numberStringArray = [];

    if (numberString.length % 2 == 0) {
      for (let i = 0; i < numberString.length; i = i + 2) {
        numberStringArray.push(numberString[i + 1]);
        numberStringArray.push(numberString[i]);
      }
      swappedNumber = Number(numberStringArray.join(""));
    } else {
      numberStringArray.push(numberString[0]);
      for (let i = 1; i < numberString.length; i = i + 2) {
        numberStringArray.push(numberString[i + 1]);
        numberStringArray.push(numberString[i]);
      }
      swappedNumber = Number(numberStringArray.join("")) * -1;
    }
  } else {
    const numberString = number.toString();
    console.log(numberString);
    const numberStringArray = [];

    if (numberString.length % 2 == 0) {
      for (let i = 0; i < numberString.length; i = i + 2) {
        numberStringArray.push(numberString[i + 1]);
        numberStringArray.push(numberString[i]);
      }
      swappedNumber = Number(numberStringArray.join(""));
    } else {
      numberStringArray.push(numberString[0]);
      for (let i = 1; i < numberString.length; i = i + 2) {
        numberStringArray.push(numberString[i + 1]);
        numberStringArray.push(numberString[i]);
      }
      swappedNumber = Number(numberStringArray.join(""));
    }
  }

  return swappedNumber;
};


const even = swapDigits(-564783);
console.log(even); // logs 657438 instead of -657438

const odd = swapDigits(512364783);
console.log(odd);
larry8989
  • 339
  • 1
  • 11
  • How specifically have you identified that exact line as the source of the problem? If that exact line is the source of the problem, how is the rest of the code relevant? What exact values are observed on that exact line and what is the exact result? This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? – David Mar 23 '23 at 17:47

2 Answers2

0

The first branch of your "if" doesn't multiply the number by -1:

if (numberString.length % 2 == 0) {
      for (let i = 0; i < numberString.length; i = i + 2) {
        numberStringArray.push(numberString[i + 1]);
        numberStringArray.push(numberString[i]);
      }
      swappedNumber = Number(numberStringArray.join(""));
}

So if there's an even number of digits, you won't negate the number.

Jon S.
  • 1,378
  • 8
  • 14
0

You can simplify your logic by capturing the sign, before calling absolute value.

You can also swap without a temporary variable by using an XOR swap.

const swapDigits = (n) => {
  const
    sign = n < 0 ? -1 : 1,
    digits = Math.abs(n).toString(10).split('').map(d => parseInt(d, 10));
  for (let i = 0; i < digits.length; i+=2) {
    if (digits[i + 1]) {
      digits[i]     ^= digits[i + 1];
      digits[i + 1] ^= digits[i];
      digits[i]     ^= digits[i + 1];
    }
  }
  return sign * parseInt(digits.join(''), 10);
};

console.log(swapDigits(-564783));   // even = -657438
console.log(swapDigits(512364783)); // odd  = 1532468703
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132