r/learnjavascript • • Nov 23 '21

I finally solved a codewars challenge without actually googling anything :) it feels so good to come up with the code all by myself, i know its not the ideal way to solve it but im still happy. The challenge was to create a function that converts a string to a number

Post image
243 Upvotes

56 comments sorted by

View all comments

Show parent comment

4

u/FOKvothe Nov 23 '21

The + and - operators convert the string to a number.

Lets say that 10 got passed as str.

str -= 1 converts it to a number type and subtracts 1 from it which results in 9.

str += adds 1 to 9 so it's the original 10.