I am getting the this message "Uncaught TypeError: Cannot read property 'split' of null" for this linesplitted = elements[i].nextSibling.nodeValue.split("||"); The code is in below. I will appreciate your input to solve this. The code has worked before. I think it is something about new php oder jquery...
$('#r_NEStd').hide(); // Das Feld ist am Anfang versteckt und duch klicken
auf NE wird wieder gezeigt.
$("input[name='x_Posten[]'], input[name='x_NE[]']").click(function() { //
Field2 has multiple inputs(checkboxes) so they should be selected by name
document.getElementById("x_Total").readOnly = true;
document.getElementById("x_Auslagen").readOnly = true;
document.getElementById("x_RBetrag").readOnly = true;
if ($("input[name='x_Posten[]']:checked").val() &&
$("input[name='x_NE[]']:checked").val()) { // WENN NichtErhaeltlichkeit und
Posten geklickt sind
document.finvoicesadd.x_Total.value = '';
var Anzahl = 0;
var Auslagen = 0;
var AuslagenMwSt = 0;
var Total = 0;
var splitted;
var nestdsatz = document.finvoicesadd.x_NEStd.value;
var e = document.getElementById("x_Vorschuss");
var VorschussValue = e.options[e.selectedIndex].value;
var nestdsatz = document.finvoicesadd.x_NEStd.value;
var elements = document.getElementsByName("x_Posten[]");
for (i = 0; i < elements.length; i++) {
if (elements[i].checked) {
splitted = elements[i].nextSibling.nodeValue.split("||");
Auslagen = Auslagen + +parseFloat(splitted[4]);
AuslagenMwSt = AuslagenMwSt + +(parseFloat(splitted[4]) *
(1 + parseFloat(splitted[5])));
Anzahl = Anzahl + +parseFloat(splitted[2]);
Total = Total + +(parseFloat(splitted[2] * nestdsatz) *
(1 + parseFloat(splitted[5])));
}
}
document.finvoicesadd.x_Auslagen.value = Auslagen.toFixed(2);
document.finvoicesadd.x_Total.value = (Total + AuslagenMwSt).toFixed(2);
document.finvoicesadd.x_RBetrag.value = (Math.round(((Total +
AuslagenMwSt) - VorschussValue) * 20, 0) / 20).toFixed(2);
document.finvoicesadd.x_Vorlage.value = "rechnungne";
} else { // WENN x_Posten oder NE nicht oder nur ein Feld gelickt ist
document.finvoicesadd.x_Total.value = '';
var Anzahl = 0;
var Auslagen = 0;
var Total = 0;
var splitted;
var e = document.getElementById("x_Vorschuss");
var VorschussValue = e.options[e.selectedIndex].value;
var nestdsatz = document.finvoicesadd.x_NEStd.value;
var elements = document.getElementsByName("x_Posten[]");
for (i = 0; i < elements.length; i++) {
if (elements[i].checked) {
splitted = elements[i].nextSibling.nodeValue.split("||");
// Auslagen = Auslagen+ +(parseFloat(splitted[4])*
(1 + parseFloat(splitted[5])));
Auslagen = Auslagen + +parseFloat(splitted[4]);
Anzahl = Anzahl + +parseFloat(splitted[3]);
Total = Total + +parseFloat(splitted[6]);
}
}
document.finvoicesadd.x_Auslagen.value = Auslagen.toFixed(2);
document.finvoicesadd.x_Total.value = (Total).toFixed(2);
document.finvoicesadd.x_RBetrag.value = (Math.round(((Total) -
VorschussValue) * 20, 0) / 20).toFixed(2);
document.finvoicesadd.x_Vorlage.value = "rechnung";
}
});
Replace this:
if (elements[i].checked) {
with this:
debugger;
if (elements[i].checked && elements[i].nextSibling.nodeValue != null) {
This will solve the error, although I have no idea which repercussions it will have on your program.
The error is given by the fact that elements[i].nextSibling.nodeValue is null when you're checking it.
Related
I'm trying to create a textarea with some content inside and i need the height to be based on the content size.
cargarClases() {
var containerGeneral = document.querySelector(".bimContainer").querySelector(".containerInfo--list").querySelector("nav").querySelector("ul");
for (let i = 0; i < 15; i++) {
var li = document.createElement('li');
li.classList.add("classDesplegable");
containerGeneral.appendChild(li);
var image1 = document.createElement('img');
image1.src = '../view/manageproject/img/FlechaBim.svg';
li.appendChild(image1);
var p = document.createElement('p');
p.innerHTML = "Nombre de la clase";
li.appendChild(p);
var ul = document.createElement('ul');
ul.classList.add("dNone");
containerGeneral.appendChild(ul);
for (let j = 0; j < 4; j++) {
var li1 = document.createElement('li');
ul.appendChild(li1);
var p = document.createElement('p');
p.innerHTML = "Nombre de la c...";
p.classList.add("textClass");
li1.appendChild(p);
var p2 = document.createElement('p');
p2.innerHTML = "Nombre de la c...";
p2.classList.add("textClass2");
li1.appendChild(p2);
var textarea = document.createElement("textarea");
function updateHeight(el) {
el.style.height = '';
el.style.height = el.scrollHeight + 'px';
}
updateHeight(textarea);
textarea.addEventListener('input', () => updateHeight(textarea));
textarea.maxLength = "44"
textarea.innerHTML = "P1D30Tr0M0sC0WMul3P1D30Tr0M0sC0WMul3";
if (i % 2 == 0) {
//input.disabled = "true"; togliere commento quando sapremo quali classi possano essere editate e quali no
textarea.classList.add("textArea");
textarea.classList.add("line-clamp");
} else {
textarea.classList.add("textArea");
}
li1.appendChild(textarea);
var check = document.createElement('input');
check.type = 'checkbox';
check.classList.add("inputCheck2");
li1.appendChild(check);
}
}
}
This is what i've found on internet but it doesn't work. The height output is always 0px.
Any help is appreciated because i'm going crazy.
Have a nice day!
Edited so maybe it's easier to understand the problem
I am creating a function to calculate the division of 2 numbers that are inside an array. The problem I have is that the division is done the other way around. ej: if I put 10/2 as parameters, the result that returns is 2/10. So how could I solve this error?
Here I leave the complete code, just check the code of the function "fDividir".
Code javascript:
sysc es un sistema que podrás usar para implementar para la creación de tu calculadora.
// inicio de las funciones
//función sumar
const fSumar = function (valores = "0"){
let arregloNum = valores.split("+");
let oSuma = 0;
for (let i = 0; i < arregloNum.length; i++){
oSuma = parseInt(arregloNum[i]) + oSuma;
}
return oSuma;
}
//función restar
const fRestar = function (valores = "0"){
let arregloNum = valores.split("-");
let oResta = 0;
for (let i = 0; i < arregloNum.length; i++){
oResta = parseInt(arregloNum[i]) - oResta;
}
return oResta;
}
// función multiplicar
const fMultiplicar = function (valores = "0"){
let arregloNum = valores.split("×");
let oMultiplicacion = 1;
for (let i = 0; i < arregloNum.length; i++){
oMultiplicacion = parseInt(arregloNum[i]) * oMultiplicacion;
}
return oMultiplicacion;
}
// función dividir
const fDividir = function (valores = "0"){
let arregloNum = valores.split("/");
let oDivision = 1;
for (let i = 0; i < arregloNum.length; i++){
oDivision = parseInt(arregloNum[i]) / oDivision;
}
return oDivision;
}
// fin de las funciones
console.log(fDividir("10/2")); // 0.2
Change your division function:
// función dividir
const fDividir = function (valores = "0"){
let arregloNum = valores.split("/");
let oDivision = 1;
for (let i = arregloNum.length - 1; i > 0; i--){
oDivision = parseInt(arregloNum[i]) * oDivision;
}
return arregloNum[0] / oDivision;
}
Note that you were inverting the divisors order...
Another, possible solution:
// función dividir
const fDividir = function (valores = "0"){
let arregloNum = valores.split("/");
let oDivision = arregloNum[0];
for (let i = 1; i < arregloNum.length; i++){
oDivision /= parseInt(arregloNum[i]);
}
return oDivision;
}
I created an easy game with Javascript, the purpose is to pick a square who has a RGB color(random generated) from a list of squares and show the length of time until the square with the correct color is found. Everything works fine, except after the time is shown corectly and I click on a button, the time continues to change and show again a new time and i don't want that. I try to give display: none or textcontent ="" after showing the time, but is not working.
The problem is in these lines:
end = new Date().getTime();
timeTaken = (end - start) / 1000;
timp.style.display ="inline";
timp.innerHTML = "Timpul tau: " + timeTaken + " s.";
Thanks!
The game link: https://ionutbedeoan.github.io/joc/
My javascript code :
var numSquares = 6;
var colors = generateRandomColors(numSquares);
var squares = document.querySelectorAll(".square");
var pickedColor = pickColor();
var colorDisplay = document.getElementById("colorDisplay");
var messageDisplay = document.querySelector("#message");
var h1 = document.querySelector("h1");
var resetButton = document.querySelector("#reset");
var easyBtn = document.querySelector("#easyBtn");
var hardBtn = document.querySelector("#hardBtn");
var numSquares = 6;
var timp = document.getElementById("timeTaken");
var start = new Date().getTime();
var end;
var timeTaken;
easyBtn.addEventListener("click", function () {
start = new Date().getTime();
numSquares = 3;
this.classList.add("selected");
hardBtn.classList.remove("selected");
colors = generateRandomColors(numSquares);
pickedColor = pickColor();
colorDisplay.textContent = pickedColor;
for (var i = 0; i < 3; i++) {
squares[i].style.backgroundColor = colors[i];
}
for (var i = 3; i < squares.length; i++) {
squares[i].style.display = "none";
}
timp.textContent= "";
messageDisplay.textContent = "";
h1.style.background = "steelblue";
});
hardBtn.addEventListener("click", function () {
start = new Date().getTime();
this.classList.add("selected");
numSquares = 6;
easyBtn.classList.remove("selected");
colors = generateRandomColors(numSquares);
pickedColor = pickColor();
colorDisplay.textContent = pickedColor;
for (var i = 0; i < squares.length; i++) {
squares[i].style.backgroundColor = colors[i];
squares[i].style.display = "block";
}
messageDisplay.textContent = "";
timp.textContent= "";
h1.style.background = "steelblue";
});
resetButton.addEventListener("click", function () {
start = new Date().getTime();
//generare noi culori
colors = generateRandomColors(numSquares);
//alegem o noua culoare random din vector
pickedColor = pickColor();
//schimbam colorDisplay sa fie la fel cu culoarea aleasa
colorDisplay.textContent = pickedColor;
//schimbare culori ale patratelor
for (var i = 0; i < squares.length; i++) {
// adaug culorile la fiecare patrat
squares[i].style.background = colors[i];
}
h1.style.background = "steelblue";
messageDisplay.textContent = "";
this.textContent = "Culori noi";
timp.textContent= "";
});
colorDisplay.textContent = pickedColor;
for (var i = 0; i < squares.length; i++) {
// adaug culorile la fiecare patrat
squares[i].style.background = colors[i];
//cand dau click pe un patrat
squares[i].addEventListener("click", function () {
// pun intr-o variabila culoarea patratatului ca sa o verific cu culoarea pe care trebuie sa o ghicesc, pentru a vedea daca am nimerit culoarea
var clickedColor = this.style.backgroundColor;
if (clickedColor === pickedColor) {
messageDisplay.textContent = "Corect!"
resetButton.textContent = "Joc nou"
changeColors(clickedColor);
h1.style.backgroundColor = clickedColor;
end = new Date().getTime();
timeTaken = (end - start) / 1000;
timp.style.display ="inline";
timp.innerHTML = "Timpul tau: " + timeTaken + " s.";
} else {
this.style.backgroundColor = "#232323";
messageDisplay.textContent = "Gresit!";
}
});
};
function changeColors(color) {
//schimbam culoarea la fiecare patrat
for (var i = 0; i < squares.length; i++) {
squares[i].style.backgroundColor = color;
}
}
function pickColor() {
var random = Math.floor(Math.random() * colors.length);
return colors[random];
}
function generateRandomColors(num) {
// fac un vector
var arr = [];
//adaugam numarul de culori random
for (var i = 0; i < num; i++) {
//alegem o culoare random si o bagam in vector
arr[i] = randomColor();
}
//returnam vectorul
return arr;
}
function randomColor() {
// alegem un rosu intre 0 si 255
var r = Math.floor(Math.random() * 256);
//alegem un verde intre 0 si 255
var g = Math.floor(Math.random() * 256);
//alegem un albastru intre 0 si 255
var b = Math.floor(Math.random() * 256);
return "rgb(" + r + ", " + g + ", " + b + ")";
}
Thanks !
Check if the correct answer was already selected in your square's event listener and return if so. This will prevent it from running the rest of the event listener's code where it updates the time.
if (clickedColor === pickedColor) {
if (messageDisplay.textContent === "Corect!") return;
...
} else {
...
}
I'm not a programmer, so maybe the info that I looking for in somewhere but I don't know How to search for it.
I have this script to post recent posts by category. It will get the latest published posts from that category forever without ending. If there any solution to make it run once.
Example: If I put this code into a blog post, I want it to post the latest 5 posts only from a specific category, and stop getting any new published posts from that category after that.
I don't know if you understand what I mean or not, but excuse me because I'm not a programmer.
Thank You..
<style>
/*Recent Post By tag*/
img.recent-thumb{float:left;margin-right:10px;height:50px;width:50px;}
.recent-by-tag{width:100%;margin:0;padding:0}
ul.recent-by-tag li{padding:10px 0;margin-bottom:0;margin-right:28px;border-bottom:1px solid #ddd}
.recent-by-tag li{font-size:13px;list-style:none;padding-left:0;margin:0;padding:0;overflow:hidden}
.recent-by-tag a{font-family:'Oswald',sans-serif;font-size:14px;font-weight:normal;margin:0;}
.recent-by-tag strong{padding-left:0}
span.showdates{font-size:14px;margin:10px 0 0}</style>
<div id="rcentDiv">
</div>
<script type='text/javascript'>
var numposts = 10;
var showpostthumbnails = true;
var showpostdate = false;
// Recent Post By Tag
// Recent Post By Tag For Blogger
// insert revised rcentbytag() code here
function rcentbytag(e) {
var storedHTML = "";
function saveHTML(h) {
storedHTML += h;
}
saveHTML('<table><tbody><tr><td><ul class="recent-by-tag">');
for (var t = 0; t < numposts; t++) {
var n = e.feed.entry[t];
var r = n.title.$t;
var i;
if (t == e.feed.entry.length) break;
for (var o = 0; o < n.link.length; o++) {
if (n.link[o].rel == "replies" && n.link[o].type == "text/html") {
var u = n.link[o].title;
var f = n.link[o].href
}
if (n.link[o].rel == "alternate") {
i = n.link[o].href;
break
}
}
var l;
try {
l = n.media$thumbnail.url
} catch (h) {
s = n.content.$t;
a = s.indexOf("<img");
b = s.indexOf('src="', a);
c = s.indexOf('"', b + 5);
d = s.substr(b + 5, c - b - 5);
if (a != -1 && b != -1 && c != -1 && d != "") {
l = d
} else l = "http://2.bp.blogspot.com/-giova1ZCh-A/Uzq6L8QTJNI/AAAAAAAAAJc/USXictTq_xs/s70-c/KM+Icon.png"
}
var p = n.published.$t;
var v = p.substring(0, 4);
var m = p.substring(5, 7);
var g = p.substring(8, 10);
var y = new Array;
y[1] = "January";
y[2] = "February";
y[3] = "March";
y[4] = "April";
y[5] = "May";
y[6] = "June";
y[7] = "July";
y[8] = "August";
y[9] = "September";
y[10] = "October";
y[11] = "November";
y[12] = "December";
saveHTML('<li class="clear">');
if (showpostthumbnails == true) saveHTML('<img class="recent-thumb" alt="' + r + '" src="' + l + '"/>');
saveHTML('<strong>' + r + '</strong>');
saveHTML('<br>');
var x = "";
var T = 0;
if (showpostdate == true) {
x = '<span class="showdates">' + x + g + " " + y[parseInt(m, 10)] + " " + v + "</span>";
T = 1
}
saveHTML(x);
saveHTML("</li>");
if (t != numposts - 1) saveHTML("")
}
saveHTML("</ul></td></tr></tbody></table>")
document.getElementById("rcentDiv").innerHTML = storedHTML;
}
</script>
<script async="async" src="https://www.example.com/feeds/posts/default/-/CategoryName?orderby=published&alt=json-in-script&callback=rcentbytag" return false;"></script>
Replace all of your code with this, let me know if it works; after changing the page make sure you hard refresh the page (ctrl + F5 in google chrome) to get the new content. Note that there are a lot of bad practices in the code you provided and that I've only slightly edited here; if this is for a production website I highly recommend hiring a developer who is familiar with modern javascript techniques like ES6 modules.
<style>
/*Recent Post By tag*/
img.recent-thumb{float:left;margin-right:10px;height:50px;width:50px;}
.recent-by-tag{width:100%;margin:0;padding:0}
ul.recent-by-tag li{padding:10px 0;margin-bottom:0;margin-right:28px;border-bottom:1px solid #ddd}
.recent-by-tag li{font-size:13px;list-style:none;padding-left:0;margin:0;padding:0;overflow:hidden}
.recent-by-tag a{font-family:'Oswald',sans-serif;font-size:14px;font-weight:normal;margin:0;}
.recent-by-tag strong{padding-left:0}
span.showdates{font-size:14px;margin:10px 0 0}</style>
<div id="rcentDiv">
</div>
<script type='text/javascript'>
var limitreached = false;
var maxposts = 5;
var numposted = 0;
var showpostthumbnails = true;
var showpostdate = false;
// Recent Post By Tag
// Recent Post By Tag For Blogger
// insert revised rcentbytag() code here
function rcentbytag(e) {
if (limitreached === false) {
var storedHTML = "";
function saveHTML(h) {
storedHTML += h;
}
saveHTML('<table><tbody><tr><td><ul class="recent-by-tag">');
for (var t = 0; t < maxposts; t++) {
numposted = numposted + 1;
if (numposted > maxposts) {
limitreached = true;
}
if (limitreached === false) {
var n = e.feed.entry[t];
var r = n.title.$t;
var i;
if (t == e.feed.entry.length) break;
for (var o = 0; o < n.link.length; o++) {
if (n.link[o].rel == "replies" && n.link[o].type == "text/html") {
var u = n.link[o].title;
var f = n.link[o].href
}
if (n.link[o].rel == "alternate") {
i = n.link[o].href;
break
}
}
var l;
try {
l = n.media$thumbnail.url
} catch (h) {
s = n.content.$t;
a = s.indexOf("<img");
b = s.indexOf('src="', a);
c = s.indexOf('"', b + 5);
d = s.substr(b + 5, c - b - 5);
if (a != -1 && b != -1 && c != -1 && d != "") {
l = d
} else l = "http://2.bp.blogspot.com/-giova1ZCh-A/Uzq6L8QTJNI/AAAAAAAAAJc/USXictTq_xs/s70-c/KM+Icon.png"
}
var p = n.published.$t;
var v = p.substring(0, 4);
var m = p.substring(5, 7);
var g = p.substring(8, 10);
var y = new Array;
y[1] = "January";
y[2] = "February";
y[3] = "March";
y[4] = "April";
y[5] = "May";
y[6] = "June";
y[7] = "July";
y[8] = "August";
y[9] = "September";
y[10] = "October";
y[11] = "November";
y[12] = "December";
saveHTML('<li class="clear">');
if (showpostthumbnails == true) {
saveHTML('<img class="recent-thumb" alt="' + r + '" src="' + l + '"/>');
}
saveHTML('<strong>' + r + '</strong>');
saveHTML('<br>');
var x = "";
var T = 0;
if (showpostdate == true) {
x = '<span class="showdates">' + x + g + " " + y[parseInt(m, 10)] + " " + v + "</span>";
T = 1
}
saveHTML(x);
saveHTML("</li>");
if (t != maxposts - 1) {
saveHTML("");
}
}
}
saveHTML("</ul></td></tr></tbody></table>");
document.getElementById("rcentDiv").innerHTML = storedHTML;
}
}
</script>
<script async="async" src="https://www.example.com/feeds/posts/default/-/CategoryName?orderby=published&alt=json-in-script&callback=rcentbytag" return false;"></script>
Use a boolean (yes/no value) to check if the function has been ran already.
var CodeRan;
function YourFunction(){
if(!CodeRan){ // If CodeRan is false...
// Set CodeRan to true, so the next time the function is called it wont get past the If statement.
CodeRan = true;
// Your code goes here.
} // End of IF statement.
} // End of yourFunction
Possible duplicate of this:
Javascript running code once
Where is the error? I want to convert a binary number to ASCII-text. The result is not so good. Or is this code correct?
For example:
01010111011010010111001000100000011000100110 01010110011101101001011011100110111001100101 01101110001000000110110101101001011101000010 00000110010101110100011101110110000101110011 00100000011001010110100101101110011001100110 00010110001101101000011001010110110100101110 00100000010101110110010101101110011011100010 00000110010001110101001000000110010001101001 01100101011100110010000001101100011001010111 00110110010101101110001000000110101101100001 01101110011011100111001101110100001011000010 00000110100001100001011100110111010000100000 01100100011101010010000001110011011000110110 10000110111101101110001000000110100001100101 01110010011000010111010101110011011001110110 01010110011001110101011011100110010001100101 01101110001011000010000001100100011000010111 00110111001100100000011001000110100101100101 01110011011001010111001000100000010101000110 01010111100001110100011000100110110001101111 01100011011010110010000001100101011010010110 11100110010100100000011000100110100101101110 01100001011001010111001001100101001000000100 01000110000101110010011100110111010001100101 01101100011011000111010101101110011001110010 00000110010001100101011100110010000001010100 01100101011110000111010001100101011100110010 00000110100101110011011101000010110000100000 01100100011001010110111000100000011001000111 01010010000001100111011001010111001001100001 01100100011001010010000001101100011010010110 01010111001101110100001011100010000001000010 01101001011101000111010001100101001000000111 10100110000101100101011010000110110001100101 00100000011001000110100101100101001000000101 01110100111101000101010100100101010001000101 01010010001000000110010001101001011001010111 00110110010101110011001000000100001001101100 01101111011000110110101101110011001011000010 00000111101001101001011001010110100001100101 00100000001100010011001100100000011001000110 00010111011001101111011011100010000001100001 01100010001000000111010101101110011001000010 00000110111001101111011101000110100101100101 01110010011001010010000001100100011000010111 00110010000001000101011100100110011101100101 01100010011011100110100101110011001000000110 00010110110001110011001000000101101001100001 01101000011011000010000001000001
The result is:
®Òä#Äginne76´º]Ø\-Ìf6VÒ»+sqÔÊæ#ØÊen ka779º\ݤl6öâ«;Õ¹ÜX#ÈÂs die9²¹*^mdæR&+)ÉÍÑØØêÜÎdes T2¼:2¹\ÝÄGRvW&#)cKÍиÒèèÊ#aehle24²ÓÑTD,W6W2&{[a饡#bf#Èvon a1:·2ÝYL¤2W&vsK±ÌiÐØ#
Code:
function bin(text){
var bins = new Array();
var byte = "";
var chary = "";
for(var i = 0; i<text.length; i++){
byte += text.charAt(i);
if(i%bits==0){
bins.push(byte);
byte = "";
}
}
for(var i = 0; i<bins.length; i++){
var binary = bins[i];
var decimal = 0;
var index = 0;
while(binaerzahl > 0){
rest = binary % 2;
binary = Math.floor(binary / 10);
decimal += rest * Math.pow(2, index);
index++;
}
chary += String.fromCharCode(decimal);
}
return chary;
}
function bin (text) {
var res = '', j;
for (var i = 0 ; i < text.length; i = j) {
j += 8; // specify radix--v
res += String.fromCharCode( parseInt( text.slice( i, j ), 2 ) );
}
return res;
}
// remove spaces from the string
var str = '01010111011010010111001000100000011000100110010101100111011010010110111001101110011001010110111000100000011011010110100101110100001000000110010101110100011101110110000101110011001000000110010101101001011011100110011001100001011000110110100001100101011011010010111000100000010101110110010101101110011011100010000001100100011101010010000001100100011010010110010101110011001000000110110001100101011100110110010101101110001000000110101101100001011011100110111001110011011101000010110000100000011010000110000101110011011101000010000001100100011101010010000001110011011000110110100001101111011011100010000001101000011001010111001001100001011101010111001101100111011001010110011001110101011011100110010001100101011011100010110000100000011001000110000101110011011100110010000001100100011010010110010101110011011001010111001000100000010101000110010101111000011101000110001001101100011011110110001101101011001000000110010101101001011011100110010100100000011000100110100101101110011000010110010101110010011001010010000001000100011000010111001001110011011101000110010101101100011011000111010101101110011001110010000001100100011001010111001100100000010101000110010101111000011101000110010101110011001000000110100101110011011101000010110000100000011001000110010101101110001000000110010001110101001000000110011101100101011100100110000101100100011001010010000001101100011010010110010101110011011101000010111000100000010000100110100101110100011101000110010100100000011110100110000101100101011010000110110001100101001000000110010001101001011001010010000001010111010011110100010101010010010101000100010101010010001000000110010001101001011001010111001101100101011100110010000001000010011011000110111101100011011010110111001100101100001000000111101001101001011001010110100001100101001000000011000100110011001000000110010001100001011101100110111101101110001000000110000101100010001000000111010101101110011001000010000001101110011011110111010001101001011001010111001001100101001000000110010001100001011100110010000001000101011100100110011101100101011000100110111001101001011100110010000001100001011011000111001100100000010110100110000101101000011011000010000001000001';
console.log(bin(str));
Result:
Wir beginnen mit etwas einfachem. Wenn du dies lesen kannst, hast du schon herausgefunden, dass dieser Textblock eine binaere Darstellung des Textes ist, den du gerade liest. Bitte zaehle die WOERTER dieses Blocks, ziehe 13 davon ab und notiere das Ergebnis als Zahl A
JSFiddle
Alternative:
var res = str.match(/[01]{8}/g).map(function(v) {
return String.fromCharCode( parseInt(v,2) );
}).join('');
ht
tp://jsfiddle.net/Wy93E/1/
Alternative 2:
var res = str.replace(/[01]{8}/g, function(v) {
return String.fromCharCode( parseInt(v,2) );
});
ht
tp://jsfiddle.net/Wy93E/2/
Alternative 3:
var res = String.fromCharCode.apply(null, str.match(/[01]{8}/g).map(function(v) {
return parseInt(v,2)
}));
ht
tp://jsfiddle.net/Wy93E/4/
If you just want to convert binary string to ASCII letters, this kind of simple function does the trick.
function bin (text) {
var output = ''
for (var i = 0 ; i < text.length; i+= 8) {
var c = 0;
for (var j=0; j < 8 ; j++) {
c <<= 1;
c |= parseInt(text[i + j]);
}
output += String.fromCharCode(c);
}
return output;
}
The problem is the spaces in the string:
var asd = "01010111011010010111001000100000011000100110 01010110011101101001011011100110111001100101 01101110001000000110110101101001011101000010 00000110010101110100011101110110000101110011 00100000011001010110100101101110011001100110 00010110001101101000011001010110110100101110 00100000010101110110010101101110011011100010 00000110010001110101001000000110010001101001 01100101011100110010000001101100011001010111 00110110010101101110001000000110101101100001 01101110011011100111001101110100001011000010 00000110100001100001011100110111010000100000 01100100011101010010000001110011011000110110 10000110111101101110001000000110100001100101 01110010011000010111010101110011011001110110 01010110011001110101011011100110010001100101 01101110001011000010000001100100011000010111 00110111001100100000011001000110100101100101 01110011011001010111001000100000010101000110 01010111100001110100011000100110110001101111 01100011011010110010000001100101011010010110 11100110010100100000011000100110100101101110 01100001011001010111001001100101001000000100 01000110000101110010011100110111010001100101 01101100011011000111010101101110011001110010 00000110010001100101011100110010000001010100 01100101011110000111010001100101011100110010 00000110100101110011011101000010110000100000 01100100011001010110111000100000011001000111 01010010000001100111011001010111001001100001 01100100011001010010000001101100011010010110 01010111001101110100001011100010000001000010 01101001011101000111010001100101001000000111 10100110000101100101011010000110110001100101 00100000011001000110100101100101001000000101 01110100111101000101010100100101010001000101 01010010001000000110010001101001011001010111 00110110010101110011001000000100001001101100 01101111011000110110101101110011001011000010 00000111101001101001011001010110100001100101 00100000001100010011001100100000011001000110 00010111011001101111011011100010000001100001 01100010001000000111010101101110011001000010 00000110111001101111011101000110100101100101 01110010011001010010000001100100011000010111 00110010000001000101011100100110011101100101 01100010011011100110100101110011001000000110 00010110110001110011001000000101101001100001 01101000011011000010000001000001";
var letters = [], txt;
asd = asd.replace( /\s+/g, "" );
for( var i = 0, l = asd.length; i < l; i += 8 ) {
txt = "";
for( var j = 0; j <= 7; ++j ) {
txt += asd[j+i];
}
letters.push( String.fromCharCode( parseInt( txt, 2 ) ) );
}
letters.join( "" );
//"Wir beginnen mit etwas einfachem. Wenn du dies lesen kannst, hast du schon herausgefunden, dass dieser Textblock eine binaere Darstellung des Textes ist, den du gerade liest. Bitte zaehle die WOERTER dieses Blocks, ziehe 13 davon ab und notiere das Ergebnis als Zahl A"
Disclaimer: string bracket notation doesn't work in older browsers