LearnFrenzy
Sign in Take a free test

Variables: var, let and const · Variables Coding Problems · Question 3

Multiplication table in JavaScript Solution, output & explanation

  • Program
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

Print the multiplication table of 7 from 1 to 10 in the form "7 x 1 = 7", right-aligning the results.

Solution
main.js
const n = 7;
for (let i = 1; i <= 10; i++) {          // let: the counter changes every iteration
  console.log(`${n} x ${String(i).padStart(2)} = ${String(n * i).padStart(2)}`);
}

Output Verified by running it on Node.js

7 x  1 =  7
7 x  2 = 14
7 x  3 = 21
7 x  4 = 28
7 x  5 = 35
7 x  6 = 42
7 x  7 = 49
7 x  8 = 56
7 x  9 = 63
7 x 10 = 70

A classic for loop needs let because i++ reassigns the counter. padStart(2) keeps the columns aligned for one- and two-digit numbers.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links