/    Sign up×
Community /Pin to ProfileBookmark

Need a concept explained

I have drank tea, coffee and Jolt Cola to awaken my mind but it hasn’t worked so far. With that said….

I am looking for a plain English primer or explanation on how to do something that even a 2 yr old can understand. My brain is just not the idea of how something is done conceptially based upon the current reading/sites I have found.

What I want to do is create 2 arrays:

[code]
[I]var blah = new Array(“blahA”, “blahB”) [/I]
[I]var widget = new Array(“widgeA”, “widgeB”, widgeC”[/i]

// assign each item in the arrays with multiple fixed values –
blahA = val1, val2
blahb = val3, val4
widgeA = val5, val6
widgeB = val7, val8
widgeC = val9, val10

// with these results, I then do the following:

function doitnow {

if blah = blahA
and widget = widgeA
then val1 – val5
if blah = blahB
and widge = widgeA
then val3 – val5
if blah = blahA
and widget = widgeB
then val1 – val7
and so on…
}
[/code]

Could someone try to explain or point me to a primer on how to do this in such a way that even a 2 yr old can understand? That is what it seems like my mental capacity is today. Maybe I need drink some more tea/coffee/Jolt Cola…

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@JunkMaleNov 26.2011 — You have two arrays, I suggest that you simplfy the process by having to iterate one array by concatenating the two arrays in to one.
[CODE]valueX=0;
val = blah.concat( widgit );
results = [];
for(v=0; v<val.length; v++){
if( typeof results[ val[v] ] == "undefined" ) results[ val[v] ] = [];
for(innerLoop=0; innerLoop<=1; innerLoop++){
valueX++;
results[ val[v] ].push( "val"+valueX );
}
}
[/CODE]
I am only guessing that this is what you want and that the routine does the job requested, that would be up to yourself to determine if it could be fit to meet those needs.

In short, the logic is this....

valueX =0

create single array out of blah and widgit

create a single 'results' array

loop through the new array called val ..

..if the results array for the results[arrayName] does not exist, create it

..loop twice and ....

....increment the value of valueX

....push the concatenated string in to the array element named blahA,b,c...

....end of loop twice

..end of main loop

The question is this... Was this what your attempting to do? Create an array of associated arrays and values? If so then that demo should be something along those lines, it does require more information on what your trying to do other than make a multi dimensional array.

The question is what is the purpose of the function doitnow(){} all about?

The demo above produces an associative array of array elements, eg.

val['blahA']=['val1','val2']

val['blahB']=['val3','val4']

So without knowing exactly what your data for val1, val2 should be and how its to be used, its hard to advise.
Copy linkTweet thisAlerts:
@flyboynmauthorNov 26.2011 — Thank you for the reply. I was trying to get a general idea of how to go about something. I don't want you to think you are doing my homework for me.

What I am trying to do is this:

I have a variable that can only be 2 things - onething or twothing. These correspond to a size.

I have a second variable that can be only 4 things - 1thing, 2thing, 3thing or 4thing. These are fixed numeric values.

I want to be able to have a user chose between onething or twothing. I then want user to be able to chose between 1thing, 2thing, 3thing and 4thing.

With those choices, I will then have a function that show if 1thing or 2thing, etc will fit in onething or twothing in space and weight.
Copy linkTweet thisAlerts:
@JunkMaleNov 26.2011 — Well whatever it is your trying to do, you will need to break the process in to stages whilst you experiment.

Often its best to hand code arrays that will have static data at start up.
Copy linkTweet thisAlerts:
@flyboynmauthorNov 26.2011 — This is what I have so far. I have commented in what I am planning on doing. What do you think? Any suggestions for me to change?:

<i>
</i>&lt;html&gt;
&lt;title&gt; Can you get away with the loot? &lt;/title&gt;
&lt;head&gt;
&lt;body style="background-color:tan;"&gt;
&lt;h2&gt;&lt;center&gt;&lt;p&gt; In the movide &lt;i&gt;The Italian Job&lt;/i&gt;, Mini Coopers were used to steal gold bars.
&lt;br&gt;
The cars were modified to take the extra weight but did not have any extra cargo space added.
&lt;br&gt;Now it is your time to steal the bars!&lt;/center&gt;&lt;/h2&gt;
&lt;p&gt;
&lt;p&gt;
You will need to find out if you can fit the heist in the car. <br/>
&lt;br&gt;
This will be based upon the weight of the metal bars and the cargo capacity of Mini you choose.
&lt;br&gt;
You get to choose which model of Mini you use for your heist. <br/>
&lt;p&gt;Mini Coopers come in 2 different models - 2 door and 4 door.
&lt;br&gt;The 2 door has 24 cubic feet of cargo space.
&lt;br&gt;The 4 door has 35.6 cubic feet of cargo space.
&lt;p&gt;Select the model that you wish to use.
&lt;p&gt;
&lt;input type="radio" name="car" onclick="chose(this, '2doors')" /&gt; 2 Door Mini
&lt;input type="radio" name="car" onclick="chose(this, '4doors')" /&gt; 4 Door Mini
&lt;/p&gt;&lt;p&gt;
&lt;p&gt;
Now choose the metal you want to steal.
&lt;p&gt;
&lt;input type="radio" name="metal" onclick="chose(this, 'Al')" /&gt; Aluminum
&lt;input type="radio" name="metal" onclick="chose(this, 'Au')" /&gt; Gold
&lt;input type="radio" name="metal" onclick="chose(this, 'Pt')" /&gt; Platinum
&lt;input type="radio" name="metal" onclick="chose(this, 'Ag')" /&gt; Silver
&lt;/p&gt;
&lt;input type="button" value="Can it be done?" onclick="document.metal.car.value= fit(document.metal.car.value);" /&gt;
&lt;p&gt;
&lt;p&gt;
&lt;hr /&gt;
&lt;p&gt;
Your chosen model of Mini can carry &lt;!-- modelmax --&gt; bars.
This will take up &lt;!-- spaceused --&gt; of the &lt;!-- spaceavail --&gt; cargo space available.
&lt;!-- function willfit --&gt;
&lt;br&gt;
&lt;p&gt;
&lt;p&gt;
If you load 1/3 of the &lt;!-- car --&gt; Mini Cooper's cargo space.
it would take &lt;!-- maxbars --&gt; of &lt;!-- metal --&gt;
The total weight of this cargo is &lt;!-- metalweight --&gt;.
&lt;!-- maxWeight --&gt;


&lt;script&gt;

// setting variables in arrays for the car model, max bars each can carry.
var carModel = new Array("2doors", "4doors")
var maxBars = new Array(933, 1276) // maximum # of bars the mini can carry

// setting values of total cargo space, types of metal and how many bars it will take to equal 1800 lbs or less
var totalCargoSpace = new Array (41472, 56672) //total available cargo space available in cubic inches
var metalType = new Array("Pt", "Ag", "Au", "Al") //setting the metal type of bars
var maxMetalWeight = new Array(53, 107, 58, 414) // number of bars that will less than or equal to 1800 lbs

// do the lookup into parallel arrays
function getData(form) {
// make a copy of the text box contents
var inputText = form.carName.value
// loop through all entries of carModel array
for (var i = 0; i &lt; carModel.length; i++) {
// compare uppercase versions of entered text against one entry
// of carModel
if (inputText.toUpperCase() == carModel[i].toUpperCase()) {
// if they're the same, then break out of the for loop
break
}
}
// make sure the i counter hasn't exceeded the max index value
if (i &lt; carModel.length) {
// display corresponding entries from parallel arrays
form.manager.value = metalType[i]
form.quota.value = maxWeight[i]
} else { // loop went all the way with no matches
// empty any previous values
form.manager.value = ""
form.quota.value = ""
// advise user
alert("No match found for " + inputText + ".")
}
}
function willfit (
if (cargospace - maxBars) &lt; 0

<i> </i> document.write("It will not work. You will not get away.";

<i> </i> else

<i> </i> document.write("Congratuations! You will be able to get away!";
)

&lt;/script&gt;
&lt;/body&gt;&lt;/html&gt;
Copy linkTweet thisAlerts:
@JunkMaleNov 27.2011 — I would, if your using drop down menus or radio groups to dispense with the type of car array as you already have that information in the form and your limited to a choice of one out of the group. Setting the one radio in the group to checked will negate the need to test if one is checked as one will be checked and you then can concentrate on the inner workings of what array the input relates to.

information in the cargo space can be passed in the function call [B]chose([COLOR="Navy"]this[/COLOR], '2doors',[COLOR="Red"]933[/COLOR])[/B], similar for the metal types.

You have then negated the need for most of the checking routines you have as well as removing the need for arrays, you can pass all the user information to the functions that calculate the number of bars, etc.
Copy linkTweet thisAlerts:
@flyboynmauthorNov 27.2011 — Thank you for showing me the syntax of that. I am going to work on it a bit more today. I will look at the assignment's directions again to make sure that she allows for us to do the radio boxes. For some strange reason, I could have sworn she wanted an array but if not, I am home free. ?
Copy linkTweet thisAlerts:
@JunkMaleNov 27.2011 — Well it depends on what is asked, if you have been specifically asked to do a task in a certain fashion then its often best to follow that route as it is part of the scoring process.

If this was an open task where you find the best working method, then I would opt for a method that means that I am not having to replicate data that already exists. When you look at a check box group or radio group or select group, they are arrays or objects in an array. So having javascript replicate that information in many ways is pointless.

If you needed a car array then you could have [CODE]cars = [{car:mini,doors:2,bars:933,cubic:41472},{car:mini,doors:4,bars:1276,cubic:56672}];
metals = [{metal:'Pt',bars:58},{metal:'Ag',bars:107},{metal:'Au',bars:58},{metal:'Al',bars:414}];
[/CODE]
So... [CODE]m = Metals[2].metal; // will = Au
c = cars[1].bars; // will = 1276[/CODE]

This allows you to group together the data in tidy bundles and meets your specification for the use of arrays and also allows you to set the data as a set of objects in an array.

Then you can on submit of the form check the inputs and match to the array data and calculate if the vehicle will get away or not.
Copy linkTweet thisAlerts:
@flyboynmauthorNov 27.2011 — That is exactly what I was looking for. Thank you very much. I am going to work on my code a bit more and then post what I have once I get that far.

Thank you for teaching this old dog new tricks.
×

Success!

Help @flyboynm spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...