Not sure why my JS code is wrong

The JS exericises are going slowly for me, and I wanted to get some more practice with . So I found some beginning exercises at Hacker Rank.

I can’t pass their first test. I tested my code in the console, and it looks like I have it correct. But they say it’s wrong. I’d be grateful if anyone can tell me what’s going on. Below is what I have:

/**
*   A line of code that prints "Hello, World!" on a new line is provided in the editor. 
*   Write a second line of code that prints the contents of 'parameterVariable' on a new line.
*
*	Parameter:
*   parameterVariable - A string of text.
**/
function greeting(parameterVariable) {
    // This line prints 'Hello, World!' to the console:
    console.log('Hello, World!');
    console.log('Welcome to 10 Days of JavaScript!');
        // Write a line of code that prints parameterVariable to stdout using console.log:

}
greeting();

Here is the error message I get when I try to run it on Hacker Rank:

Wrong Answer!

Your output:

  • Hello, World!
  • Welcome to 10 Days of JavaScript!
  • Hello, World!
  • Welcome to 10 Days of JavaScript!

Expected output:

  • Hello, World!
  • Welcome to 10 Days of JavaScript!

I don’t know if I need to start learning JavaScript from the beginning, or if there’s some bug on their end.

Hello~!

I don’t have an account, so I can’t say for sure - but I would guess that your greeting() call on the bottom line is messing with the tests. You call the function, so it prints to the console, and then the tests call the function, so it has printed twice.

I’m not sure why it prints twice. You do have to call the function to run the function. But also you are hard coding what you log rather than using the parameterVariable.

Without trying to give just give a complete answer hints would be to pass in the parameter to your function call:

greeting("text you want to print out")

and in the function itself use the variable like

function greeting(variable) {
console.log(variable)
}

That was it. I assumed I had to call it. But I guess when I hit run, that calls it, too.
Thanks for your help

Just FYI, for your learning experience. the point of the exercise is to use the parameterVariable. You hard coded the value

console.log('Welcome to 10 Days of JavaScript!');

What the point of the exercise was for you to learn to do it this way

console.log(parameterVariable) 

And that make the function reusable, which is what you want. Hard coding it like you did makes the whole function meaningless because you could just log anything you want without the need to call a function.

Thank you. I get what you’re saying about making the function reusable vs hard coding it. But…

I tried running this:

function greeting(parameterVariable) {
    
    console.log(parameterVariable);
    
        // Write a line of code that prints parameterVariable to stdout using console.log:

}
greeting('Hello, World!\nWelcome to 10 Days of JavaScript!');

This runs correctly when I put in my console, but I get an error message from their program. I’m sure there’s another way to do it that passes their test, but it does seem like this yields the correct output.

I think the error message is because in their “program” or “environment “ you don’t need to call the function, it is done behind the scenes for you (so when you call it it gets called twice and their tests see that as an error). But in “real life” like your console you would call the function like you did.