C++ to javascript conversion

The question I'm doing is:

Write a JavaScript program to create a function which returns the number of times required to replace a given number with the sum of its digits until it converts to a single digit number. Like if given number is 123, number of times required to convert it into a single digit number is 1 (1+2+3=6). Your output code should be in the format console.log("Result is ", variableName)

I could not find the solution to this problem so I googled it and found this page.

The code on this page is in C/C++ ,Java etc...I took the C++ code and tried to convert it to javascript myself and this is the result:

var num=prompt("Enter a number");
var a= num.toString();

function test(x) 
{var temporary_sum = 0, count = 0; 

while (x.length() > 1) 
{ 
    temporary_sum = 0; 

    // computing sum of its digits 

    for (var i = 0; i < x.length(); i++) 
        temporary_sum += ( x[ i ] - '0' ) ; 

    // converting temporary_sum into string 
    // x again .

    x = temporary_sum.toString() ; 

    // increase the count 

    count++; 
 } 

 return count; 
} 

var output = test(a) ;
console.log("Result is: ", output);

This code does not give any output at all. How can I fix this? Is there a better way to do this question?

Here is a better way to do that using recursion. And reduce

function test(x,count=0){
  if(String(x).length === 1) return count;
  let sum = String(x).split('').reduce((ac,a) => Number(a) + ac,0);
  return test(sum,++count);
} 
console.log(test(123))  //1
console.log(test(456))  //2
console.log(test(99999999999))  //3

C Programming Tutorial for Beginners, Learn C the Hard Way: Practical Exercises on the Computational Subjects You Keep Avoiding (Like C) Discover historical prices for C stock on Yahoo Finance. View daily, weekly or monthly format back to when Citigroup, Inc. stock was issued.

I'll answer your last question - yes, there is a better way to do this question. You want to use recursion. You can also split the string on '' to convert its digits into an array, and you want to use parseInt to turn it back into a number.

"C" Programming Language: Brian Kernighan, This course will give you a full introduction into all of the core concepts in the C programming Duration: 3:46:13 Posted: Aug 15, 2018 C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system. C is the most widely used computer language. It keeps fluctuating at number one scale of popularity

I will use a different approach (I don't say it would be better). First, I will skip the mapping from number to string and wraps the logic that sums the digits of some number into a function called sumDigits(). This way you have a resusable method you can use for other purposes later. The second step is to define your test function using the previously created sumDigits mainly using the while loop you already have but testing with another condition and generalized to accept also integer negative numbers:

const sumDigits = (num) =>
{
    let sum = 0;

    while (num)
    {
        sum += num % 10, num = Math.floor(num / 10);
    }

    return sum;
}

const test = (num) =>
{
    let counter = 0;
    num = Math.abs(num);

    while (num >= 10)
    {
       num = sumDigits(num), counter++;
    }

    return counter;
}

console.log(test(123));
console.log(test(456));
console.log(test(-789));

Learn C Programming, "C" is one of the most widely used programming languages of all time. Prof Brian Kernighan Duration: 8:26 Posted: Aug 18, 2015 Programming Languages Development - C++ has been used extensively in developing new programming languages like C#, Java, JavaScript, Perl, UNIX’s C Shell, PHP and Python, and Verilog etc. Computation Programming - C++ is the best friends of scientists because of fast speed and computational efficiencies.

C Tutorial, Tutorials Examples References Compiler. C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on. Learn C# programming - for beginning developers, developers new to C#, and experienced C# / .NET developers

C Language - Overview, C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone� C provides a compound assignment operator for each binary arithmetic and bitwise operation (i.e. each operation which accepts two operands). Each of the compound bitwise assignment operators perform the appropriate binary operation and store the result in the left operand.

Learn C, C Language - Overview - C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system � C - switch statement - A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is chec

Comments
  • The code works for me if I take the parens off of x.length (it's a property, not a method). The answer is incorrect as far as I can tell, but the code runs and gives an output.
  • Your code should throw an error because x.length is not a function. Do you not see an error in the console?
  • the console stays completely empty but that might just be some issue with the website I'm practicing this on
  • "Here is a better way to do that" Can you clearly define "better"? How is "better" objectively measured?
  • @guest271314 By better I mean its clean and than regular for or while loop
  • What do you mean by "clean"? What is the issue with "regular for or while loop"?
  • @guest271314 I mean recursion is better than 1)creating a variable temporary_sum 2)then using this ugly thing for (var i = 0; i < x.length(); i++) 3) And then creating a nested for loop is while loop.I can be wrong
  • "I mean recursion is better than 1)creating a variable temporary_sum 2)then using this ugly thing for (var i = 0; i < x.length(); i++) 3) And then creating a nested for loop is while loop." That is your opinion. How is "better" objectively measured?