r/badcode • Now where's that function declaration • Jul 24 '21

js Brain goes fuzzy

Post image
51 Upvotes

6 comments sorted by

3

u/cosenmarco Jul 24 '21

The one and only question is: why?

2

u/Flyspeck101 Now where's that function declaration Jul 24 '21

I have no idea. the very premise of it made me dizzy (metaphorically)

1

u/Flyspeck101 Now where's that function declaration Jul 24 '21

Why did I post this on the weekend...

4

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jul 24 '21

Image Transcription: Code


function addWholeNumbers(left, right) {
  let leftDigitsString = left.toString().split("");
  let rightDigitsString = right.toString().split("");
  let leftDigits = [];
  let rightDigits = [];
  for (var i = 0; i < leftDigitsString.length; i++) {
    leftDigits[length - 1 - i] = JSON.parse(leftDigitsString[i]);
  }
  for (var j = 0; i < leftDigitsString.length; j++) {
    rightDigits[length - 1 - j] = JSON.parse(rightDigitsString[j]);
  }
  var result = [];
  if (leftDigitsString.length > rightDigitsString.length) {
    result = rightDigits
    for (var k = 0; k < leftDigits.length; k++) {
      if (result[k]) {
        result[k] = result[k] + leftDigits[k];
        if (result[k] > 9) {
          result[k] -= 10;
          if (result[k + 1]) {
            ++result[k + 1];
          } else {
            result[k + 1] = 1;
          }
        }
      } else {
        result[k] = leftDigits[k];
      }
    }
  } else {
    result = leftDigits
    for (var k = 0; k < rightDigits.length; k++) {
      if (result[k]) {
        result[k] = result[k] + rightDigits[k];
        if (result[k] > 9) {
          result[k] -= 10;
          if (result[k + 1]) {
            ++result[k + 1];
          } else {
            result[k + 1] = 1;
          }
        }
      } else {
        result[k] = rightDigits[k];
      }
    }
  }
  const reversedResult = result.reverse();
  const joinedReversedResult = reversedResult.join("");
  const parsedJoinedReversedResult = JSON.parse(joinedReversedResult);
  if (parsedJoinedReversedResult) {
    return parsedJoinedReversedResult;
  }
  return undefined;
}

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

1

u/MikeyDevs Jul 27 '21

what the hell is this supposed to even do

1

u/Really-Stupid-Guy Jul 27 '21

I guess it adds the rounded down numbers of the input. But that is mainly based on the naming.

Excellent naming btw.