DEV Community

Adrian
Adrian

Posted on

What’s your alternative solution? Challenge #3

About this series

This is series of daily JavaScript coding challenges... for both beginners and advanced users.

Each day I’m gone present you a very simple coding challenge, together with the solution. The solution is intentionally written in a didactic way using classic JavaScript syntax in order to be accessible to coders of all levels.

Solutions are designed with increase level of complexity.

Today’s coding challenge

Print the multiplication table with 7.

Enter fullscreen mode Exit fullscreen mode

(scroll down for solution)

Code newbies

If you are a code newbie, try to work on the solution on your own. After you finish it, or if you need help, please consult the provided solution.

Advanced developers

Please provide alternative solutions in the comments below.

You can solve it using functional concepts or solve it using a different algorithm... or just solve it using the latest ES innovations.

By providing a new solution you can show code newbies different ways to solve the same problem.

Solution

// Solution for challenge03

for(var i = 1; i <= 10; i++)
{
    var row = "7 * " + i + " = " + 7 * i;
    println(row);
}

Enter fullscreen mode Exit fullscreen mode

To quickly verify this solution, copy the code above in this coding editor and press "Run".

Note: The solution was originally designed for codeguppy.com environment, and therefore is making use of println. This is the almost equivalent of console.log in other environments. Please feel free to use your preferred coding playground / environment when implementing your solution.

Top comments (3)

Collapse
 
aminnairi profile image
Amin

I prefered to return an array of objects containing all the part of the multiplication, and let the developer-user of the function choose how to format the output, that way it makes it an O(n) solution.

"use strict";

const printMultiplicationTable = (limit, number, callback, ...parameters) => {
    if (0 !== parameters.length) {
        throw new ReferenceError("too much parameters");
    }

    if ("number" !== typeof limit) {
        throw new TypeError("first argument expected to be a number");
    }

    if (!Number.isInteger(limit)) {
        throw new TypeError("first argument expected to be an integer");
    }

    if (1 > limit) {
        throw new RangeError("first argument expected to be greater than zero");
    }

    if ("number" !== typeof number) {
        throw new TypeError("second argument expected to be a number");
    }

    if (!Number.isInteger(number)) {
        throw new TypeError("second argument expected to be an integer");
    }

    if (1 > number) {
        throw new RangeError("second argument expected to be greater than zero");
    }

    if ("function" !== typeof callback) {
        throw new TypeError("third argument expected to be a function");
    }

    for (let index = 0; index < limit; index++) {
        const current = index + 1;

        if (1 !== callback.length) {
            throw new Error("third argument expect to be a function receiving only one argument");
        }

        callback({
            operand1: number,
            operand2: current,
            result: number * current
        });
    }
}

printMultiplicationTable(10, 7, ({operand1, operand2, result}) => {
    console.log(`${operand1} * ${operand2} = ${result}`);
});

// 7 * 1 = 7
// 7 * 2 = 14
// 7 * 3 = 21
// 7 * 4 = 28
// 7 * 5 = 35
// 7 * 6 = 42
// 7 * 7 = 49
// 7 * 8 = 56
// 7 * 9 = 63
// 7 * 10 = 70
Enter fullscreen mode Exit fullscreen mode
Collapse
 
hume profile image
Horacio Rivero • Edited
const print = (i, b) => console.log(`${b.toString()} * ${i} ${b * i}`)
const end = (i) => i > 1; 
const printMultTable = (i, b) => end(i) && (printMultTable(--i, b), print(i, b)); 

printMultTable(11, 7)