how to tell ascii to send character and not code - javascript

How can i make ASCII send what is typed than code.
I mean if A is type on a keyboard the result on php should be the letter typed and not the Code
var ucode = function(s) {
var len = s.length;
var rs = "";
for ( var i = 0; i < len; i++) {
var k = s.substring(i, i + 1);
rs += "$" + (s.charCodeAt(i) + "1") + ";";
}
return rs;
};

Related

How to iterate a JavaScript code for two objects

I would like to apply same piece of code to two objects in JavaScript.
When calling getElementsByClass ,there appears 2 objects in my website.So I would like to apply the same code for both of them.Currently I'm applying it to only one Object (text[0]) and I would like to implement it also to text[1] .
var text=document.getElementsByClassName("th");
var text =text[0];
var newDom = '';
var animationDelay = 6;
for(let i = 0; i < text.innerText.length; i++)
{
newDom += '<span class="char">' + (text.innerText[i] == ' ' ? ' ' : text.innerText[i])+ '</span>';
}
text.innerHTML = newDom;
var length = text.children.length;
for(let i = 0; i < length; i++)
{
text.children[i].style['animation-delay'] = animationDelay * i + 'ms';
}
}
I think you want to do the same thing with using item[0] and item[1] together.
You can create a function. Or call this function by iterating your items too.
var text=document.getElementsByClassName("th");
function myFunc(text) {
var newDom = '';
var animationDelay = 6;
for(let i = 0; i < text.innerText.length; i++)
{
newDom += '<span class="char">' + (text.innerText[i] == ' ' ? ' ' : text.innerText[i])+ '</span>';
}
text.innerHTML = newDom;
var length = text.children.length;
for(let i = 0; i < length; i++)
{
text.children[i].style['animation-delay'] = animationDelay * i + 'ms';
}
}
}
myFunc(text[0]); // call functions with your items.
myFunc(text[1]);

Conversion of JSON to CSV using Javascript doesnt give Keys

I have converted JSON to CSV using JavaScript but in a bizarre fashion, I don't see the headers being transferred to CSV file. I only see the corresponding values.
Below is the example of
1) JSON ....
[
{
"entityid": 2,
"personid": 45676
}
]
2) JavaScript code ....
function DownloadJSON2CSV(objArray)
{
alert(objArray);
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '';
for (var i = 0; i < array.length; i++) {
var line = '';
for (var index in array[i]) {
//line += array[i][index] + ',';
if (line != '') line += ','
line += array[i][index];
}
alert(line);
// Here is an example where you would wrap the values in double quotes
// for (var index in array[i]) {
// line += '"' + array[i][index] + '",';
// }
//line.slice(0,line.Length-1);
str += line + '\r\n';
}
alert(str);
window.open( "data:text/csv;charset=utf-8," + escape(str))
}
3) CSV Output ....
2,45676
I should see the keys - entityid and personid also in CSV in the first line of the document, but I don't.
This code will extract headers from the json keys additionally it will double quote the fields which include commas in it.
function convertToCSV(objArray) {
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '';
var keys = [];
for(var k in objArray[0]) keys.push(k);
for (var i = 0; i < keys.length; i++)
{
if(i==keys.length-1){str=str+keys[i]+'\r\n'}
else {str=str+keys[i]+','}
}
for (var i = 0; i < array.length; i++) {
var line = '';
for (var index in array[i]) {
if (line != '') line += ','
if(array[i][index].toString().includes(",") && typeof array[i][index] === 'string'){array[i][index]="\""+array[i][index]+"\""}
line += array[i][index];
}
str += line + '\r\n';
}
return str;
}
Usage: (for Node.js)
var fs = require('fs'); //**run** npm install fs **if not installed yet in cmd**
var arrayOfObjects = [{"id":28,"Title":"Sweden"}, {"id":56,"Title":"USA"},{"id":89,"Title":"England"}];
fs.writeFile("./test.csv", convertToCSV(arrayOfObjects));
You hadn't set it up to output the header line.
function DownloadJSON2CSV(objArray)
{
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '';
var headers = new Array();
for (var i = 0; i < array.length; i++) {
var line = '';
var data = array[i];
for (var index in data) {
headers.push(index);
if (line != '') {
line += ','
}
line += '"' + array[i][index] + '"';
console.log('line: ' + line);
}
str += line + ((array.length>1) ? '\r\n' : '');
line = '';
}
headers = ArrNoDupe(headers);
console.log('headers: ' + headers);
console.log('str: ' + str);
str = headers + '\r\n' + str;
console.log('final str: ' + str);
window.open( "data:text/csv;charset=utf-8," + escape(str));
}
function ArrNoDupe(a) {
var temp = {};
for (var i = 0; i < a.length; i++)
temp[a[i]] = true;
var r = [];
for (var k in temp)
r.push(k);
return r;
}
CSV outputs like so...
entityid,personid
"2","45676"

In a js regexp should i escape angle brackets? [duplicate]

I am trying to get a regex expression to accept < and > as my outside delimiters to grab all the content in between them.
so content like such
< tfdsfa >
should be grabbed.
Do I have to escape the < and > characters or something?
Regex generated by my script:
/<[^(>)]*>/g
Code from file:
data.method.highlight = function() {
var x = data.syntax,
text = data.$.span.html();
for (var i=0, len = x.length; i < len; i++) {
var rx;
if (x[i].range) {
rx = new RegExp(x[i].tag[0] + "[^(" + x[i].tag[1] + ")]*" + x[i].tag[1], "g");
console.log(rx);
}
else {
var temprx = x[i].tag[0];
for (var z = 1; z < x[i].tag.length; z++) {
temprx += "|" + x[i].tag[z];
}
rx = new RegExp(temprx, "g");
}
text = text.replace(rx,function (match) {
console.log("looping - range");
return '<span class="' + x[i].class.default + '">' + match + '</span>';
});
data.$.span.html(text);
}
};
Neither < nor > are metacharacters inside a regular expression.
This works for me:
'<foo> and <bar>'.match(/<[^>]*>/g); // ["<foo>", "<bar>"]
You could be having a problem when you try to insert the result into HTML and the browser thinks that it is not a valid HTML tag, like <blablabla>.

javascript printing to previous line

I have a bit of code that requires printing underscores but to the line above it, how would i do this? I'm not sure how to print the underscore to the previous line, not much experience with javascript. thanks!
var landscape = function() {
var result = "";
var flat = function(size) {
for (var count = 0; count < size; count++)
result += "_";
};
var hill = function(size) {
result += " /";
for (var count = 0; count < size; count++)
result += ""+
"_";
result += " \\";
};
//BUILD SCRIPT
flat(3)
hill(4);
flat(6);
hill(1);
flat(1);
//END SCRIPT
return result;
it makes this ___ /____ \______ /_ \_`enter code here`enter code here`
and i want this
_____ ___
___/ \__/ \____/\_
You can keep track of the two lines separately and then concatenate them just before returning the result.
JS:
function landscape() {
var resultTop = '';
var resultBottom = '';
function hill(size) {
resultTop += ' ';
resultBottom += '/';
for (var i = 0; i < size; i++) {
resultTop += '_';
resultBottom += ' ';
}
resultTop += ' ';
resultBottom += '\\';
}
function flat(size) {
for (var i = 0; i < size; i++) {
resultTop += ' ';
resultBottom += '_';
}
}
flat(3);
hill(4);
flat(6);
hill(1);
flat(1);
var result = resultTop + '<br/>' + resultBottom;
return result;
}
Here's a fiddle.
One workaround is to print a unicode character that draws a line on top. Turns out there is such a character: the Upper One-eighth Block
It's "\u2594" in unicode escape or ▔ as HTML entity or you can simply copy/paste the literal character from the example below:
____/▔▔▔▔\____/▔▔\___

How to change the = character to "with the value"

I have this from another question:
document.getElementById('value').innerHTML=listCookies()
function listCookies() {
var theCookies = document.cookie.split(';');
var aString = '';
for (var i = 1 ; i <= theCookies.length; i++) {
aString += ' ' + theCookies[i-1] + "\n";
}
return aString;
}
It will say:
cookie=value
How can I make it like this:
cookie with the value 4
function listCookies() {
var theCookies = document.cookie.split(';');
var aString = '';
for (var i = 0 ; i < theCookies.length; i++) {
aString += ' ' + theCookies[i].replace('=', ' with the value ') + "\n";
}
return aString;
}

Categories

Resources