On my site I want to include a palindrome finder where you can drag in a .txt file and it will give you all the palindromes within it. I am able to read this data and log it in my console, but am unable to use the actual data. When I try to get using jquery it fails because it does not recognize the data as a string. I have included my code for better explanation.
HTML:
<div id="drop_zone" ondrop="dropHandler(event);" ondragover="dragOverHandler(event);">
JS:
function dropHandler(ev) {
console.log('File(s) dropped');
// Prevent default behavior (Prevent file from being opened)
ev.preventDefault();
if (ev.dataTransfer.files) {
let files = ev.dataTransfer.files;
let data = "";
file = files[0];
if(file.type.indexOf("text") == 0){
let reader = new FileReader();
reader.onload = function(ev){
data = ev.target.result;
//console.log(data); GIVES ME CONTENTS OF FILE!
//console.log(typeof data);
}
reader.readAsText(file);
//console.log(typeof data);
//$.get("/palindrome" + data, {}, function response(){
//console.log(response);
//});
}
}
}
CPP (I am using Crow backend)
CROW_ROUTE(app, "/palindrome/<string>")([](string data){
crow::json::wvalue x;
x = getPalindromes(data);
return x;
});
/////////////////////////////
#include <unordered_map>
#include <vector>
#include <algorithm>
#include "crow_all.h"
using namespace std;
//Globally declared map
unordered_map<string, bool> hashmap;
crow::json::wvalue getPalindromes(string data){
crow::json::wvalue x;
string word = "";
for(int i = 0; i < data.size(); i++){
if(data[i] == '\n'){
hashmap.insert({word, true});
word = "";
}
else{
word += data[i];
}
}
string temp;
vector<string> words;
int numValid = 0;
for(auto& i: hashmap){
temp = i.first;
reverse(temp.begin(), temp.end());
auto got = hashmap.find(temp);
if(got != hashmap.end()){
words.push_back(i.first);
numValid++;
}
}
x["words"] = words;
x["numValid"] = numValid;
return x;
}