How to use Documet.write to print to browser - javascript

I am beginner at java script. I am trying to write a script that prompts the users to enter 3 numbers and the program count the number of zero, positive and negative integers user inserted. However,When I output to browser, there is not output shown to browser.
here is my Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset= "utf-8">
<title>Counter</title>
<script type="text/javascript">
<!--
var zero =0;
var negative =0;
var positve =0;
var a1 = ["first", "second", "third", "fourth"];
var a2 = [];
var i = 0;
for( x=0; x< a1.length; x++){
a2. push (prompt("Enter " + a1[x]+ " integer\n" + ""));
}
while (i < a2.length)
{
if (a2[i++]== 0)
zero = zero + 1;
else if (a2[i++] < 0)
negative = negative + 1;
else
{
positive = positive + 1;
i ++;
}
}
//-->
</script>
</head>
<body>
<h1> Welcome to Counter System </h1>
<script>
<!--
document.write ("Zeros Numbers entered by the users = " + zero + " <br/>" +
"Positive Numbers entered by the users= " + positive + "<br/> "+
"Negative Numbers entered by the users =" + negative + "<br/>");
//-->
</script>
</body>

I guess the values are not getting printed as they are not present in below script tag as script tags get differentiated on run time. You can do like this:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset= "utf-8">
<title>Counter</title>
<script type="text/javascript">
<!--
var zero =0;
var negative =0;
var positve =0;
var a1 = ["first", "second", "third", "fourth"];
var a2 = [];
var i = 0;
for( x=0; x< a1.length; x++){
a2. push (prompt("Enter " + a1[x]+ " integer\n" + ""));
}
while (i < a2.length)
{
if (a2[i++]== 0)
zero = zero + 1;
else if (a2[i++] < 0)
negative = negative + 1;
else
{
positive = positive + 1;
i ++;
}
}
<!--
document.write ("Zeros Numbers entered by the users = " + zero + " <br/>" +
"Positive Numbers entered by the users= " + positive + "<br/> "+
"Negative Numbers entered by the users =" + negative + "<br/>");
//-->
//-->
</script>
</head>
<body>
<h1> Welcome to Counter System </h1>

Related

How do I fix this code regarding getelementbyid [Javascript]?

So I'm currently taking a coding class and am not very well versed in coding. I'm having trouble with a getelementbyid block which isn't writing my looped array for an assignment. Could anyone help me out?
Here's the code:
<html>
<head>
<title>
Looping Assignment
</title>
</head>
<body>
<h1 align=center>Name and Grades</h1>
<p id="message"> Name </p>
<p id="message2"> Grade </p>
<script>
var input = []
var message = " "
var message2 = " "
var n = 0
var i = 1
var names = n
var grade = i
for (n = 0; n < 1; n++) {
var names = n + 1
input[n] = window.prompt("Enter First Name" + names)
message += "Your name is " + input[n] + "<br>"
}
for (i = 1; i < 5; i++) {
input[i] = window.prompt("Enter Grade (numerical value)" + grade)
message2 += "Grade " + i + " is " + input[i] + "<br>"
}
document.getElementById(message).innerHTML = "your name is " + input[n] + "<br>"
document.getElementById(message2).innerHTML = "Grade" + i + " is " + input[i] + "<br"
</script>
</body>
</html>
Your variables and your id-s have the same names. By doing getElementbyId(message) you are passing the variables' values instead of the fixed id you gave to your elements.
You need to put the id-s in quotes as follows:
document.getElementById("message")
document.getElementById("message2")
Tested at my end and this is working code
<!DOCTYPE html>
<html>
<head>
<title>
Looping Assignment
</title>
</head>
<body>
<h1 align=center>Name and Grades</h1>
<p id="message"> Name </p>
<p id="message2"> Grade </p>
<script>
var input = []
var message = " "
var message2 = " "
var n = 0
var i = 1
var names = n
var grade = i
for (n = 0; n < 1; n++) {
var names = n + 1
input[n] = window.prompt("Enter First Name" + names)
message += "Your name is " + input[n] + "<br>"
}
for (i = 1; i < 5; i++) {
input[i] = window.prompt("Enter Grade (numerical value)" + grade)
message2 += "Grade " + i + " is " + input[i] + "<br>"
}
document.getElementById('message').innerHTML = "your name is " + input[n] + "<br>"
document.getElementById('message2').innerHTML = "Grade" + i + " is " + input[i] + "<br"
</script>
</body>
</html>
error:-
The id of getElementbyId must be in quotes
Make sure your selectors are strings
document.getElementById("message"); not document.getElementById(message);
Make sure you end your lines of code with semi-colons. This is not optional even though the code will attempt to run without them.
Change your output to render your accumulated data - right now you're overwriting your output every time you loop.
Note: I don't know what you're doing with the numerical value inside of the prompt methods (names and grade ) so I didn't touch it.
Example:
<!DOCTYPE html>
<html>
<head>
<title>
Looping Assignment
</title>
</head>
<body>
<h1 align=center>Name and Grades</h1>
<p id="message"> Name </p>
<p id="message2"> Grade </p>
<script>
var input = [];
var message = " ";
var message2 = " ";
var n = 0;
var i = 1;
var names = n;
var grade = i;
for (n = 0; n < 1; n++) {
var names = n + 1
input[n] = window.prompt("Enter First Name " + names)
message += "Your name is " + input[n] + "<br>";
}
for (i = 1; i < 5; i++) {
input[i] = window.prompt("Enter Grade (numerical value) " + grade)
message2 += "Grade " + i + " is " + input[i] + "<br>";
}
document.getElementById("message").innerHTML = message + "<br>";
document.getElementById("message2").innerHTML = message2 + "<br>";
</script>
#SlavicMilk
Below should be sufficient for your assignment:
HTML (index.html):
<h1 style="text-align: center">Name and Grades</h1>
<table>
<thead>
<th>Name</th>
<th>Grade</th>
</thead>
<tbody id="data">
</tbody>
</table>
<script src="script.js"></script>
JS (script.js):
let student = []
for (let i = 0; i < 5; i++) {
student[i] = {
"name": window.prompt(`Enter Name ${i + 1}`),
"grade": window.prompt(`Enter Grade (numerical value) ${i + 1}`)
}
}
document.getElementById('data').innerHTML = student.map(student => {
return `<tr><td>${student.name}</td><td>${student.grade}</td></tr>`
})

Having a problem with my javascript code - computer has to guess the digit value I input

First I created a version of this which worked where I didn't input the value on the webpage:
<p>Enter in how many fingers (between 0 and 5) you are holding up: </p>
<p>
<input id="fingersInput" type="text">
<button id="fingersSubmit">Guess!</button>
</p>
<div id="fingers-game-v2"></div>
<p id="computer-guess-results"></p>
<script type="text/javascript">
var numberOfFingers = document.getElementById("fingersInput").value;
var fingersText = "";
var correctGuesses = 0;
i = 0;
while (i < 5)
{
var computerGuess = Math.floor((Math.random()*5)+1);
fingersText += "<p>" + computerGuess + "</p>";
if (computerGuess == 3)
{
var correctGuesses = correctGuesses + 1;
}
i++;
}
document.getElementById("fingers-game-v2").innerHTML = fingersText;
document.getElementById("computer-guess-results").innerHTML = "<h3>" + "Number of times the computer guessed correctly: " + "</h3>" + correctGuesses;
</script>
Then this is the version I'm trying to create where I enter in the input on the webpage, and then the computer tries to guess it, but it's not executing at all or showing anything when I try to debug in the console.
<p>Enter in how many fingers (between 0 and 5) you are holding up: </p>
<p>
<input id="fingersInput" type="text">
<button id="fingersSubmit">Guess!</button>
</p>
<div id="fingers-game-v2"></div>
<p id="computer-guess-results"></p>
<script type="text/javascript">
var numberOfFingers = document.getElementById("fingersInput").value;
var fingersText = "";
var correctGuesses = 0;
document.getElementById("fingersSubmit").onclick = function ()
{
i = 0;
while (i < 5)
{
var computerGuess = Math.floor((Math.random()*5)+1);
fingersText += "<p>" + computerGuess + "</p>";
if (computerGuess == numberOfFingers)
{
var correctGuesses = correctGuesses + 1;
}
i++;
}
document.getElementById("fingers-game-v2").innerHTML = fingersText;
document.getElementById("computer-guess-results").innerHTML = "<h3>" + "Number of times the computer guessed correctly: " + "</h3>" + correctGuesses;
}
I'm a beginner coder so if there is any useful knowledge pertaining to this in addition to showing me the correct code, then I'd greatly appreciate that!
Thanks!
Here's a working version where I tried to stay as close to your original code as possible:
<html>
<body>
<p>Enter in how many fingers (between 0 and 5) you are holding up:</p>
<p>
<input id="fingersInput" type="text" />
<button id="fingersSubmit">Guess!</button>
</p>
<div id="fingers-game-v2"></div>
<p id="computer-guess-results"></p>
<script type="text/javascript">
document.getElementById('fingersSubmit').onclick = function() {
i = 0;
var fingersText = '';
var correctGuesses = 0;
var numberOfFingers = document.getElementById('fingersInput').value;
while (i < 5) {
var computerGuess = Math.floor(Math.random() * 5 + 1);
fingersText += '<p>' + computerGuess + '</p>';
if (computerGuess == numberOfFingers) {
correctGuesses = correctGuesses + 1;
}
i++;
}
document.getElementById('fingers-game-v2').innerHTML = fingersText;
document.getElementById('computer-guess-results').innerHTML =
'<h3>' +
'Number of times the computer guessed correctly: ' +
'</h3>' +
correctGuesses;
};
</script>
</body>
</html>
The main reason your code wasnt working is because you need to get the input value when the button is clicked (i.e. place it inside the onclick)
The other issue is that you redeclare correctGuesses inside the if block, so you should just remove the var keyword, otherwise you will get NaN
Finally, this isn't strictly a problem with your code, but moving forward you should be aware that numberOfFingers is a string whereas correctGuesses is a number. In your if block, you compare the two values and it works because you used ==, which only checks value. It's typically better practice to use === which is a stricter equality check, comparing both value and type. So moving forward, you might want to make sure to convert numberOfFingers to a number before you do the equality check

Javascript/HTML random character placed at bottom of page [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Random Character example Is a link to a picture of my web page.
I can't find the location where the > symbol at the bottom of the page is generated. I've used cmd f to search the code for a random one and none seem to be misplaced. It's not very intrusive, nonetheless, I like to understand my code.
Also, I do not understand why my for loop is not activating.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>The web site of matt9878</title>
<!-- The style.css file allows you to change the look of your web pages.
If you include the next line in all your web pages, they will all share the same look.
This makes it easier to make new pages for your site. -->
<link href="/style.css" rel="stylesheet" type="text/css" media="all">
</head>
<body>
<div class="outputText">
<!-- Equations assighment -->
<p id="equation"> Equation assighment: <br/></p>
<!-- Counting Coins assighment -->
<p id="combinations"> Coin combo assighment: <br/></p>
<!-- Timely Mesasurements assighment -->
<p id= "circumference"> Timely Mesasurements assighment: <br/></p>
<!-- Blackjack assighment -->
<p id="blackjackTotal"> Blackjack assighment: <br/></p>
<!-- Prime Time Assighment -->
<p id="primeTime"> Prime Time assighment: <br/></p>
</div>
<!-- Greetings assignment -->
<script>
window.alert("Hola, Como esta?");
</script>
<!--Equations assighment-->
<script>
for(i = 0; i < 4; i++)
{
var x = Math.round(Math.random() * 1500);
var y = Math.round(Math.random() * 1500);
document.getElementById("equation").innerHTML += x + " + " + y + " = " + (x + y) + "<br/>";
}
//*seriesOfEquations();
</script>
<!-- Counting Coins assighment -->
<script>
var value = 175;
var penny = 1;
var nickel = 5;
var dime = 10;
var quarter = 25;
document.getElementById("combinations").innerHTML += (value / penny) + "(" + penny + ")" + " = " + value + "<br/>";
document.getElementById("combinations").innerHTML += (value / nickel) + "(" + nickel + ")" + " = " + value + "<br/>";
document.getElementById("combinations").innerHTML += Math.floor(value / dime) + "(" + dime + ")" +
" + " + nickel + " = " + value + "<br/>";
document.getElementById("combinations").innerHTML += (value / quarter) + "(" + quarter + ")" + " = " + value + "<br/>";
</script>
<!-- Timely Mesasurements assighment -->
<script>
var date = new Date();
var rad = date.getHours();
document.getElementById("circumference").innerHTML += (2 * Math.PI * rad).toFixed(3) + "<br/>";
</script>
<!-- Blackjack assighment -->>
<script>
//Card dealing
var cardOne = (Math.ceil(Math.random() * 10 ) + 1);
var cardTwo = (Math.ceil(Math.random() * 10 ) + 1);
var combined = cardOne + cardTwo;
var cardValues = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
//Ace checking
if (cardOne == 11 && combined > 21) {
cardOne = 1;
combined = cardOne + cardTwo;
}
else if (cardTwo == 11 && combined > 21) {
cardTwo = 1;
combined = cardOne + cardTwo;
}
//Dealer hit sequence
if (combined < 17) {
document.getElementById("blackjackTotal").innerHTML += "The dealer is hitting thier current total of " + combined + "<br/>";
cardOne = (Math.ceil(Math.random() * 10 ) + 1);
//Ace checking
if (cardOne == 11 && (combined + cardOne) > 21) {
cardOne = 1;
combined = combined + cardOne;
}
else {
combined = combined + cardOne;
}
}
//Blackjack
if (combined == 21) {
document.getElementById("blackjackTotal").innerHTML += "Blackjack!!!!" + "<br/>";
}
//Dealer bust
else if (combined > 21) {
document.getElementById("blackjackTotal").innerHTML += "The dealer has gone bust with a total of " + combined + "." + "<br/>";
}
//Dealer total ancouncement
else{
document.getElementById("blackjackTotal").innerHTML += "The dealer's total is " + combined + "." + "<br/>";
}
</script>
<!-- Prime time assighment -->
<script>
var numOne = Number(prompt("Please enter the first whole number to begin the range you'd like to explore."));
var numTwo = Number(prompt("Enter the second whole number to conclude the range you'd like to explore. Hint: Second choice > first."));
window.alert(numOne + ", " + numTwo);
if (numOne < numTwo && (numOne.isInteger() && numTwo.isInteger())) {
for(; numOne <= numTwo; numOne++) {
document.getElementById("primeTime").innerHTML += numOne + ", " + numTwo + "<br/>";
}
}
else {
window.alert("Please reload the webpage and try again. Error code: User error.");
}
</script>
</body>
</html>
In line 77 there was an extra > in a comment:
<!-- Blackjack assighment -->>
Which runs as
>
Fixed:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>The web site of matt9878</title>
<!-- The style.css file allows you to change the look of your web pages.
If you include the next line in all your web pages, they will all share the same look.
This makes it easier to make new pages for your site. -->
<link href="/style.css" rel="stylesheet" type="text/css" media="all">
</head>
<body>
<div class="outputText">
<!-- Equations assighment -->
<p id="equation"> Equation assighment: <br/></p>
<!-- Counting Coins assighment -->
<p id="combinations"> Coin combo assighment: <br/></p>
<!-- Timely Mesasurements assighment -->
<p id= "circumference"> Timely Mesasurements assighment: <br/></p>
<!-- Blackjack assighment -->
<p id="blackjackTotal"> Blackjack assighment: <br/></p>
<!-- Prime Time Assighment -->
<p id="primeTime"> Prime Time assighment: <br/></p>
</div>
<!-- Greetings assignment -->
<script>
window.alert("Hola, Como esta?");
</script>
<!--Equations assighment-->
<script>
for(i = 0; i < 4; i++)
{
var x = Math.round(Math.random() * 1500);
var y = Math.round(Math.random() * 1500);
document.getElementById("equation").innerHTML += x + " + " + y + " = " + (x + y) + "<br/>";
}
//*seriesOfEquations();
</script>
<!-- Counting Coins assighment -->
<script>
var value = 175;
var penny = 1;
var nickel = 5;
var dime = 10;
var quarter = 25;
document.getElementById("combinations").innerHTML += (value / penny) + "(" + penny + ")" + " = " + value + "<br/>";
document.getElementById("combinations").innerHTML += (value / nickel) + "(" + nickel + ")" + " = " + value + "<br/>";
document.getElementById("combinations").innerHTML += Math.floor(value / dime) + "(" + dime + ")" +
" + " + nickel + " = " + value + "<br/>";
document.getElementById("combinations").innerHTML += (value / quarter) + "(" + quarter + ")" + " = " + value + "<br/>";
</script>
<!-- Timely Mesasurements assighment -->
<script>
var date = new Date();
var rad = date.getHours();
document.getElementById("circumference").innerHTML += (2 * Math.PI * rad).toFixed(3) + "<br/>";
</script>
<!-- Blackjack assighment -->
<script>
//Card dealing
var cardOne = (Math.ceil(Math.random() * 10 ) + 1);
var cardTwo = (Math.ceil(Math.random() * 10 ) + 1);
var combined = cardOne + cardTwo;
var cardValues = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
//Ace checking
if (cardOne == 11 && combined > 21) {
cardOne = 1;
combined = cardOne + cardTwo;
}
else if (cardTwo == 11 && combined > 21) {
cardTwo = 1;
combined = cardOne + cardTwo;
}
//Dealer hit sequence
if (combined < 17) {
document.getElementById("blackjackTotal").innerHTML += "The dealer is hitting thier current total of " + combined + "<br/>";
cardOne = (Math.ceil(Math.random() * 10 ) + 1);
//Ace checking
if (cardOne == 11 && (combined + cardOne) > 21) {
cardOne = 1;
combined = combined + cardOne;
}
else {
combined = combined + cardOne;
}
}
//Blackjack
if (combined == 21) {
document.getElementById("blackjackTotal").innerHTML += "Blackjack!!!!" + "<br/>";
}
//Dealer bust
else if (combined > 21) {
document.getElementById("blackjackTotal").innerHTML += "The dealer has gone bust with a total of " + combined + "." + "<br/>";
}
//Dealer total ancouncement
else{
document.getElementById("blackjackTotal").innerHTML += "The dealer's total is " + combined + "." + "<br/>";
}
</script>
<!-- Prime time assighment -->
<script>
var numOne = Number(prompt("Please enter the first whole number to begin the range you'd like to explore."));
var numTwo = Number(prompt("Enter the second whole number to conclude the range you'd like to explore. Hint: Second choice > first."));
window.alert(numOne + ", " + numTwo);
if (numOne < numTwo && (numOne.isInteger() && numTwo.isInteger())) {
for(; numOne <= numTwo; numOne++) {
document.getElementById("primeTime").innerHTML += numOne + ", " + numTwo + "<br/>";
}
}
else {
window.alert("Please reload the webpage and try again. Error code: User error.");
}
</script>
</body>
</html>
just above card dealing code, there is extra '>' in commented blackjack assignment. Hope it will help

Random Number Generator Javascript - Ranges

Looking for some guidance from those more knowledgeable than myself.
I'm writing a program that will prompt the user to click a button, which will then generate a random integer from 1 to 100.
I can manage that part fine. What I am struggling with is making the program print what range this random integer is in i.e. i<=33.
For example, when I click the button I want to see:
"10 is a number less than 33"
or
"55 is a number between 33 and 66"
Here is my code so far, I have put the code I am having problems with in comments.
<!DOCTYPE HTML5>
<html>
<head>
<meta charset="UTF-8">
<title>question2</title>
</head>
<body>
<p>Click the button to display a random number from 1 to 100.</p>
<button onclick="myFunction()">Click here</button>
<p id="demo"></p>
<script>
function myFunction() {
var text = "";
var i = Math.floor((Math.random() * 100) + 1);
document.getElementById("demo").innerHTML = i;
}
// if (myFunction <= 33) {
// text += ("<br>" + i + " is a number less than or equal to 33");
// }
// if (myFunction > 34 and i < 65) {
// text += ("<br>" + i + " is a number between 33 and 66 (exclusive)");
// }
// if (my Function >= 66) {
// text += ("<br>" + i + " is a number greater than or equal to 66");
// }
</script>
</body>
</html>
Thanks guys.
You could test and if true display the result and exit.
function getRandom() {
var i = Math.floor((Math.random() * 100) + 1);
if (i <= 33) {
document.getElementById("demo").innerHTML += i + " is a number less than or equal to 33<br>";
return;
}
if (i < 66) {
document.getElementById("demo").innerHTML += i + " is a number between 33 and 66 (exclusive)<br>";
return;
}
document.getElementById("demo").innerHTML += i + " is a number greater than or equal to 66<br>";
}
<p>Click the button to display a random number from 1 to 100.</p>
<button onclick="getRandom()">Click here</button>
<p id="demo"></p>
Did you mean something like this:
function myFunction() {
var text = "";
var i = Math.floor((Math.random() * 100) + 1);
if (i <= 33) {
text += ("<br>" + i + " is a number less than or equal to 33");
}
if (i > 34 && i < 65) {
text += ("<br>" + i + " is a number between 33 and 66 (exclusive)");
}
if (i >= 66) {
text += ("<br>" + i + " is a number greater than or equal to 66");
}
document.getElementById("demo").innerHTML = text;
}
The problem is you are calling variables from inside myFunction() outside of that function. Examples include i and myFunction(). It would be better to put the if else statements inside the main function.
function myFunction() {
var demo = document.getElementById("demo");
var i = Math.floor((Math.random() * 100) + 1);
if (i <= 33) {
demo.innerHTML = i + " is a number less than 33";
}
else if (i < 65) {
demo.innerHTML = i + "is a number between 33 and 65";
}
else {
demo.innerHTML = i + " is a number greater than or equal to 66";
}
}
<p>Click the button to display a random number from 1 to 100.</p>
<button onclick="myFunction()">Click here</button>
<p id="demo"></p>
Your function isn't what you should be comparing against in your if statements.
But, I did throw something together let me know if this is what you were looking for.
You were on the right track. Also on the middle comparison I changed you "and" to a shortcircuit "&&".
JSfiddle: https://jsfiddle.net/xew8dvbd/
$(document).ready(function() {
function myFunction() {
var i = Math.floor((Math.random() * 100) + 1);
var text = "";
if (i <= 33) {
text += ("<br>" + i + " is a number less than or equal to 33");
}
if (i > 34 && i < 65) {
text += ("<br>" + i + " is a number between 33 and 66 (exclusive)");
}
if (i >= 66) {
text += ("<br>" + i + " is a number greater than or equal to 66");
}
document.getElementById("demo").innerHTML = text;
};
$("#submit").click(myFunction);
});
Not sure why, but I had to do this as a function expression rather than a function definition to make it work. Oddly, that's the second time this one worked as a gotcha in the last hour.
myFunction = function() {
var text = "";
var i = Math.floor((Math.random() * 100) + 1);
document.getElementById("demo").innerHTML = i;
switch (true) {
case (i < 20):
console.log(i + " is less than 20.");
break;
case (i < 40):
console.log(i + " is between 21 and 40.");
break;
case (i < 60):
console.log(i+ " is between 21 and 60.");
break;
default:
console.log(i+ " is over 60.");
}
};
<p>Click the button to display a random number from 1 to 100.</p>
<button onclick="myFunction()">Click here</button>
<p id="demo"></p>

Basic Javascript Program, help need

My program needs to consist of a for loop, use both input functions already created ( name and number) and totals need to be acquired. I also need to be able to CANCEL and go to my doc.write where i would input name and number. I also need to give the user another chance to type in their name or number if they accidentally typed in number where letters should be and vise-versa. I think i have majority of the structure right, any help would be greatly appreciated!
function inputName() {
var nameIn = prompt("Enter your Name");
while(!isNaN(nameIn)) {
nameIn = prompt("Must contain only letters");
}
return nameIn;
}
/* INPUT NUMBER */
function inputNum(){
var numIn=parseFloat(prompt("Enter the number of hours worked \n 0-60 hours"));
var min=0;
var max=60;
while(numIn>min && numIn>max ){
numIn=prompt("Enter a valid number between 0-60");
return numIn;
}
</script>
<body>
<script type="text/javascript">
//DECLARATIONS
var wage=10.00;
var earned; // hrsWrked*wage
var totHrsWrked=0;
var totEarning=0;
var BR= "
";
var howMany;
var loopControl;
//INPUT & PROCESSING
howMany=parseFloat(prompt("How many employees are you inputing?"));
for(loopControl=1 ; loopControl <= howMany; ++loopControl){
var inpNam=inputName();
var inpNumber=inputNum();
earned= inpNumber*wage;
totEarning+=earned;
totHrsWrked+=inpNumber;
//OUTPUT
document.write("Name: "+ inpNam+ BR);
document.write("Hours Worked: " + inpNumber + BR);
document.write("Money Earned: $ " + earned + BR +BR);
}
document.write("Total Hours Worked: " + totHrsWrked.toFixed(2) + BR);
document.write("Total Earnings: " + "$"+totEarning.toFixed(2)+ BR+BR);
</script>
</body>
</html>
Here's the edited version of your code. :)
<! doctype html>
<html>
<body>
<script>
//DECLARATIONS
var wage=10.00, earned, totHrsWrked=0, totEarning=0, howMany;
//INPUT & PROCESSING
howMany=parseFloat(prompt("No of employees"));
for( var loopControl=1 ; loopControl <= howMany; ++loopControl)
{
var inpNam=inputName();
var inpNumber=inputNum();
earned= inpNumber*wage;
totEarning+= (+earned);
totHrsWrked+= (+inpNumber);
//OUTPUT
document.write("Name: "+ inpNam+ "<br>");
document.write("\n Hours Worked: " + inpNumber + "<br>");
document.write("Money Earned: $ " + earned + "<br><br>");
}
document.write("Total Hours Worked: " + totHrsWrked.toFixed(2)+ "<br>");
document.write("Total Earnings: " + "$"+totEarning.toFixed(2)+ "<br>");
//INPUT NAME
function inputName() {
var nameIn = prompt("Enter your Name");
while(!isNaN(nameIn)) {
nameIn = prompt("Must contain only letters");
}
return nameIn;
}
//INPUT NUMBER
function inputNum(){
var numIn=parseFloat(prompt("Enter the number of hours worked \n 0-60 hours"));
var min=0;
var max=60;
while(numIn<=min || numIn>=max ){
numIn=prompt("Enter a valid number between 0-60");
}
return numIn;
}
</script>
</body>
</html>

Categories

Resources