0

I am having a range variable which takes a single integer and then I want to print the table of 9 till that range. So I am able to print the table of 9 successfully but I want it to be printed on a single line with space between them. May I please get an explanation on this as I am a beginner in Javascript.

This is my code 👇

  let range = 3; 
    
  for (i=1; i<=range; i++) {
    final_res = 9 * i;
    console.log(final_res)
  }   
1
  • 1
    You're executing console log 3 times (1,2,3). console.log prints a new line. Commented May 13, 2021 at 7:36

2 Answers 2

1

Put everything in an array then print it

  let range = 3; 
  const values = []
  for (i=1; i<=range; i++) {
    final_res = 9 * i;
    values.push(final_res)
  }   
  console.log(values.join())

Sign up to request clarification or add additional context in comments.

2 Comments

In the end, I'm getting whitespace so can you please help me out in understanding this.
console.log prints a new line at the end it's normal
1

Create a function to calculate the table. Instead of printing each result , append them with a space and print final result

let range = 3;

function printTable(multiplier, multiplicant = 9) {
  let final_res = ''
  for (let i = 1; i <= multiplier; i++) {
    final_res += `${multiplicant * i} `
  }
  return final_res;
}

console.log(printTable(range))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.