+ 1
How to make function that remembers all numbers in a number?
(sorry for bad english. Its not my first language.) Example: INPUT:123 Output: a=1,b=2,c=3 etc. Also i forgot some of the Javascript so please dont judge me. Here is my version of the code: <html> <script> function dividenums (n){ for (var i=10;a != n%i;i*10){ var a = n%i; var div = i; } return div; } var no = dividenums(123); alert(no); </script> </html> I tried..... Any help would be helpful!
5 Antworten
+ 3
I am not an JavaScript expert
But looks like you are only returning one number from the function which would be the last digit of the input. Instead you should be returning an array/list of numbers
+ 1
Take the input as a string e.g. use prompt() or get some <input> element value. You then check whether all the character in onput string was numeric, convert to number as necessary using parseInt() or Number() construct
You can save all the digits into an array and return the array.
0
Stefan
You are using a condition which is true at the start of for loop
a!=n%a
and made it false by doing this
a = n%i
Which stops the for loop from executing
And you return div which is i which is 10.
I edited your code and added comments so that it's easy to understand.
Check this code:
https://code.sololearn.com/WLEP60UofbFf/?ref=app
0
Idk why but it didnt work.
0
Stefan
What didn't work?
I didn't get your point.
In that edited code I added a new Function editedDivide() which works perfectly fine. It saves all the digits to an array and you can return that array to a global variable, which can be accessed outside that function too.
Please check my code again.
https://code.sololearn.com/WLEP60UofbFf/?ref=app