if else statement..calculation for the activity level - javascript

var bmr=100;
var sum=50;
var caloriesneeded = 0;
var activitylevel = prompt("Enter your activity level ");
document.write( "<p>You need to consume ");
if ( activitylevel == "0"){
document.write (caloriesneeded = bmr * 1.2 );
}
else if (activitylevel == "1-3"){
document.write(caloriesneeded = bmr * 1.375 );
}
else if (activitylevel == "3-5"){
document.write(caloriesneeded = bmr * 1.55 );
}
else if (activitylevel == "6-7"){
document.write (caloriesneeded = bmr * 1.725 );
}
else if (activitylevel == "2x"){
document.write(caloriesneeded = bmr * 1.9 );
}
else {
document.write("invalid");
}
if (caloriesneeded > sum){
document.write ("<p>You still can consume " + (caloriesneeded - sum) +
"kcal of food</p>");
}
else if (caloriesneeded == sum){
document.write("<p>You have consumed just enough " + (caloriesneeded -
sum) + "kcal of food</p>");
}
else if (caloriesneeded < sum){
document.write ("<p>You have over consumed " + (sum - caloriesneeded )
+ "kcal of food</p>");
}
Only the first half of the code is working and the calculation for calories needed > sum, calories needed == sum and calories needed < sum does not show any result. Only when the activity level is 'Invalid' then the second part of the code will be working. Anyone spot any errors?

var bmr=100;
var sum=50;
var caloriesneeded = 0;
var activityLevels = {
"0": 1.2,
"1-3": 1.375,
"3-5": 1.55,
"6-7": 1.725,
"2x": 1.9
};
var output = "";
function calculateCaloriesNeeded(activityLevel) {
if ( typeof activityLevels[activityLevel] !== "undefined") {
caloriesneeded = bmr * activityLevels[activityLevel];
return true;
}
return false;
}
var currentActivityLevel = prompt("Enter your activity level ");
if (calculateCaloriesNeeded( currentActivityLevel )) {
// input has been valid
output += "<p>You need to consume " + caloriesneeded + "</p>";
if (caloriesneeded > sum){
output += "<p>You still can consume " + (caloriesneeded - sum) + "kcal of food</p>";
} else if (caloriesneeded == sum){
output += "<p>You have consumed just enough " + (caloriesneeded - sum) + "kcal of food</p>";
} else if (caloriesneeded < sum){
output += "<p>You have over consumed " + (sum - caloriesneeded ) + "kcal of food</p>";
}
} else {
output += "<p>Your input is not valid. Please choose one of “0”, “1-3”, “3-5”, “6-7” or “2x”.</p>";
}
document.write( output );
A little bit cleaner approach.

I am sorry to say but your code is full of bugs.
document.write( "<p>You need to consume "); // missing closing tag </p>
document.write (caloriesneeded = bmr * 1.725 ); // assign and passing a parameter at the same time?
Calculate the value and then pass it to the document eg:
caloriesneeded = bmr * 1.2;
document.write ("<p>"+caloriesneeded+"</p>");
The value passed to the prompt windows are number no need to put them into quotes.
if ( activitylevel == 0){
This is not a range check:
if (activitylevel == "1-3"){
You should do the following:
if (activitylevel >= 1 && activitylevel < 3){
As expressed above the user have no idea what to enter in the prompt window. It would be better to have a list from which the user could choose from.
Here I have made a simple example in how you could potentially make it a better solution.
$("#activeSelection").change(function() {
var bmr = 100;
var sum = 50;
var caloriesneeded = 0;
var $selection = $("#activeSelection").val();
if ($selection == 0) {
caloriesneeded = bmr * 1.2;
$('#calorieResponse').text(caloriesneeded);
} else if ($selection >= 1 && $selection < 3) {
caloriesneeded = bmr * 1.375
$('#calorieResponse').text(caloriesneeded);
} else if ($selection >= 3 && $selection <= 5) {
caloriesneeded = bmr * 1.55
$('#calorieResponse').text(caloriesneeded);
} else if ($selection >= 6 && $selection < 7) {
caloriesneeded = bmr * 1.725;
$('#calorieResponse').text(caloriesneeded);
} else if ($selection >=8 && $selection <=10) {
caloriesneeded = bmr * 1.9
$('#calorieResponse').text(caloriesneeded);
}
if (caloriesneeded > sum){
$('#consume').text("You still can consume " + (caloriesneeded - sum) + "kcal of food");
}
else if (caloriesneeded == sum){
$('#consume').text("You have consumed just enough " + (caloriesneeded -
sum) + "kcal of food");
}
else if (caloriesneeded < sum){
$('#consume').text("You have over consumed " + (sum - caloriesneeded )+ "kcal of food");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span>Hei, what is your activity level? </span>
<select id="activeSelection">
<option>Please select</option>
<option value="0">Couch potato</option>
<option value="1">Not doing much</option>
<option value="2">Lazy</option>
<option value="3">Little active</option>
<option value="4">Somehow active</option>
<option value="5">Active</option>
<option value="6">Active 2 times a week</option>
<option value="7">Active 3 times a week</option>
<option value="8">Active 4 times a week</option>
<option value="9">Active 5 times a week</option>
<option value="10">Always on the go</option>
</select>
<div>You need to consume: </div><span id="calorieResponse"></span>
<div id="consume"></div>

Related

If statement with || is not doing anything

some context,
I'm making a game wherein you have to guess a randomly generated number between 1 and 1 million. I'm trying to make it so that if the user inputs something that is not a number, above 1 million, or below zero, it will display an error warning. The last else if statement is supposed to do this, but is not doing so. I've tried several revisions, including individual else if statements for each forbidden case, but they aren't working.
<script type = "text/javascript">
var randomNumber = Math.floor(Math.random() * 1000000 + 1);
var count = 0;
var x = 1000000
console.log(randomNumber)
document.getElementById("submitguess").onclick = function(){
var userGuess = document.getElementById("guessField").value;
console.log(userGuess)
if(userGuess == randomNumber)
{
if (count < 10) {
document.getElementById('higherOrLower').innerHTML = '<div class="powerupstyling"><a class="facethebossstyling">YOU BEAT THE GAME. CONGRATS !</a></div>'
}
else if (count > 10) {
document.getElementById('higherOrLower').innerHTML = "<center>grats you got it in " + count + " tries. Unfortunately, you didn't quite beat the game. Aim for 10 tries or lower.</center>"
}
}
else if (userGuess > randomNumber) {
count++;
document.getElementById('higherOrLower').innerHTML = '<br><div class="powerupstyling"><a class="gostyling">go lower</a></div>'
document.getElementById('countCounter').innerHTML = "<center>tries: " + count + "</center>";
}
else if (userGuess < randomNumber) {
count++;
document.getElementById('higherOrLower').innerHTML = '<br><div class="powerupstyling"><a class="gostyling">go higher</a></div>'
document.getElementById('countCounter').innerHTML = "<center>tries: " + count + "</center>";
}
else if (userGuess >= 1000000 || userGuess =< 0 || userGuess == "") {
alert('input a valid number !')
}
}
</script
You have to write the last else if before you check if the userGuess is higher or lower than the randomNumber.
That's because when userGuess is below 0 or over 1.000.000 it will always be catched by the else ifs that check if the userGuess is lower or higher than randomNumber. Therefore the last else if will never be reached.
The correct order would be:
Check if the user guessed right.
Check if the input is wrong.
Check if the user guessed too high.
Check if the user guessed too low.
But you did:
Check if the user guessed right.
Check if the user guessed too high.
Check if the user guessed too low.
Check if the input is wrong.
Do the following:
<script type = "text/javascript">
var randomNumber = Math.floor(Math.random() * 1000000 + 1);
var count = 0;
var x = 1000000
console.log(randomNumber)
document.getElementById("submitguess").onclick = function(){
var userGuess = document.getElementById("guessField").value;
console.log(userGuess)
if(userGuess == randomNumber)
{
if (count < 10) {
document.getElementById('higherOrLower').innerHTML = '<div class="powerupstyling"><a class="facethebossstyling">YOU BEAT THE GAME. CONGRATS !</a></div>'
}
else if (count > 10) {
document.getElementById('higherOrLower').innerHTML = "<center>grats you got it in " + count + " tries. Unfortunately, you didn't quite beat the game. Aim for 10 tries or lower.</center>"
}
}
else if (userGuess >= 1000000 || userGuess <= 0 || userGuess == "") {
alert('input a valid number !')
}
else if (userGuess > randomNumber) {
count++;
document.getElementById('higherOrLower').innerHTML = '<br><div class="powerupstyling"><a class="gostyling">go lower</a></div>'
document.getElementById('countCounter').innerHTML = "<center>tries: " + count + "</center>";
}
else if (userGuess < randomNumber) {
count++;
document.getElementById('higherOrLower').innerHTML = '<br><div class="powerupstyling"><a class="gostyling">go higher</a></div>'
document.getElementById('countCounter').innerHTML = "<center>tries: " + count + "</center>";
}
}
</script
There are a couple problems with your conditionals.
else if (userGuess > randomNumber)
{ ... }
This will handle all numbers, even those greater than 1,000,000.
else if (userGuess < randomNumber)
{ ... }
This will handle all numbers, even those less than 0.
Therefore, your last conditional will only ever catch a blank input (which would probably be better expressed as a cast to string or bool).
Also, as pointed out in the comments to your question, the correct less than or equal syntax is <=.
You do:
else if (userGuess >= 1000000 || userGuess =< 0 || userGuess == "") {
But it's like this:
else if (userGuess >= 1000000 || userGuess <= 0 || userGuess == "") {
<script type = "text/javascript">
var randomNumber = Math.floor(Math.random() * 1000000 + 1);
var count = 0;
var x = 1000000
console.log(randomNumber)
document.getElementById("submitguess").onclick = function(){
var userGuess = document.getElementById("guessField").value;
console.log(userGuess)
if(userGuess == randomNumber)
{
if (count < 10) {
document.getElementById('higherOrLower').innerHTML = '<div class="powerupstyling"><a class="facethebossstyling">YOU BEAT THE GAME. CONGRATS !</a></div>'
}
else if (count > 10) {
document.getElementById('higherOrLower').innerHTML = "<center>grats you got it in " + count + " tries. Unfortunately, you didn't quite beat the game. Aim for 10 tries or lower.</center>"
}
}
else if (userGuess > randomNumber) {
count++;
document.getElementById('higherOrLower').innerHTML = '<br><div class="powerupstyling"><a class="gostyling">go lower</a></div>'
document.getElementById('countCounter').innerHTML = "<center>tries: " + count + "</center>";
}
else if (userGuess < randomNumber) {
count++;
document.getElementById('higherOrLower').innerHTML = '<br><div class="powerupstyling"><a class="gostyling">go higher</a></div>'
document.getElementById('countCounter').innerHTML = "<center>tries: " + count + "</center>";
}
else if (userGuess >= 1000000 || userGuess <= 0 || userGuess == "") {
alert('input a valid number !')
}
}
</script

Prompt window won't open when clicking on button

I've got an assignment where I have to see if the entered number is an Armstrong number, the problem is when I click on the button the prompt window doesn't show up, and it only occurs when I have the rest of the function written out. When it is by itself the prompt window shows.
function armNum(){
var num = prompt("Enter a number between 0 and 999!: ");
var firstNum;
var secondNum;
var thirdNum;
if(num < 100 && num > 0)
{
firstNum = num/10;
secondNum = num%10;
var StrongNum = (firstNum**3) + (secondNum**3);
if( num == StrongNum)
{
document.getElementById("ispis").innerHTML = "nummber " + num + " is an armstrong number!"
}
else
{
document.getElementById("ispis").innerHTML = "number" + num + "is not an armstrong number!"
}
}
if(num > 99 && num < 1000)
{
firstNum = num/100;
secondNum = (num/10)%10;
thirdNum = num % 10;
var StrongNum = (firstNum**3) + (secondNum**3) = (thirdNum**3);
if( num == StrongNum)
{
document.getElementById("ispis").innerHTML = "nummber " + num + " is an armstrong number!"
}
else
{
document.getElementById("ispis").innerHTML = "number" + num + "is not an armstrong number!"
}
}
}
You have a mistake at this string (change "=" to "+"):
var StrongNum = (firstNum3) + (secondNum3) = (thirdNum**3);
You don`t call function
try to add after your function
armNum();
there is a error in tnis line = (firstNum**3) + (secondNum**3) = (thirdNum**3); you have used '=' instead off '+' & you
function armNum(){
var num = prompt("Enter a number between 0 and 999!: ");
var firstNum;
var secondNum;
var thirdNum;
if(num < 100 && num > 0)
{
firstNum = num/10;
secondNum = num%10;
var StrongNum = (firstNum**3) + (secondNum**3);
if( num == StrongNum)
{
alert( "nummber " + num + " is an armstrong number!")
}
else
{
alert( "number" + num + "is not an armstrong number!"); }
}
if(num > 99 && num < 1000)
{
firstNum = num/100;
secondNum = (num/10)%10;
thirdNum = num % 10;
var StrongNum = (firstNum**3) + (secondNum**3)+(thirdNum**3);
if( num == StrongNum)
{
alert( "nummber " + num + " is an armstrong number!");
}
else
{
alert("number" + num + "is not an armstrong number!");
}
}
}
<body onload=armNum()>
</body>

Inequalities not displaying right output, JS

I am trying to make a simple final grade calculator and am having some issues with the output of the inequalities. It consists of 4 grades each given a double value. They are then multiplied by their respected weight and put into a series of inequalities to see which grade the participant would receive. Unfortunately, I am getting the incorrect letter value even though the double value is correct. I assume it's my inequalities, but I can't figure out whats wrong with them. Any help is greatly appreciated!
I didn't want to include all of the code because it's a lot, but they all correspond to their individual drop-down menus like this one.
<form name="GPA">
<select name="demoSelect" onchange="showData2()">
<option value="zilch">Select Quarter 1:</option>
<option id="4.0" >A</option>
<option id="3.5" >B+</option>
<option id="3.0" >B</option>
<option id="2.5" >C+</option>
<option id="2.0" >C</option>
<option id="1.5" >D</option>
<option id="0.0" >F</option>
</select>
</form>
End of Form
function showData() {
var theSelect = GPA.demoSelect;
var firstGrade = document.getElementById('firstGrade');
firstGrade.innerHTML = (theSelect[theSelect.selectedIndex].id);
var grade = theSelect[theSelect.selectedIndex].id;
var finalGrade = parseFloat(grade);
var theSelect = GPA2.demoSelect2;
var secondGrade = document.getElementById('secondGrade');
secondGrade.innerHTML = (theSelect[theSelect.selectedIndex].id);
var grade2 = theSelect[theSelect.selectedIndex].id;
var finalGrade2 = parseFloat(grade2);
var theSelect = GPA3.demoSelect3;
var secondGrade = document.getElementById('thirdGrade');
thirdGrade.innerHTML = (theSelect[theSelect.selectedIndex].id);
var grade3 = theSelect[theSelect.selectedIndex].id;
var finalGrade3 = parseFloat(grade3);
var theSelect = GPA4.demoSelect4;
var forthGrade = document.getElementById('forthGrade');
forthGrade.innerHTML = (theSelect[theSelect.selectedIndex].id);
var grade4 = theSelect[theSelect.selectedIndex].id;
var finalGrade4 = parseFloat(grade4);
var FinalCalc = ((finalGrade * 0.2) + (finalGrade2 * 0.3) + (finalGrade3 * 0.2) + (finalGrade4 * 0.3));
if (FinalCalc >= 3.75) {
document.getElementById("output2").innerHTML = "A";
} else if (FinalCalc >= 3.25 && FinalCalc < 3.75) {
document.getElementById("output2").innerHTML = "B+";
} else if (FinalCalc >= 2.75 && FinalCalc < 3.25) {
document.getElementById("output2").innerHTML = "B";
} else if (FinalCalc >= 2.25 && FinalCalc < 2.75) {
document.getElementById("output2").innerHTML = "C+";
} else if (FinalCalc >= 1.75 && FinalCalc < 2.25) {
document.getElementById("output2").innerHTML = "C";
} else if (FinalCalc >= 1.25 && FinalCalc < 1.75) {
document.getElementById("output2").innerHTML = "D";
} else if (FinalCalc < 1.25) {
document.getElementById("output2").innerHTML = "F";
}
document.getElementById("output").innerHTML = FinalCalc;
}
The partial you have provided works fine:
function showData() {
var theSelect = GPA.demoSelect;
var firstGrade = document.getElementById('firstGrade');
firstGrade.innerHTML = (theSelect[theSelect.selectedIndex].id);
var grade = theSelect[theSelect.selectedIndex].id;
var finalGrade = parseFloat(grade);
var FinalCalc = finalGrade * 0.2
//var FinalCalc = ((finalGrade * 0.2) + (finalGrade2 * 0.3) + (finalGrade3 * 0.2) + (finalGrade4 * 0.3));
if (FinalCalc >= 3.75) {
document.getElementById("output2").innerHTML = "A";
} else if (FinalCalc >= 3.25 && FinalCalc < 3.75) {
document.getElementById("output2").innerHTML = "B+";
} else if (FinalCalc >= 2.75 && FinalCalc < 3.25) {
document.getElementById("output2").innerHTML = "B";
} else if (FinalCalc >= 2.25 && FinalCalc < 2.75) {
document.getElementById("output2").innerHTML = "C+";
} else if (FinalCalc >= 1.75 && FinalCalc < 2.25) {
document.getElementById("output2").innerHTML = "C";
} else if (FinalCalc >= 1.25 && FinalCalc < 1.75) {
document.getElementById("output2").innerHTML = "D";
} else if (FinalCalc < 1.25) {
document.getElementById("output2").innerHTML = "F";
}
document.getElementById("output").innerHTML = FinalCalc;
}
Check it out here: http://jsfiddle.net/wjvkg8q9/15/
Note that your function is called showData(), and the onchange event calls showData2()
Is it possible that you are editing wrong duplicated function in the rest of your code?
P.S: I have commented the other grade variables in the fiddle, but as you've said they correspond.

Difficulty assigning a Discount

As the title says, I'm having trouble assigning discounts to my products. When I load the page it says nan and undefined. Any help is appreciated. Thankyou!
This is my JavaScript code:
var ProductType;
var ProductQty;
var ProductPrice;
var DiscountPercent;
var TotalAmount;
function calculate() {
ProductType = prompt("Please enter the type of product you require!").toUpperCase();
document.write("<br>");
ProductQty = prompt("Please enter the number of products you require!");
elsestatement();
ProductQty = parseInt(ProductQty);
document.write("Type of Products:" + ProductType);
document.write("<br>");
document.write("Number of Products:" + ProductQty);
document.write("<br>");
var GrossAmount =(ProductPrice) * (ProductQty);
document.write("Gross Amount is:" + GrossAmount);
GrossAmount = parseInt(GrossAmount);
discountAmt();
var DiscountAmount = (GrossAmount) - (GrossAmount) * (DiscountPercent)
var TotalAmount = (GrossAmount) * (DiscountPercent)
document.write("<br>");
document.write("Discount Amount:" + DiscountAmount)
document.write("<br>");
document.write("Discount Percent:" + DiscountPercent)
document.write("<br>");
document.write("Total Amount:" + TotalAmount)
}
function elsestatement(){
if (ProductType == 'A') {
ProductPrice = 100;
} else if (ProductType == 'B') {
ProductPrice = 75;
} else if (ProductType == 'C'){
ProductPrice = 50;
}
else {
document.write("<br>");
document.write("Invalid Product Type");
document.write("<br>");
}
if (ProductQty <1|| ProductQty >100) {
document.write("Invalid Quantity")
}
}
function discountAmt() {
if (GrossAmount <200) {
DiscountPercent = '0';
} else if (GrossAmount >= 200 && GrossAmount<=399.99) {
DiscountPercent = '.05';
} else if (GrossAmount>=400 && GrossAmount<=599.99 ) {
DiscountPercent = '.075';
} else if (GrossAmount >=600)
DiscountPercent = '.1';
}
This is my HTML Code:
<!DOCTYPE html>
<html>
<title>Product</title>
<body>
<h1>Product Calc</h1>
<script src="Product.js"> </script>
<script>calculate()</script>
<script>elsestatement()</script>
<script>discountAmt()</script>
</body>
Sorry for the confusion. I was mistaken about the unclosed function. Instead, the problem is that GrossAmount was defined in the calculate function instead of in the outer scope. Therefor, it was not reachable in the discountAmt function.
Here is your fixed code, except with the document.writes removed so that it can run in the sandbox:
var ProductType;
var ProductQty;
var ProductPrice;
var DiscountPercent;
var TotalAmount;
var GrossAmount;
function calculate() {
ProductType = prompt("Please enter the type of product you require!").toUpperCase();
ProductQty = prompt("Please enter the number of products you require!");
elsestatement();
ProductQty = parseInt(ProductQty);
GrossAmount = ProductPrice * ProductQty;
GrossAmount = parseInt(GrossAmount);
discountAmt();
var DiscountAmount = GrossAmount - GrossAmount * DiscountPercent;
var TotalAmount = GrossAmount * DiscountPercent;
}
function elsestatement(){
if (ProductType == 'A') {
ProductPrice = 100;
} else if (ProductType == 'B') {
ProductPrice = 75;
} else if (ProductType == 'C'){
ProductPrice = 50;
} else {}
if (ProductQty < 1|| ProductQty > 100) {}
console.log('ProductPrice: ', ProductPrice);
}
function discountAmt() {
if (GrossAmount < 200) {
DiscountPercent = '0';
} else if (GrossAmount >= 200 && GrossAmount <= 399.99) {
DiscountPercent = '.05';
} else if (GrossAmount >= 400 && GrossAmount <= 599.99) {
DiscountPercent = '.075';
} else if (GrossAmount >= 600) {
DiscountPercent = '.1';
}
console.log('DiscountPercent: ', DiscountPercent);
}
calculate();
Obviously you are not closing the elsestatement function ie } is missing. and manage the code like this
change
<!DOCTYPE html>
<html>
<title>Product</title>
<body>
<h1>Product Calc</h1>
<script src="Product.js"> </script>
<script>calculate()</script>
<script>elsestatement()</script>
<script>discountAmt()</script>
</body>
to
<!DOCTYPE html>
<html>
<title>Product</title>
<script src="Product.js"> </script>
<body>
<h1>Product Calc</h1>
</body>
<script>
$(function(){
calculate();
});
</script>

Input not being assigned in a switch situation

So I know ive cluttered this to High heaven and back but it should work. My only problem is that im not getting a returned value for my input of homeValue. Therefore none of my taxes can be applied. Here is the original problem statement:
Ps. Sorry for just straight asking this the other day, I had left my Flashdrive at home and have this due later this week so I wanted to get it posted today.
You have been asked to write a property tax program for the tri-county area. If you live in Charleston county then you owe 1 percent of your home's value if it's less than or equal to $50,000. You owe 1.50 percent if it's greater than $50,000 but not greater than $150,000 and 2 percent if it's greater than $150,00. If you live in Dorchester county then you owe 1.25 percent of your home's value if it's less than or equal to $50,000. You owe 1.50 percent if it's greater than $50,000 but not greater than $150,000 and 1.75 percent if it's greater than $150,000. If you live in Berkeley county then you owe 2 percent of your home's value if it's less than or equal to $50,000 of value, 2.25 percent if it's greater than $50,000 but not greater than $150,000 and 2.75 percent if it's greater than $150,000.
At the end of the program they want to see the value for the home, the county it resides in and the property tax owed. You must use Switch logic when selecting the county in your code.
<script type="text/javascript">
<!--
//assumptions
var lowValue = 50000;
var medValue = 150000;
var highValue, taxCode, valueCode;
var charleston1 = .01;
var charleston2 = .015;
var charleston3 = .02;
var dorchester1 = .0125;
var dorchester2 = .015;
var dorchester3 = .0175;
var berkeley1 = .02;
var berkeley2 = .0225;
var berkeley3 = .0275;
var county, taxOwed, tax;
var charleston, dorchester, berkeley;
var homeValue = 0;
//input
homeValue = prompt("How much is the property worth?","");
homeValue = parseInt(homeValue);
county = prompt("Which county do you live in?", "");
//calculations
switch (county)
{
case "charleston":
taxCode = charleston;
break;
case "dorchester":
taxCode = dorchester;
break;
case "berkeley":
taxCode = berkeley;
break;
default:
alert("You didnt enter a proper county.");
break;
}
switch (homeValue)
{
case (homeValue):
valueCode = lowValue;
break;
case (homeValue <= medValue):
valueCode = medValue;
break;
case (homeValue > medValue):
valueCode = highValue
default:
break;
}
switch (taxCode)
{
case (charleston && lowValue):
homeTax = charleston1;
break;
case (charleston && medValue):
homeTax = charleston2;
break;
case (charleston && highValue):
homeTax = charleston3;
break
case (dorchester && lowValue):
homeTax = dorchester1;
break;
case (dorchester && medValue):
homeTax = dorchester2
break
case (dorchester && highValue):
homeTax = dorchester3
break;
case (berkeley && lowValue):
homeTax = berkeley1;
break;
case (berkeley && medValue):
homeTax = berkeley2;
break;
case (berkeley && highValue):
homeTax = berkeley3;
break;
Default:
alert("somethings wrong");
}
totalTax = homeValue * homeTax;
//output
document.write("$" + homeValue + " house in " + county + " County.");
document.write("Tax owed: $" + totalTax);
// -->
</script>
var county, homeValue, taxOwed;
//input
homeValue = prompt("How much is the property worth?","");
homeValue = parseInt(homeValue);
county = prompt("Which county do you live in?", "");
switch (county)
{
case "charleston":
if (homeValue <= 50000) {
taxOwed = homeValue * 0.01;
}
else if (homeValue > 50000 && homeValue < 150000) {
taxOwed = homeValue * 0.015;
}
else {
taxOwed = homeValue * 0.02;
}
break;
case "dorchester":
if (homeValue <= 50000) {
taxOwed = homeValue * .0125;
}
else if (homeValue > 50000 && homeValue < 150000) {
taxOwed = homeValue * 0.015;
}
else {
taxOwed = homeValue * 0.0175;
}
break;
case "berkeley":
if (homeValue <= 50000) {
taxOwed = homeValue * 0.02;
}
else if (homeValue > 50000 && homeValue < 150000) {
taxOwed = homeValue * 0.0225;
}
else {
taxOwed = homeValue * 0.0275;
}
break;
default:
alert("You didnt enter a proper county.");
break;
}
//output
alert("Your house in " + county + " has a value of $" + homeValue + " and tax owed is $" + taxOwed);

Categories

Resources