How to make console log this variable?

427 views javascript
1

beginning programmer here. If your reaction is "really...?" yeah, really. Sorry.

Im trying to write a function which logs the table of variable a in the console. I would be able to change the table logged by changing the argument in the function.

It keeps giving me the error code index.js: Unexpected token, expected ; (2:6) no matter how I change the code.

Function:

    function table(a) {
  Let a = 3
  console.log(`1 * ${a}, 2 * ${a}, 3 * ${a}, 4 * ${a}, 5 * ${a}, 6 * ${a}, 7 * ${a}, 8 * ${a}, 9 * ${a}, 10 * ${a}`)
}

table(a)

answered question

let not Let js is case sensitive

More than that, you won't be able to declare a with the statement let because the param has the same name.

@Ele How would i go about correcting this?

Couple points to share here, firstly, back-ticks will convert everything to a string so it will not evaluate the expression. Secondly, if you actually want the evaluation of every expression then do something like console.log(1 * a, 2 * a...)

Learn to use your developer console, error should have been listed as "Uncaught SyntaxError: Unexpected identifier" Click on it takes you to that line. You can use tools like eslint, jshint, etc to point out errors in your IDE.

2 Answers

7

Javascript is case-sensitive, so the statement letmust be written in lower-case. Likewise, what you really want to do is declare the variable a outside of function table.

function table(a) {
  console.log(`1 * ${a}, 2 * ${a}, 3 * ${a}, 4 * ${a}, 5 * ${a}, 6 * ${a}, 7 * ${a}, 8 * ${a}, 9 * ${a}, 10 * ${a}`)
}

let a = 3;
table(a);

posted this
10

function table(a) {
    console.log(`1 * ${a}, 2 * ${a}, 3 * ${a}, 4 * ${a}, 5 * ${a}, 6 * ${a}, 7 * ${a}, 8 * ${a}, 9 * ${a}, 10 * ${a}`);
}
let a = 3;
table(a);

Here's corrected code, I think, because I don't know why you had let a = 3 in the function body

posted this

Have an answer?

JD

Please login first before posting an answer.