Build a Calorie Counter - Step 27

Tell us what’s happening:

Please see the console statement inside clearInputString. Is it also a way to concatenate strings in JS ? I tried to search online but it didn’t helped.

Your code so far

<!-- file: index.html -->

/* file: styles.css */

/* file: script.js */
// User Editable Region

function cleanInputString(str) {
  console.log("original string: ", str);
  const regex = /[+-\s]/g;
  return str.replace(regex, '');
}

console.log(cleanInputString("+-99"));

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36

Challenge Information:

Build a Calorie Counter - Step 27

no, it this is not string concatenation in JS

the console.log method accepts multiple arguments.

in the console it will log each argument separated by a space.

Give this a try and you will see an error in JavaScript.

function cleanInputString(str) {
  let newStr = "original string: ", str

  const regex = /[+-\s]/g;
  return str.replace(regex, '');
}

console.log(cleanInputString("+-99"));

That line is attempting to declare multiple variables.

If I change it to this, then the error goes away and you will see what those variables are logged as in the console.

  let newStr = "original string: ", something;
  console.log(newStr)
  console.log(something)

From the MDN docs, it says this about passing multiple arguments to console.log

  • Pass in a variable number of arguments whose string representations get concatenated into one string, then output to the console.

So console.log will format the output in that way. But this is not a way to concatenate strings in JS.

Hope that helps

Thank you very much, I logged your given example and now it is absolutely clear to me. Thank you :slight_smile: