Help with javascript function

Hi fellow students!
I have got the following function declaration and function calling:

function agreeOrDisagree('str1', 'str2') {
     if (first === second) {
     return You agree!;
  } else { 
    return You disagree!;
   }
}
agreeOrDisagree(si, no);

But still getting “uncaught syntax error”
What could be wrong??

What are these?

parameters…that I pass the value “si” and “no” when calling a function

Parameter are placeholders for the values passed. They are essentially variables. What you have are string literals. What you need are parameters that will be assigned to the values passed to the function. It appears you have confused where quotes belong. Variable names do not belong in quotes, string literal values do. You’ve got it backwards throughout your code.

Also, what are these?

They aren’t defined in your code.

I am a begineer… I tought this was a place to put questions in order to learn… I think its from mistakes that we learn…so I am bit scared now…

No need to be scared. Let’s review assignment of string values to variables:

let myString = "my value";
//  variable   string literal
//now if we print the variable, we will see the value it is assigned to printed to the console
console.log(myString);

Output:

my value

console.log() is a function similar to your function above called agreeOrDisagree(). In your function you are wanting two values supplied as parameters. Since parameters are basically variables they should not be enclosed in quotes. So your function declaration should look similar to:

function myFunction(str1, str2) {
  //function body
}

Then when you call the function, you need to supply the two values as arguments in the function call:

myFunction("si", "no");
//you could also assign variables to the string literals, and use the variables as the arguments:
const myString1 = "si";
const myString2 = "no";
myFunction(myString1, myString2);
//notice where the quotes are used, and where they aren't

Hopefully this helps?

2 Likes

thanks a lot, I will now try to finish the exercise, which is to a fun write a function, agreeOrDisagree() , that takes in two strings, and returns 'You agree!' if the two strings are the same and 'You disagree!' if the two strings are different.

1 Like

Remember to use the parameters in the function rather than undefined variables:

function myFunction(str1, str2) {
  if(str1 === str2) {
    //code to execute if the condition is true
  } else {
    //code to execute if the condition is false
}

Happy coding!

this is my function now working:

function agreeOrDisagree(str1, str2) {
 
  if (str1 === str2) {
    return 'You agree';
  } else {
    return 'You disagree!';
  }
}
console.log(agreeOrDisagree('si', 'si'));

Thank you for your help :smile:

3 Likes