Using HTML, CSS, and JavaScript, Input a user-specified text file consisting of
ID: 3781962 • Letter: U
Question
Using HTML, CSS, and JavaScript, Input a user-specified text file consisting of pairs of values, each on a separate line of text, with the first value in the pair being a word and the second being a percentage (no error-checking for format). Input a second user-specified text file assumed to be a list of words, each on a separate line of text (no error checking for format). Display a scrollable list of the fifteen most extreme words from the second file (i.e., the fifteen words that are in both files and have first-file percentages farthest from 0.5). Display the percentages next to the displayed words. If there are not at least fifteen words shared by the two files, just display a message to that effect.
Explanation / Answer
<!DOCTYPE html>
<html>
<head>
<title>Most Extream Word</title>
<script type="text/javascript">
var fileReader;
var listword={};
var wordPercentage={};
/**
* Check for the various File API support.
*/
function checkFileAPI() {
if (window.File && window.FileReader && window.FileList && window.Blob) {
fileReader = new FileReader();
return true;
} else {
alert('The File APIs are not fully supported by your browser. Fallback required.');
return false;
}
}
/**
* read text input
*/
function readFile1(filePath) {
var fileText = "";
if(filePath.files && filePath.files[0]) {
fileReader.onload = function (e) {
fileText = e.target.result;
var lines = fileText.split(' ');
for(var line = 0; line < lines.length; line++){
var word=lines[line].split(" ")
wordPercentage[word[0]]=parseFloat(word[1]);
}
};
fileReader.readAsText(filePath.files[0]);
}
else {
return false;
}
return true;
}
function readFile2(filePath) {
var fileText = "";
if(filePath.files && filePath.files[0]) {
fileReader.onload = function (e) {
fileText = e.target.result;
console.log(wordPercentage) ;
var lines = fileText.split(' ');
var str=""
for(var line = 0; line < lines.length; line++){
var word=lines[line].trim();
console.log(wordPercentage[word]>0.5);
if(wordPercentage[word]>parseFloat(0.5)){
listword[word] =wordPercentage[word];
str+="<p>"+word+" "+wordPercentage[word]+"</p>";
}
}
console.log(listword);
displayContents(str);
};
fileReader.readAsText(filePath.files[0]);
}
else {
return false;
}
return true;
}
/**
* display content using a basic HTML replacement
*/
function displayContents(txt) {
var el = document.getElementById('result');
el.innerHTML = txt; //display fileText in DOM
}
</script>
<style>
#container{
width:80%;
height:500px;
border:2px solid #CCC;
margin:10%;
}
.content{
margin-top:10%;
margin-left:20%;
margin-right:20%;
padding-left:20%;
}
.content div{
margin-top:2%;
}
#result{
height:100px;
overflow:scroll;
}
</style>
</head>
<body>
<div id="container">
<div class="content">
<div class="File1">
<label>Input File 1:<input type="file" /></label>
</div>
<div class="File2">
<label>Input File 2:<input type="file" /></label>
</div>
<h3>Result</h3>
<div id="result">
</div>
</div>
</div>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.