Untitled

mail@pastecode.io avatar
unknown
javascript
2 years ago
1.6 kB
3
Indexable
Never
const compareAndFill = (a, b) => {
  let number = [[], []];
  if (a.length > b.length) {
    const diff = a.length - b.length;
    for (var i = 0; i < a.length; i++) {
      if (i < diff) {
        number[1][i] = 0;
      } else {
        number[1][i] = parseInt(b[i - diff]);
      }
      number[0][i] = parseInt(a[i]);
    }
  } else if (a.length < b.length) {
    const diff = b.length - a.length;
    for (var i = 0; i < b.length; i++) {
      if (i < diff) {
        number[0][i] = 0;
      } else {
        number[0][i] = parseInt(a[i - diff]);
      }
      number[1][i] = parseInt(b[i]);
    }
  } else {
    for (var i = 0; i < b.length; i++) {
      number[0][i] = parseInt(a[i]);
      number[1][i] = parseInt(b[i]);
    }
  }
  return number;
};

const fetchNeigbor = (numbers, k) => {
  let found = false;
  for (var i = 0; i < numbers.length; i++) {
    const j = numbers.length - i - 1;
    if (numbers[j] > 1) {
      numbers[j] -= 1;
      found = true;
    }
  }
  if (found) {
    numbers[k] += 10;
  }
  return numbers;
};

const subtract = (numbers) => {
  let sol = new Array(numbers[0].length).fill(0);
  // console.log(sol);
  for (var i = 0; i < numbers[0].length; i++) {
    const j = numbers[0].length - i - 1;
    const a = numbers[0][j];
    const b = numbers[1][j];
    if (a > b) {
      sol[j] = a - b;
    } else if (a === b) {
      sol[j] = 0;
    } else {
    }
  }
  return sol;
};

const main = (a, b) => {
  const numbers = compareAndFill(a, b);
  const sol = subtract(numbers);
  console.log(sol);
};

const num = fetchNeigbor([1, 0, 0, 0], 3);
console.log(num);
// main("8", "2");