w3resource

JavaScript: Multiply every digit of a number three times

JavaScript Math: Exercise-81 with Solution

Write a JavaScript program to multiply every digit of a number three times.

Test Data:
(11) -> 11
(66) -> 216216
(336) -> 2727216
(444) -> 646464
(1151) -> 111251

Sample Solution:

JavaScript Code:

/**
 * Function to multiply every digit of a number three times and concatenate the results.
 * @param {number} n - The input number.
 * @returns {number} - The result of multiplying every digit of the input number three times.
 */
function test(n) {
    // Convert the number to a string, map each digit to its cube, and join them as a string
    return +[...String(n)].map(x => x * x * x).join('');
}

// Test cases
// Assign value to n
n = 11;
// Print the value of n
console.log("n = " + n);
// Multiply every digit of the said number three times
console.log("Multiply every digit of the said number three times: " + test(n));
// Repeat the above steps for different values of n
n = 66;
console.log("n = " + n);
console.log("Multiply every digit of the said number three times: " + test(n));
n = 336;
console.log("n = " + n);
console.log("Multiply every digit of the said number three times: " + test(n));
n = 444;
console.log("n = " + n);
console.log("Multiply every digit of the said number three times: " + test(n));
n = 1151;
console.log("n = " + n);
console.log("Multiply every digit of the said number three times: " + test(n));

Output:

n = 11
Multiply every digit of the said number three times: 11
n = 66
Multiply every digit of the said number three times: 216216
n = 336
Multiply every digit of the said number three times: 2727216
n = 444
Multiply every digit of the said number three times: 646464
n = 1151
Multiply every digit of the said number three times: 111251

Flowchart:

JavaScript: Multiply every digit of a number three times.

Live Demo:

See the Pen javascript-math-exercise-81 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus.

Previous: Check an integer is Repdigit number or not.
Next: Mean of all digits of a number

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.