reset password
Author Message
CSr_esco525
Posts: 15
Posted 19:54 Nov 06, 2015 |

Hello, I am in need of assistance for my HTML document. It appears that for Lab 7, my code for asking the first name and last are working just fine, HOWEVER, when I click OK or CANCEL, this happens:

(NOTE: I typed "Nick" for the first name and "Johnson" for the last name.)

 

My intentions was for it to say "Greeting's Nick!", but it misplaces it and puts "true" in the end, which I have no idea where that came from.

Here is the code for this part of the lab:

<script>
            var nameF = prompt("Please enter your fist name");
            var nameL = prompt("Please enter your last name");
            var nameQ = confirm("Would you like to be called by your first name? If so, click OK.");
                if(nameQ){
                    document.write(nameF);
                }
                else{
                    document.write(nameL);
                }
            document.write("Greeting's " + nameQ + "!");

</script>

If somebody could take the time to correct what my mistake is I would appreciate it! Thank you for taking the time to read this and I wish the best of luck to all of you!

4542elgh
Posts: 71
Posted 23:01 Nov 06, 2015 |

simple to fix,

your problem is that you try to check the condition, if you click on OK, then execute document.write(nameF); first!

so you will have your nameF you input display as the FIRST element

then you  call this method  document.write("Greeting's " + nameQ + "!"); which will print AFTER your "nameF" resulting in the problem you have.

the true value is that you have if statement and if you press ok, it is the same as returning the boolean value true. 

the fix will be easy do the following:

 

 var nameF = prompt("Please enter your fist name");
            var nameL = prompt("Please enter your last name");
            var nameQ = confirm("Would you like to be called by your first name? If so, click OK.");
                if(nameQ){
                    document.write("Greeting's " + nameF + "!");
                }
                else{
                    document.write("Greeting's " + nameL + "!");
                }

 

have a nice day

Last edited by 4542elgh at 23:05 Nov 06, 2015.