Javascript code correct?
I’m racking your brains on this question it’s part of a earlier exam paper, as
I haven’t so much done considerably javascript ahead of I’m unclear if that is
correct.If almost any body might help it could be greatly appreciated.
The question is usually as is a follower of:
You happen to be provided using a string assortment called country that contains
the name of the country how the Ski Tavern visits.For example
one element of this array can be as uses:
country0 = “Austria”
A further array identified as villages features strings by using information about
village names and altitude in a particular state.For illustration:
villages0 = “Austria:Seefield 850m*”;
Write your JavaScript job snowReport(countryIndex) that will use
the arrays provided to generate a report off villages in which include your
offered country.The function is going to be passed a strong integer symbolizing
the index of an element with the country range.
A fit is found in the event the country appears in a very string (Hint employ
appropriate JavaScript function to guage if any country name is
found in a hamlet string.)
All complementing villages ought to be displayed.Output ought to be
displayed from the villages div factor you defined just (a).
Output is usually shown for a search for Austria with Figure 1b.
The problem along with your makeArray function is that the variables that contain the array’s usually are local for the function.As a result, when the actual function returns towards the caller the actual variables that contain the arrays will not anymore exist.In addition, a job can only return 1 value – so moving back two arrays seriously isn’t something you can use.
Your application specification tells you that that snowReport functionality should agree to an integer, however you have handed it any reference to a spectrum.Also, it is advisable to SEARCH the actual villages array for just a string in which exists inside the countries range.Something for instance:
villagesi.search( countrycountryIndex )
If your string is usually matched, the position from the match is actually returned – otherwise -1 is usually returned.You incorporate the use of this knowledge to get a issue:
if( villagesi.search( countrycountryIndex )! = -1 )
// the match is usually found!
I construct a sample quickly, but MY PARTNER AND I haven’t tried it to view if it works:
The HTML:
The JavaScript (test.js):
(function()
var united states = “Austria”, “Canada”, “France”, “Germany”, “Switzerland”;
var villages = “Austria:Seefield 850m”, “Austria:Ellam 100m”;
function snowReport( countryIndex )
var strCountry = countrycountryIndex;
var villagesLen = villages.length;
var villagesDiv = document.getElementById(“villages”);
var my spouse and i, output = “”;
for( i=0; my spouse and i < villagesLen; i++ )
if( villagesi.search(strCountry)! = -1 )
output += “Found complement at villages” + my partner and i + “:” + villagesi + “
“;
if( output )
villagesDiv.innerHTML = output;
else
villagesDiv.innerHTML = “No suits found regarding ” + strCountry + ““;
document.onload = snowReport( 0 );
)();
Leave a Reply
You must be logged in to post a comment.