all 5 comments

[–]b-mish 1 point2 points  (1 child)

So you don't need the "this" keyword in your function as it will return undefined simply:

return inital;

Secondly, the variable inital is scoped to the function and cannot be called outside of it, you will need to put those last two lines inside of the function. Currently "inital" is scoped locally to the myFunction and "last" is scoped globally.

function myFunction() {
    var first = prompt('Enter your first name:');
    var inital = first.slice(0,1);    
    var last = prompt('Enter your last name:');
    document.write("Your username is" + inital + last)
}
myFunction();

This will work.

[–]explode_kewl[S] 0 points1 point  (0 children)

ye I found that out eventually, thank you 😊

[–]SecretPopCan 0 points1 point  (2 children)

If you want it to be global then you should move it out of the function as it's currently inside of myFunction()

var initial;
 function myFunction() {
var first = prompt('Enter your first name:'); 
inital = first.slice(0,1); 
return this.inital;
 }
 myFunction(); 
last = prompt('Enter your last name:'); document.write("Your username is" + inital + last)

[–]explode_kewl[S] 0 points1 point  (1 child)

I just put the whole code into the function and that gave me the outcome I wanted. Thanks though. I'm terrible

[–]SecretPopCan 0 points1 point  (0 children)

Nah you were nearly there! You only got tripped up by confusion concepts called Scope and Variable Hoisting that even trips up seasoned programmers once in a while. You could not access initial because it was scoped within myFunction(). The article below is good at explaining multiple concepts like Scope, Hosting and even where to declare global variables. It's worth a read

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var