Search code examples
javascriptfunctiondocumentissue-tracking

COMPLETE - pureJS issue with writing data, any ideas?


I am trying to take the data from "create_name()" and write the outcome to the

<p id="name"> </p> 

updating the variable name to show the new version.

however I am a bit lost in honesty and instead it returns "undefined" as the name.

the code obviously gets to this part and then fails:

Writedata(create_name); // now write our username
menu_function();
}


function Writedata(nameUser){
    document.getElementById("name").innerHTML='hello ' + nameUser + ' below are your options'; // write the username.
}

You can see the code for the entire section I am trying to make work here.

I hope I explained clearly enough.

Any Questions? feel free to ask, all help is appreciated!

init(); // lets start the inital func

function init(){ // Do some asking shit
    var name = prompt('hello, what is your name?'); // ask for the name
    document.write('<p id="name">hello ' + name + ' </p>');
    document.write('<p id="user" >1 - create a username</p>');
    document.write('<p id ="play" >2 - Play Quiz<p>');
    document.write('<p id="reload">3 - quit and reload<p>');
    Writedata(name); // tell our witer to write the name
}

function menu_function() {
    var choice = prompt("hello " + name + " please select an option from the list to your left");

    switch (choice) {
        case "1": opt1_function(); break;
        case "2": opt2_function(); break;
        case "3": reload_method(); break;
        default: menu_function(); break;
    }
}

menu_function();

function opt1_function() {
    alert(name + " you have selected option 1");
    create_name();
}

function create_name() {
    var forename = prompt("what is your forename?");
    var surname = prompt("what is your surname?");
    var username = alert("hello " + forename + " " + surname);
    var string1 = forename.substring(0 , 1);
    var create_name = alert(string1 + surname);
    Writedata(create_name); // now write our username
    menu_function();
}

function Writedata(nameUser){
    document.getElementById("name").innerHTML='hello ' + nameUser + ' below are your options'; // write the username.
}

Solution

  • stop assigning alert() function return values to variables, alert() has no return value look at this alert definition

    change this line

    var create_name = alert(string1 + surname);
    

    to

    alert(string1 + surname);
    var create_name = string1 + surname;