Solving Popular Algorithms: Add Strings

Me, struggling to solve this problem
Remember this?
let arr1 = num1.split("")
let arr2 = num2.split("")
let numZs = 0
if (num1.length > num2.length) {
numZs = num1.length - num2.length
while (numZs > 0) {
arr2.unshift("0")
numZs--
}
} else {
numZs = num2.length - num1.length
while (numZs > 0) {
arr1.unshift("0")
numZs--
}
}
let results = []
let secondDig = 0
for (let i = arr1.length-1; i >= 0; i--) {
let sumNum = parseInt(arr1[i]) + parseInt(arr2[i])
if (sumNum >= 10) {

arr1[i-1] = (parseInt(arr1[i-1]) + 1).toString()
let gettingSecondDig = sumNum.toString().split("")
secondDig = gettingSecondDig[1]
if (i === 0) {
results.unshift(sumNum.toString())
} else {
results.unshift(secondDig)
}
} else {
results.unshift(sumNum.toString())
}
}
addStrings = (num1, num2) => {
let arr1 = num1.split("")
let arr2 = num2.split("")
let numZs = 0
if (num1.length > num2.length) {
numZs = num1.length - num2.length
while (numZs > 0) {
arr2.unshift("0")
numZs--
}
} else {
numZs = num2.length - num1.length
while (numZs > 0) {
arr1.unshift("0")
numZs--
}
}

let results = []
let secondDig = 0
for (let i = arr1.length-1; i >= 0; i--) {
let sumNum = parseInt(arr1[i]) + parseInt(arr2[i])
if (sumNum >= 10) {

arr1[i-1] = (parseInt(arr1[i-1]) + 1).toString()
let gettingSecondDig = sumNum.toString().split("")
secondDig = gettingSecondDig[1]
if (i === 0) {
results.unshift(sumNum.toString())
} else {
results.unshift(secondDig)
}
} else {
results.unshift(sumNum.toString())
}
}

return results.join("")
}
Me, now that I’ve solved this problem

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A nitty gritty details about Node.js’s modules & packages

How Single Page Applications Broke Web Design

10 React Basic Fundamentals Concept.

How Create Chatbot for Website using jQuery

Laravel’s URL::to() vs URL::asset()

Redux Data Flow

4 Practical Use Cases for IIFEs in JavaScript

Angular (v4) + Typescript Easy REST

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
HopeGiometti

HopeGiometti

More from Medium

Why do array indexes mostly start at 0?

Aiming for object-oriented design elegance

Blind 75 Javascript Edition — Dynamic Programming (Part 2)

Solve: Merge Sorted Array