This program uses two files filling two HashMaps to find the population (accordi
ID: 3763723 • Letter: T
Question
This program uses two files filling two HashMaps to find the population (according to the 2010 census) of an individual state or the United States. The two files included in Blackboard are: 1. The name of the states and their abbreviations 2. The abbreviations for each state and their respective population. The first file (#1 above) is a comma separated file – name,abbreviation. The second file (#2 above) is a tab separated file – abbreviation(tab)integer. You should read the first file into a HashMap that can handle the types. You should read the second file into a HashMap that can handle those types. Your task is to create a menu driven system that requests either the name or the abbreviation for a state. If the user enters a name of the state, your program should find the abbreviation for that state (using the first HashMap) and then call the second HashMap to find the population. If conversely the user enters the abbreviation your program should look up the population directly. Please display the results of the request in a clear and concise manner. If the user simply requests the population of the United States you may use the second HashMap and sum the values for all of the states and display that directly.
Explanation / Answer
//i am implementing the code in java script
//save the script in .html format
//upload two files individually
//carefull about the data in the file
/* example file one content "a,b,c,d,e,f,g,h" state and corresponding abbrivations
example file two content "b 12 d 23 f 65 h 55" abbrivation and population
two handle special case enter "united states" exactly */
//below is the script
<html>
<head>
</head>
<body>
<input type="file" id="file1" name="files[]" multiple />
<input type="file" id="file2" name="files[]" multiple />
<input type="text" id="enttext" name="enttex" />
<output id="list"></output>
<script>
var namu=[];
var abbr=[];
var abbr2=[];
var sal=[];
document
.getElementById('file1')
.addEventListener(
'change',
function () {
var fr = new FileReader();
var z=0,l=0;
fr.onload = function () {
var str = this.result;
var res = str.split(",");
for(var i=0;i<res.length;i++)
{
if(i%2==0)
{
namu[z]=res[i];
z++;
}
else
{
abbr[l]=res[i];
l++;
}
}
};
fr.readAsText(this.files[0]);
}
);
document
.getElementById('file2')
.addEventListener(
'change',
function () {
var fr = new FileReader();
var k=0,l=0;
fr.onload = function () {
var str = this.result;
var res = str.split(" ");
for(var i=0;i<res.length;i++)
{
if(i%2==0)
{
abbr2[k]=res[i];
k++;
}
else
{
sal[l]=res[i];
l++;
}
}
};
fr.readAsText(this.files[0]);
}
);
function myFunctioncheck()
{
var popr;
var put1=document.getElementById('enttext').value;
if(put1=="united states")
{
var sun=0;
for(var i=0;i<sal.length;i++)
{
sun=sun+parseInt(sal[i]);
}
document.getElementById('list').textContent=sun;
return;
}
for(var i=0;i<namu.length;i++)
{
if(namu[i]==put1)
{
for(var j=0;j<abbr.length;j++)
{
if(abbr[i]==abbr2[j])
{
popr=sal[j];
}
}
}
else
{
if(put1==abbr[i])
{
for(var k=0;k<abbr2.length;k++)
{
if(abbr[i]==abbr2[k])
popr=sal[k];
}
}
}
}
document.getElementById('list').textContent=popr;
}
</script>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.