+ 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!!!
8 odpowiedzi
+ 3
Works for me 🤷♂️ https://code.sololearn.com/W6WD4LmHyI8s/?ref=app
+ 4
Syntax problem: Should use link not script to import w3css
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
+ 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.
+ 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>'
+ 3
Russ
The problem is that the variable does not exist when the funstion is being called do that doesn’t work.
+ 3
Russ thanks. I don’t understand my mistake but that’s fine since you helped me!
+ 2
What should be assigned to the variable final before it is concatenated with fill?
+ 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 ||.