<body onload="checkFileAPI();">
<div id="container">
<input type="file" onchange='readText(this)' />
<br />
<hr />
<h3>Contents of the Text file:</h3>
<div id="main">
...
</div>
</div>
</body>
JavaScript
var reader;
let triNumbers = []
function readText(filePath) {
var output = ""; //placeholder for text output
if (filePath.files && filePath.files[0]) {
reader.onload = function(e) {
output = e.target.result;
displayContents(output.replaceAll(/(^"|"$)/g,'').split("\",\""));
}; //end onload()
reader.readAsText(filePath.files[0]);
} //end if html5 filelist support
else if (ActiveXObject && filePath) { //fallback to IE 6-8 support via ActiveX
try {
reader = new ActiveXObject("Scripting.FileSystemObject");
var file = reader.OpenTextFile(filePath, 1); //ActiveX File Object
output = file.ReadAll(); //text contents of file
file.Close(); //close file "input stream"
displayContents(output);
} catch (e) {
if (e.number == -2146827859) {
alert('Unable to access local files due to browser security settings. ' +
'To overcome this, go to Tools->Internet Options->Security->Custom Level. ' +
'Find the setting for "Initialize and script ActiveX controls not marked as safe" and change it to "Enable" or "Prompt"');
}
}
} else { //this is where you could fallback to Java Applet, Flash or similar
return false;
}
return true;
}
function getNextTriNumber(){
let num=triNumbers.length+1
triNumbers.push(.5*num*(num+1))
}
function getWordValue(word){
let value=0;
for(let i=0;i<word.length;i++){
value+=word.charCodeAt(i)-64
}
return value
}
function displayContents(txt) {
var el = document.getElementById('main');
el.innerHTML = txt; //display output in DOM
//Project euler
let amt=0
txt.forEach(word=>{
let v=getWordValue(word)
//console.log(word,v)
while(triNumbers.length ===0 || triNumbers[triNumbers.length-1]<= v){
getNextTriNumber()
}
if(triNumbers.includes(v)) amt++
})
console.log(amt)
}
function checkFileAPI() {
if (window.File && window.FileReader && window.FileList && window.Blob) {
reader = new...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.