Kyle M Kyle M - 7 months ago 25
HTML Question

How to display the user's data with calculations

I'm very new to HTML and especially JavaScript and I already have troubles.
How could I display all the data I took from the user taken through promt.
nothing seems to work from what I know.
here is the code.

function myFunction() {
var userinput;

var userName = prompt("Please enter your name to proceed");
if (userName == true)
document.write ("Welcome to Office Cheapo, "+ userName + ". Here is your invoice:");

var notebook = prompt("How many notebooks do you want to buy?" , "1 to 10");
if (notebook == true){
document.write ("Notebooks bought -- "+ notebook);

var pens = prompt("How many pens do you want to buy?", "1 to 10");
if (pens == true){
document.write ("Pens bought -- "+ pens);

var usb = prompt("How many USB Flash Drives do you want to buy?", "1 to 10");
if (usb == true){
var taxusb = (usb*6.75)*0.05;
document.write ("USBs bought -- "+ usb);


<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Cheapo Order</title>


<button onclick="myFunction()">Make an order</button>



Answer Source

userName will either be a string or null (if the user canceled the prompt). So username == true will always be false and your document.write will never be executed.

What you might want is:

if (userName) {

This will execute your block provided that userName is not a falsy value.

The following are all falsy values:

  • null
  • undefined
  • 0
  • ""
  • false
  • NaN

So as long as the user enters something and presses OK in your prompt, the condition will be met and your code within the if branch, executed.