DEV Community is a community of 890,060 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Posted on

What’s your alternative solution? Challenge #3

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.

``````

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

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);
}

``````

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.

Discussion (3)

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
``````
Tony B

If you wanted that sort of flexibility, where the caller is going to format the data, or even filter it, etc. I would consider a generator, not callbacks.

e.g.

``````
function* multiplicationTable(limit, number) {
for (let current = 1; current <= limit; current++)
yield { number, current, result: number * current };
}

for (let line of multiplicationTable(10, 7)) {
console.log(`\${line.number} * \${line.current} = \${line.result}`);
};
``````
Horacio Rivero • Edited on
``````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)
``````