+ 7

How to add up numbers by prompt?

Hey guys and gals, I just wanna receive a number from users by the function prompt while the numbers are binary or so, to add each one together. You can take this as an example. The number is 34, I want to split it into two, like 3,4, then 3+4=7.

29th Sep 2020, 7:55 PM
Saleh Ghazimoradi
Saleh Ghazimoradi - avatar
4 odpowiedzi
29th Sep 2020, 8:11 PM
Sousou
Sousou - avatar
+ 4
Yeah, man. You hit the nail on the head. It is the thing I was looking for
29th Sep 2020, 8:16 PM
Saleh Ghazimoradi
Saleh Ghazimoradi - avatar
+ 4
Thank ya so much
29th Sep 2020, 8:16 PM
Saleh Ghazimoradi
Saleh Ghazimoradi - avatar
+ 2
I'm going to explain it in python(python and JavaScript syntax are similar you might understand it). Algorithm By default when we receive input from user we get the input in a string format. So create a for loop to loop through the input provided. Create a global variable (list type variable). In the loop convert the loop variable to int() type. Append the loop variable to the list type global variable created. Once done. Create another for loop and loop through the global list type variable. Create another int type global variable (with a default value of 0). Inside your second for loop set the int type global variable to += the second for loop variable.
29th Sep 2020, 8:15 PM
Light
Light - avatar