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

o conclude our 6 Articles Series on ReactJS, here we are providing 51 Most Important and…

Use Node.js to control your Xmas Lights

Day 71: Animate GameObjects using Timeline

React Server Components Examined

Guide to Using Hooks in React Router

10 things about React

How to keep the Airbnb ESLint config when moving to TypeScript

Eliminating Shared Module(s)

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

How to create a LinkedList class

A screenshot of the initialized LinkedList class

Program a Linked List with Templates in C++

ARRAYS AND STRINGS DATA STRUCTURES.

SOLID Design Principles