JavaScript error [solved] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

JavaScript error [solved]

code: https://code.sololearn.com/Wef7Rvg7wRiT/?ref=app problem: My code here is not working. The function validate() thinks that final is undefined, but I declared final before the function is being called. I also get a syntax error in the code. Please help!!!

15th Oct 2020, 6:28 PM
Galaxy-Coding (inactive)
Galaxy-Coding (inactive) - avatar
8 Answers
15th Oct 2020, 8:58 PM
Russ
Russ - avatar
+ 4
Syntax problem: Should use link not script to import w3css <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
15th Oct 2020, 6:57 PM
ODLNT
ODLNT - avatar
+ 4
The link below explains variable scope. In your case, the problem is function scope. https://javascript.info/var#var-variables-can-be-declared-below-their-use Basically when declaring a variable in one function that variable can not be accessed by another function. but if you declare a variable outside of any function that variable becomes a global variable and is accessible by any function within the global scope, hence Russ suggested solution.
15th Oct 2020, 9:08 PM
ODLNT
ODLNT - avatar
+ 3
ODLNT Final starts with: <h3><a target="_blank" href="https://github.com/' + data.login + '">' + data.login + '</a></h3><img class="w3-round" src="' + data.avatar_url + '">' Then I want to add the follwing to the end of the string if it isn’t equal to null or “”: '<p>' + data.bio + '</p>' '<p>Followers: ' + data.followers + '</p>' '<p>Following: ' + data.following + '</p>' '<p>Repositories: ' + data.public_repos + '</p>' '<p>Gists: ' + data.public_gists + '</p>'
15th Oct 2020, 8:17 PM
Galaxy-Coding (inactive)
Galaxy-Coding (inactive) - avatar
+ 3
Russ The problem is that the variable does not exist when the funstion is being called do that doesn’t work.
15th Oct 2020, 8:54 PM
Galaxy-Coding (inactive)
Galaxy-Coding (inactive) - avatar
+ 3
Russ thanks. I don’t understand my mistake but that’s fine since you helped me!
15th Oct 2020, 9:20 PM
Galaxy-Coding (inactive)
Galaxy-Coding (inactive) - avatar
+ 2
What should be assigned to the variable final before it is concatenated with fill?
15th Oct 2020, 7:38 PM
ODLNT
ODLNT - avatar
+ 2
My suggestion is to declare final outside the functions, then just set it to what you want inside them. Example: var final; function validate() { final = ... } Also, on line 2, I think you should use && rather than ||.
15th Oct 2020, 8:48 PM
Russ
Russ - avatar