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.
More Variables: var, let and const practice questions
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·
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!