Ahmad Zughayyer Ahmad Zughayyer - 1 year ago 77
Javascript Question

Converting numbers to a string number

function foo(num1,num2,num3){
var result ="";
var sum = 0;

sum = (num1*num2*num3);

for(var i =sum; i > 0; i--){
var str = i.toString();

for(var j=0; j<str.length;j++){
result = str[j] + result;

if(result === str){
return result;

I am trying to convert the number from the first for loop so i can use the same number in the second for loop as a string. because I want to access the index of the number. for instance,
var str = "1234";
str[0] = 1;
but, when the second loop runs, it will return unrelated output
does anyone know how to fix it ? please

Answer Source
var num = 123; // The value of num is 123 - this is a number
var sum = num.toString(); // The value of sum is "123" - this is a string

console.log(num) // This logs 123 - this is a number
console.log(sum) // This logs "123" - this is a string

sum.length // This logs 3
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download