Ahmad Zughayyer Ahmad Zughayyer - 6 months ago 9
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();
console.log(str);

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
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