What is the diffrence?

hi guys
here is my code =

const icanhaz = (argument) => {
if (argument) {
  argument =
  console.log(`i can haz ${argument}`)
} else {
  console.log('i can haz cheezburger?') 
 
}

};
icanhaz(); //wright something here or it will return the meme


it suppose to write = i can haz cheezburger? , when there is no argument and when there is it just replace the cheezburger? at the end with the given argument
and here is the code but it is the solution i get from the (function module practise quiz) because it didnt accept my code =

const icanhaz = (arg='cheezburger') => {
  return `i can haz ${arg}?`
}

// Returns: i can haz presentz?
console.log(icanhaz('presentz'));
 
// Returns: i can haz cheezburger?
console.log(icanhaz());

what is the diffrence they do exactly the same but why my code didnt get accepted ?

1 Like

I see a few possibilities for why your code failed.

  • It is possible that the requirement was for the function to return the string rather than log it to the console, and implicitly return undefined.

  • It is also possible that you were supposed to use a parameter with a default argument assigned like the solution does: (arg='cheezburger')

  • Your code doesn’t print the ‘?’ at the end when an argument is passed to the function.

  • You also have an assignment to your function parameter for no apparent reason.

You can add another console.log to see for yourself:

const icanhaz = (argument) => { if (argument) { argument = console.log(`i can haz ${argument}`) //argument is now assigned to undefined console.log(`argument is now assigned to ${argument}`); //see what argument is now assigned to } else { console.log('i can haz cheezburger?') } } //add a function call with an argument icanhaz('potato'); icanhaz(); //wright something here or it will return the meme
4 Likes

ok
but now the output is like this=

i can haz potato
argument is now assigned to undefined
i can haz potato
argument is now assigned to undefined
i can haz potato
argument is now assigned to undefined
undefined

1 Like

That code is not an example of how to pass the exercise. It’s to show you what is happening with the code you wrote.

1 Like

ok ok , i actually cant do the exam anymore (becuase after 3 fails it will give you the answer and you cannot re-take the exam afterwards)

1 Like

hi agian , look for example
when i add something in the line 15 the output will chage for example when i add an a
the output is i can haz a but agian i get the argument is now assigned to undefined so , when the output is change based on what i wrote on line 15 how can be undefined ? if it is undefined why the output change from the default i can haz cheezburger to i can haz a .

1 Like

Above is your original code. The following line doesn’t belong:

By placing it on the line above the following line

console.log(`i can haz ${argument}`)

you are assigning argument to the value returned by console.log(). That value is undefined. If you are trying to assign a default value to argument, you need to do it like the solution code shows:

const icanhaz = (argument = 'cheezburger') => {
//               ^^^^^^^^^^^^^^^^^^^^^^^^
//    assignment of default value for function parameter

The assignment happens after the expression is evaluated.

// putting it all on one line for clarity
// this is executed the same as how you wrote it on two lines
  argument = console.log(`i can haz ${argument}`)
//           ^^^^^^this is evaluated first^^^^^^^
// so the string is printed to the console.
// after printing `i can haz a` console.log() returns undefined
// then argument gets assigned to undefined

1 Like

now i got it all , i wish that my teachers could explain to me like you did , thanks mate

2 Likes