Help with JS code

Hello there,
I was following one of David Green’s lessons and for some weird reason, when he does it it works when I do it, I get an error.

Here is the piece of code

(funtion() {
	
	function greeter(str, arr) {
		var counter;
		var phrase;
		for(counter = 0; counter < arr.length; 
       counter++) {
		phrase = str + " " + arr[counter];
		console.log(phrase);
	}
 }	greeter("Hello", ["Mark", "Jenny"]);
	
}());

I have checked it time and time and time again, I have tested it on many different platforms, it is still coming up with errors.
I use JSbin just like he does, but it simply won’t work for me.
Any help would be much appreciated thanking you in advance.
Regards
Adam

Have you looked really closely at that word?

Also, look again at the very end of your code block. Are you sure that’s how he does it?

2 Likes

Thank you some much, I honestly didn’t see it.
Next time I will check every single character lol
thanks again.

It’s always the smallest things :wink: though the Javascript Console should have caught this for you… should always be your first stop for debugging.

Actually it was pointing at the { and semicolons which were very misleading so, but yes I do agree that the smallest things that are difficult to spot sometimes.

Yup. That’ll be your next lesson. If the thing that it’s pointing at doesnt seem wrong, look at the thing immediately before it.

I didn’t find the error I got in the JS Bin console very helpful

"error"
"write@[native code]
https://static.jsbin.com/js/prod/runner-4.1.7.min.js:1:13929
https://static.jsbin.com/js/prod/runner-4.1.7.min.js:1:10867"

Yeah, but the Javascript Console itself will spit Uncaught SyntaxError: Unexpected token { on the first line. Which would point you to the {, which is correct, so you move back 1 token and you get to funtion() … ah.

1 Like

for a bit more indepthness for the more technical minded, the javascript parser throws this error because funtion() itself is not invalid Javascript - it’s a function call to a named function funtion. the { immediately after it, however, then becomes invalid syntax, as you start to define an Object with no valid expression position for it to be in.

If the rest of the code HAD been valid javascript in-situ, the console would have barked an Undefined function error, citing funtion() as an undefined function.

1 Like

Like you said, it will be my lesson for next time, lol
It should be easier to spot I agree, but I am so used to Java where it really points to the line as well as the error itself, in very few rare occasions where you really need to carefully look at the code to spot the error. lol
I am learning so, this is the best way to learn, and thanks for your help.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.