7 Replies - 570 Views - Last Post: 06 December 2011 - 05:36 AM

#1 flyboynm  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 04-December 11

Am I going in the right direction?

Posted 04 December 2011 - 06:53 PM

I am VERY new to javascript coding. I am trying to write a program that will take my selections from radio boxes. There are 2 arrays - one for metals and the other for model of car.

When you select the model of car and the metal, you need to return how many bars fit in the area.

It will then take the amount of total bars that can fit and then multiply that by .33. This in turn is multiplied by the weight of the metal (I set that as part of an array) and the result is subtracted from 1800. If that result is < 0, it will respond "You did not get away" else it respond "you got away!"

I know this code is far from done but I want to see if I am going the right direction.
<HTML>
<title>
Trying to get this program to work.
</title>
<script type="text/javascript">
<head>
var carType = new Array();
carType["2 door"]=41446;
carType["4 door"]=52776;

var metal= new Array();
metal["Al"]=4.3512;
metal["Au"]=30.9912;
metal["Pt"]=34.09692;
metal["Ag"]=16.8276;

function weightUsed()
{
var spaceUsed=0;
var theForm = document.forms["carform"];
var selectedCar = theForm.elements["selectedCar"];
for(var i = 0; i < selectedCar.length; i++)
{
if(selectedCar[i].checked)
{
spaceUsed = carType[selectedCar[i].value];

break;
}
}
return spaceUsed;
}
function getmetalWeight()
{
var carmetalspace=0;
var theForm = document.forms["carform"];
var selectedmetal = theForm.elements["metal"];
carmetalspace = metal[selectedmetal.value];
return carmetalspace;
}
{
var theForm = document.forms["carform"];
{
var theForm = document.forms["carform"];
}
function calculateTotal()
{

var metalWeight = weightUsed() + getmetalWeight();
var divobj = document.getElementById('totalWeight');
divobj.style.display='block';
divobj.innerHTML = "The weight of the metal in the space is "+metalWeight;
}
function hideTotal()
{
var divobj = document.getElementById('totalWeight');
divobj.style.display='none';
}
</script>
<body style="background-color:burlywood">
In <i>The Italian Job</i>, theives were able to steal bars of gold by loading them into Mini Coopers and driving away.<p>
<p>
It is now your turn to pull your own version of <i>The Italian Job</i>.  You are able to choose what model of Mini you use and the type of metal bar you want to steal.<p>
Thes rules are:<ul>
<li>You can only load the cargo area of your Mini 1/3 of the way full</li>
<li>The cargo area used cannot weigh more than 1800 lbs.</li>
</ul>
<p>
The models of Mini available are either  a 2 or a 4 door.  The 2 Door has 25 cubic feet of cargo space - the 4 door 35.4.  
<p>
The metal bars are Aluminum, Gold, Platinum and Silver.
<p>
<div>
<form>
Which car do you want to use? <br />
<Input type = "radio" Name = car Value = "2door">2 door <br />
<Input type = "radio" Name = car Value = "4door">4 door <br />
<br />
<p> Now choose the metal you want to try to steal. <br />
<Input type = "radio" Name = metal Value = "Al">Aluminum <br />
<Input type = "radio" Name = metal Value = "Au">Gold <br />
<input type = "radio" Name = metal Value = "Pt">Platinum <br />
<input type = "radio" Name = metal Value = "Ag">Silver <br />
</form>
</div>
</body>
</HTML>



Is This A Good Question/Topic? 0
  • +

Replies To: Am I going in the right direction?

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Am I going in the right direction?

Posted 04 December 2011 - 07:22 PM

The beauty of programming is you can approach a problem in many different ways. If it works so far then chances are you are in the right direction. Instead of second guessing yourself, why not try to finish it first. If your stumped, then take your code here for the others to see. Otherwise, keep to yourself. It is a good practice so you won't rely much on others for your coding, and that is certainly a good thing. ^^
Was This Post Helpful? 0
  • +
  • -

#3 flyboynm  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 04-December 11

Re: Am I going in the right direction?

Posted 04 December 2011 - 07:35 PM

I appreciate the tips. I am trying to figure out how to go from here. I just wanted to see if I was going the right direction with what I have put up so far or have I already made a huge mistake?
Was This Post Helpful? 0
  • +
  • -

#4 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Am I going in the right direction?

Posted 04 December 2011 - 07:50 PM

Lemme check...
Was This Post Helpful? 0
  • +
  • -

#5 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Am I going in the right direction?

Posted 04 December 2011 - 07:56 PM

Firebug showed me this error:

syntax error
var spaceUsed=0;


Which is valid, if you look at it. Real problems:

1. What are you trying to achieve with this?
{
var theForm = document.forms["carform"];
{
var theForm = document.forms["carform"];
}


2. head tag is not closed and not properly placed.
Was This Post Helpful? 0
  • +
  • -

#6 flyboynm  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 04-December 11

Re: Am I going in the right direction?

Posted 05 December 2011 - 05:57 PM

I am getting there. I am now trying to figure out why I am not getting a respond function to work in my code.
Could you look at my code and tell me where I am going wrong so I can fix it?


<!DOCTYPE html>
<html>
<head>
<title>The Italian Job</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script type="text/javascript" > //src="italianjob.css">

	//Set up array that represent the car storage space in cubic inches
var car_type = new Array();
		car_type["2door"]=41446;
		car_type["4door"]=52776;

	//Set up array that represents the metal_type weight
var metal_type= new Array();
		metal_type["selectmetal"]=0;
		metal_type["Aluminum"]=4.3512;
		metal_type["Gold"]=30.9912;
		metal_type["Platinum"]=34.09692;
		metal_type["Silver"]=16.8276;

	// getCarStorage() finds the storage based on the car storage space.

function getCarStorage()
{
var carStorageArea=0;
		//refers form id="carForm"
var theForm = document.forms["carForm"];
	
var selectedCar = theForm.elements["selectedCar"];
		//Here since there are 2 radio buttons selectedCar.length = 2
		//We loop through each radio buttons
for(var i = 0; i = selectedCar.length; i++)
{
		//if the radio button is checked
if(selectedCar[i].checked)
{
		//by using the car_type array
		//We get the selected item value such as car_type["4door".value]"
carStorageArea = car_type[selectedCar[i].value];
		//If we get a match then we break out of this loop
		
break;
}
}
		//We return carStorageArea
return carStorageArea;
}
		//This function finds the metal bar weight
function getMetalDensity()
{
var carStorageSpace=0;
		//Get a reference to the form id="carForm"
var theForm = document.forms["carForm"];
		//Get a reference to the select id="metal"
var selectedmetal = theForm.elements["metal"];
		//equal to value user chose such as metal_type["Aluminum".value] would be equal to 4.3512
carStorageSpace = metal_type[selectedmetal.value];
		//finally we return carStorageSpace
return carStorageSpace;
}
function calculateTotal()
{
		//Here we get the total by calling our function
		//Each function returns a number
var carUsed = getCarStorage() / 440;
var howMany = carUsed * .33;
var fit = cartype / 44.4;
var weightBars = 1800 / metal_type;
var total = weightBars * metal_type;
var barLimit = carUsed - howmany;
var weightLimit = howMany - weightBars;

var divobj = document.getElementById('total');
	if (barLimit <0)
  {
	document.write("<This will not work.");
  }
	else if (weightLimit <0)
  {
	document.write("This will not work.");
  }
else
respond = 'With the total cargo area of the chosen car, you are able to fit ' +carUsed.toFixed(0) + 
		' bars in it. Due to weight limit of 1800 lbs, you are limited to ' + weightBars.toFixed(0) + 
		' bars in the cargo area.  The weight of the bars you want to have onboard the car is ' + total.toFixed(2) +
			' \n';
return respond}
</script>
</head>
<body style="background-color:burlywood" > <!-- onload="hideTotal()" -->
<p>In <em>The Italian Job</em>, theives were able to steal bars of gold by loading them into Mini Coopers and driving away.</p>
<p>
It is now your turn to pull your own version of <em>The Italian Job</em>.  You are able to choose what model of Mini you use and the type of metal bar you want to steal.
Thes rules are:
<ul>
<li>You can only load the cargo area of your Mini 1/3 of the way full</li>
<li>The cargo area used cannot weigh more than 1800 lbs.</li>
</ul>
<form id="carForm" onsubmit="return false;">
<label>What car do you want to use?</label>
<br />
<label class="radiolabel">
<input type="radio" onclick="calculateTotal()" value="2door" name="selectedCar" />
2 Door Mini - 25.4 cubic inches of cargo space
</label>
<br />
<label class="radiolabel">
<input type="radio" onclick="calculateTotal()" value="4door" name="selectedCar" />
4 door Mini - 35 cubic inches of cargo space
</label>
<p>Now you get to choose which metal you wish to steal.
</p>
<select id="metal" onchange="calculateTotal()" name="metal">
<option value="selectmetal">Select the metal bar type</option>
<option value="Aluminum">Aluminum</option>
<option value="Gold">Gold</option>
<option value="Platinum">Platinum</option>
<option value="Silver">Silver</option>
</select>
<br />
</form>
<div id="total" style="display: selectmetal;">
</div>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: Am I going in the right direction?

Posted 06 December 2011 - 04:47 AM

First:

for(var i = 0; i = selectedCar.length; i++)

Instead of a loop termination condition, you've set the loop index to 2, which is out of bounds of your array which only has elements 0 and 1.

Second:
Variable name differences:
var car_type = new Array();
car_type["2door"]=41446;      
car_type["4door"]=52776;

var fit = cartype / 44.4;


var howMany = carUsed * .33;

var barLimit = carUsed - howmany;


Think there are still a lot of other problems as well, with logic and flow.

Do yourself a favor and use either Firefox with Firebug or Chrome with Developer Tools so you can try to solve these problems on your own.
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: Am I going in the right direction?

Posted 06 December 2011 - 05:36 AM

View PostJackOfAllTrades, on 06 December 2011 - 12:47 PM, said:

Second:
Variable name differences:
var car_type = new Array();
car_type["2door"]=41446;      
car_type["4door"]=52776;

besides that, this "array" won’t work as expected: alert(car_type.length); // 0
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1