Converting text to small number javascript - javascript

I am trying to make a real time chat system. I am sending messages and saving them to the database. But before saving the message into database i need to encrypt it with using NTRU for Integers algorithm. In order to use this algorithm i have to convert text into numbers. I already tried to convert to ASCII code but its creating too big number for algorithm. Is there any way to convert text into small numbers? Saving into database with parent.send_message(chat_input.value)
create_chat(){
var parent = this;
var title_container = document.getElementById('title_container')
var title = document.getElementById('title')
title_container.classList.add('chat_title_container')
title.classList.add('chat_title')
var chat_container = document.createElement('div')
chat_container.setAttribute('id', 'chat_container')
var chat_inner_container = document.createElement('div')
chat_inner_container.setAttribute('id', 'chat_inner_container')
var chat_content_container = document.createElement('div')
chat_content_container.setAttribute('id', 'chat_content_container')
var chat_input_container = document.createElement('div')
chat_input_container.setAttribute('id', 'chat_input_container')
var chat_input_send = document.createElement('button')
chat_input_send.setAttribute('id', 'chat_input_send')
chat_input_send.setAttribute('disabled', true)
chat_input_send.innerHTML = `<i class="far fa-paper-plane"></i>`
var chat_input = document.createElement('input')
chat_input.setAttribute('id', 'chat_input')
chat_input.setAttribute('maxlength', 11)
chat_input.placeholder = `${parent.get_name()}. Say hello..`
chat_input.onkeyup = function(){
if(chat_input.value.length > 0){
chat_input_send.removeAttribute('disabled')
chat_input_send.classList.add('enabled')
chat_input_send.onclick = function(){
chat_input_send.setAttribute('disabled', true)
chat_input_send.classList.remove('enabled')
if(chat_input.value.length <= 0){
return
}
parent.create_load('chat_content_container')
chat_input.value = chat_input.value.toLocaleUpperCase()
parent.send_message(chat_input.value)
chat_input.value = ''
chat_input.focus()
}

I was able to solve my problem with this code
// Vocabulary
let vocabulary = new Array();{
vocabulary[10] = "A";
vocabulary[11] = "B";
vocabulary[12] = "C";
vocabulary[13] = "D";
vocabulary[14] = "E";
vocabulary[15] = "F";
vocabulary[16] = "G";
vocabulary[17] = "H";
vocabulary[18] = "I";
vocabulary[19] = "J";
vocabulary[20] = "K";
vocabulary[21] = "L";
vocabulary[22] = "M";
vocabulary[23] = "N";
vocabulary[24] = "O";
vocabulary[25] = "P";
vocabulary[26] = "Q";
vocabulary[27] = "R";
vocabulary[28] = "S";
vocabulary[29] = "T";
vocabulary[30] = "U";
vocabulary[31] = "V";
vocabulary[32] = "W";
vocabulary[33] = "X";
vocabulary[34] = "Y";
vocabulary[35] = "Z";
vocabulary[36] = " ";
vocabulary[37] = "a";
vocabulary[38] = "b";
vocabulary[39] = "c";
vocabulary[40] = "d";
vocabulary[41] = "e";
vocabulary[42] = "f";
vocabulary[43] = "g";
vocabulary[44] = "h";
vocabulary[45] = "i";
vocabulary[46] = "j";
vocabulary[47] = "k";
vocabulary[48] = "l";
vocabulary[49] = "m";
vocabulary[50] = "n";
vocabulary[51] = "o";
vocabulary[52] = "p";
vocabulary[53] = "q";
vocabulary[54] = "r";
vocabulary[55] = "s";
vocabulary[56] = "t";
vocabulary[57] = "u";
vocabulary[58] = "v";
vocabulary[59] = "w";
vocabulary[60] = "x";
vocabulary[61] = "y";
vocabulary[62] = "z";
vocabulary[63] = "!";
}
// Converting Text into numbers
var number = "";
for(var z = 0 ; z < chat_input.value.length ; z++)
{
for(var i = 10 ; i < 64 ; i++) {
if(chat_input.value.charAt(z) == vocabulary[i]) {
var test = i;
test = test.toString();
number = number + test;
}
}
}
// Seperating numbers into array
var numbers = new Array() ;
var index = 0 ;
while(number.length !== 0) {
if (number.length == 3) {
numbers[index] = number.substring(0, 3);
number = number.substring(3,number.length);
} else if (number.length == 2) {
numbers[index] = number.substring(0, 2);
number = number.substring(2,number.length);
} else if (number.length == 1) {
numbers[index] = number.substring(0, 1);
number = number.substring(1,number.length);
} else
numbers[index] = number.substring(0, 4);
number = number.substring(4,number.length);
index++;
}
//Encrpyting numbers and getting cipher text
var indexofe = 0 ;
var ciphertextz = new Array();
while(indexofe !== numbers.length) {
ciphertextz[indexofe] = parent.Encrypt(numbers[indexofe]);
indexofe++;
}

Related

JavaScript Functions: Grade Distributions

I was wondering if someone could help me understand what I am doing wrong? Below I have posted the assignment question, my code, and then the result I am getting.
Assignment
Here is my code:
function parseScores(scoresString) {
// TODO: Compete the function
let inString = scoresString.split();
return inString;
}
function buildDistributionArray(scoresArray) {
// TODO: Compete the function
let x = 0;
let distributeArray = new Array(5);
distributeArray[0] = 0;
distributeArray1 = 0;
distributeArray2 = 0;
distributeArray3 = 0;
distributeArray[4] = 0;
for(i = 0; i < scoresArray.length; i++){
if(scoresArray[i] >= 90){
distributeArray[0]++;
}
else if(scoresArray[i] >= 80 && scoresArray[i] <= 89){
distributeArray[1]++;
}
else if (scoresArray[i] >= 70 && scoresArray[i] <= 79) {
distributeArray[2]++;
}
else if(scoresArray[i] >= 60 && scoresArray[i] <= 69){
distributeArray[3]++;
}
else if(scoresArray[i] <= 59 && scoresArray[i] >= 0) {
distributeArray[4]++;
}
return distributeArray;
}
}
function setTableContent(userInput) {
// TODO: Compete the function
let myTable = document.getElementById("distributionTable");
if(userInput.length > 0) {
let parsedScores = parseScores(userInput);
let buildArray = buildDistributionArray(parsedScores);
let row = myTable.insertRow(0);
let row2 = myTable.insertRow(1);
let row3 = myTable.insertRow(2);
let cell1 = row2.insertCell(0);
let cell2 = row2.insertCell(1);
let cell3 = row2.insertCell(2);
let cell4 = row2.insertCell(3);
let cell5 = row2.insertCell(4);
cell1.innerHTML = "A";
cell2.innerHTML = "B";
cell3.innerHTML = "C";
cell4.innerHTML = "D";
cell5.innerHTML = "F";
let graphValueArray = [];
let occuranceArray = [];
for(index = 0; index < 5; index++){
occuranceArray[index] = row3.insertCell(index);
occuranceArray[index].innerHTML = buildArray[index];
graphValueArray[index] = row.insertCell(index);
let styleClass = "bar" + index;
let heightValue = (buildArray[index] * 10) + "px";
graphValueArray[index].innerHTML = "<div style = 'height" + heightValue + "class = " + styleClass + "></div>";
}
}
else {
let emptyRow = myTable.insertRow(0);
let emptyCell = emptyRow.insertCell(0);
emptyCell.innerHTML = "No graph to display";
}
}
// The argument can be changed for testing purposes
setTableContent("45 78 98 83 86 99 90 59");
my results:
Result
Here is my HTML
HTML
The problem is where you do .split(). You have to split it on space, so .split(' ')
Another tip, you don't need to include the upper limit in the if statements. The previous if statement takes care of it.
See if this works:
function parseScores(scoresString) {
// TODO: Complete the function
let inString = scoresString.split(' ');
return inString;
}
function buildDistributionArray(scoresArray) {
// TODO: Compete the function
let x = 0;
let distributeArray = new Array();
distributeArray[0] = 0;
distributeArray[1] = 0;
distributeArray[2] = 0;
distributeArray[3] = 0;
distributeArray[4] = 0;
for(i = 0; i < scoresArray.length; i++){
if (scoresArray[i] >= 90){
distributeArray[0]++;
}
else if (scoresArray[i] >= 80){
distributeArray[1]++;
}
else if (scoresArray[i] >= 70) {
distributeArray[2]++;
}
else if(scoresArray[i] >= 60){
distributeArray[3]++;
}
else if(scoresArray[i] <= 59 && scoresArray[i] >= 0) {
distributeArray[4]++;
}
return distributeArray;
}
}
function setTableContent(userInput) {
// TODO: Compete the function
let myTable = document.getElementById("distributionTable");
if(userInput.length > 0) {
let parsedScores = parseScores(userInput);
let buildArray = buildDistributionArray(parsedScores);
let row = myTable.insertRow(0);
let row2 = myTable.insertRow(1);
let row3 = myTable.insertRow(2);
let cell1 = row2.insertCell(0);
let cell2 = row2.insertCell(1);
let cell3 = row2.insertCell(2);
let cell4 = row2.insertCell(3);
let cell5 = row2.insertCell(4);
cell1.innerHTML = "A";
cell2.innerHTML = "B";
cell3.innerHTML = "C";
cell4.innerHTML = "D";
cell5.innerHTML = "F";
let graphValueArray = [];
let occuranceArray = [];
for(index = 0; index < 5; index++){
occuranceArray[index] = row3.insertCell(index);
occuranceArray[index].innerHTML = buildArray[index];
graphValueArray[index] = row.insertCell(index);
let styleClass = "bar" + index;
let heightValue = (buildArray[index] * 10) + "px";
graphValueArray[index].innerHTML = "<div style = 'height" + heightValue + "class = " + styleClass + "></div>";
}
}
else {
let emptyRow = myTable.insertRow(0);
let emptyCell = emptyRow.insertCell(0);
emptyCell.innerHTML = "No graph to display";
}
}
// The argument can be changed for testing purposes
setTableContent("45 78 98 83 86 99 90 59");

Nested if else statements only works on first statement

I am having an issue with nested if else statements. In this issue, it only executes the first if statement and skips all of the rest, when I want every statement to be executed. The else if statement works fine when there is nothing nested inside it, but when I nest additional if else statements only the first one seems to work. Here is my javascript code:
const apiURL2 = "https://api.openweathermap.org/data/2.5/forecast?id=5604473&appid=6d1d830097a2c0bac1aba2337d0139e6";
fetch(apiURL2).then((response) => response.json()).then((jsonObject) => {
const list = jsonObject['list'];
console.log(jsonObject);
for ( let i = 0; i < 5; i++){
let divForecastData = document.querySelector('div.weather-forecast-wrapper');
let temp1 = document.createElement("div");
let tempday = document.createElement("p");
let temperature = document.createElement("p");
let icon = document.createElement("i");
temperature.className = "temp";
tempday.className = "weekday";
temp1.className = "day";
if (i == 0){
tempday.textContent = "Sat";
temperature.textContent = list[i].main.temp;
if (list[i].weather[i].main = "Clear"){
icon.className = "worked"
}
else {
icon.className = " still worked"
}
}
else if (i == 1){
tempday.textContent = "Sun";
var far = list[i].main.temp
var kel = far * (9/5) - 459.67;
temperature.textContent = Math.round(kel) + "℉";
if (list[i].weather[i].main = "Clear"){
icon.className = "worked"
}
else {
icon.className = " still worked"
}
}
else if (i == 2){
tempday.textContent = "Mon";
temperature.textContent = list[i].main.temp;
}
else if (i == 3){
tempday.textContent = "Wed";
temperature.textContent = list[i].main.temp;
}
else{
tempday.textContent = "Thu";
temperature.textContent = list[i].main.temp;
}
divForecastData.appendChild(temp1);
temp1.appendChild(tempday);
temp1.appendChild(icon);
temp1.appendChild(temperature);
}
});
any suggestions?
Comparaison are coded with == or ===...
So this is not good.
if (list[i].weather[i].main = "Clear")
But list[i].weather[i] something doesn't exists, I know because it's there is a big red message that appears on the console just by running the code. You maybe wanted to use list[i].weather[0].
Here is a corrected code snippet
const apiURL2 = "https://api.openweathermap.org/data/2.5/forecast?id=5604473&appid=6d1d830097a2c0bac1aba2337d0139e6";
fetch(apiURL2).then((response) => response.json())
.then((jsonObject) => {
let divForecastData = document.querySelector('div.weather-forecast-wrapper');
console.log(jsonObject);
const list = jsonObject['list'];
for ( let i = 0; i < 5; i++){
const item = list[i];
let temp1 = document.createElement("div");
let tempday = document.createElement("p");
let temperature = document.createElement("p");
let icon = document.createElement("i");
temperature.className = "temp";
tempday.className = "weekday";
temp1.className = "day";
if (i == 0){
tempday.textContent = "Sat";
temperature.textContent = item.main.temp;
if (item.weather[i].main = "Clear"){
icon.className = "worked"
} else {
icon.className = " still worked"
}
} else if (i == 1){
tempday.textContent = "Sun";
var far = item.main.temp
var kel = far * (9/5) - 459.67;
temperature.textContent = Math.round(kel) + "℉";
if (item.weather[0].main === "Clear"){
icon.className = "worked"
} else {
icon.className = " still worked"
}
} else if (i == 2){
tempday.textContent = "Mon";
temperature.textContent = item.main.temp;
} else if (i == 3){
tempday.textContent = "Wed";
temperature.textContent = item.main.temp;
} else{
tempday.textContent = "Thu";
temperature.textContent = item.main.temp;
}
divForecastData.appendChild(temp1);
temp1.appendChild(tempday);
temp1.appendChild(icon);
temp1.appendChild(temperature);
}
});
<div class='weather-forecast-wrapper'></div>

Javascript Output is giving empty response

I'm making a binary calculator. Following is my javascript code:
// let operator = document.getElementsByClassName('operator');
let plus = document.getElementById('btnSum');
let minus = document.getElementById('btnSub');
let into = document.getElementById('btnMul');
let divide = document.getElementById('btnDiv');
let resultBox = document.getElementById('res');
var val1;
var val2;
var clicked = false;
plus.addEventListener("click", function(){
clicked = true;
let middleOperator = '+';
resultBox.innerHTML += ' + ';
});
minus.addEventListener("click", function(){
clicked = true;
let middleOperator = '-';
resultBox.innerHTML += ' - ';
});
into.addEventListener("click", function(){
clicked = true;
let middleOperator = '*';
resultBox.innerHTML += ' * ';
});
divide.addEventListener("click", function(){
clicked = true;
let middleOperator = '/';
resultBox.innerHTML += '/';
});
function pushZero(){
if(clicked){
//if middleOperator is pressed
resultBox.innerHTML += '0';
val2 = resultBox.innerText;
}else{
//if middleOperator is not pressed
resultBox.innerHTML += '0';
val1 = resultBox.innerText;
}
}
function pushOne(){
if(clicked){
//if middleOperator is pressed
resultBox.innerHTML += '1';
val2 = resultBox.innerText;
}else{
//if middleOperator is not pressed
resultBox.innerHTML += '1';
val1 = resultBox.innerText;
}
}
//passing array to this function
function binaryToNum(a){
let num = 0;
for(var i=0; i<a.length; i++){
num += a[i]*Math.pow(2, a.length-i-1);
}
return num;
}
//Function to convert a number to binary
function numToBinary(x){
var s='';
let n;
while(x>=1){
n = x%2;
s += `${n}`;
x /= 2;
}
return s;
}
function result(middleOperator){
var ans;
var num1 = binaryToNum(val1);
var num2 = binaryToNum(val2);
if(middleOperator == '+'){
ans = num1 + num2;
}
if(middleOperator == '-'){
ans = num1 - num2;
}
if(middleOperator == '*'){
ans = num1*num2;
}
if(middleOperator == '/'){
ans = num1/num2;
}
var output = numToBinary(ans);
console.log(output);
resultBox.innerHTML = output;
}
When I'm trying to display the result it shows empty output.
While assigning any random string to the output displays the same on the screen.(Thus there is no issue on Html or Dom side).
Also when I'm console.log() gives an empty output. How to solve this?

Javascript Check variable.Then gain ++ per second

I have a problem i want to check a variable.If its 0 then gain ++ after 1.5s.If its 10 then gain ++ after .4s.Its complicated.It doesnt really work.My code so far:
if(road == 1){setInterval(function(){stamina = stamina+1;document.getElementById("stamina").innerHTML = stamina;},1400);}
else if(road == 2){setInterval(function(){stamina = stamina+1;document.getElementById("stamina").innerHTML = stamina;},1300);}
else if(road == 3){setInterval(function(){stamina = stamina+1;document.getElementById("stamina").innerHTML = stamina;},1200);}
else if(road == 4){setInterval(function(){stamina = stamina+1;document.getElementById("stamina").innerHTML = stamina;},1100);}
else if(road == 5){setInterval(function(){stamina = stamina+1;document.getElementById("stamina").innerHTML = stamina;},1000);}
else if(road == 6){setInterval(function(){stamina = stamina+1;document.getElementById("stamina").innerHTML = stamina;},900);}
else if(road == 7){setInterval(function(){stamina = stamina+1;document.getElementById("stamina").innerHTML = stamina;},800);}
else if(road == 8){setInterval(function(){stamina = stamina+1;document.getElementById("stamina").innerHTML = stamina;},600);}
else if(road == 9){setInterval(function(){stamina = stamina+1;document.getElementById("stamina").innerHTML = stamina;},400);}
else if(road == 10){setInterval(function(){stamina = stamina+1;document.getElementById("stamina").innerHTML = stamina;},200);}
else{setInterval(function(){stamina++;document.getElementById("stamina").innerHTML = stamina;},1500);}
And the code to build a road is this:
function build_road() {
if ((wood + tavern) >= 29 && stone > 4 && road < 10) {
road++;
document.getElementById("road_count").innerHTML = road;
wood = (wood + tavern) - 20;
stone = stone - 5;
document.getElementById("wood").innerHTML = wood;
document.getElementById("stone").innerHTML = stone;
exp = exp + 20;
var x = document.getElementById("PROGRESS");
x.setAttribute("value", exp);
x.setAttribute("max", max);
if (exp == 100) {
exp = 0;
level++;
document.getElementById("level").innerHTML = level;
}
alert("Congratulations,You've create a Road,Now you gain stamina slightly faster.");
}
else {
alert("You need: 30Wood,5Stone .Maximum 10 Roads.")
}
}
Make reusable functions (it's often a good practice, when you a difficulties with a piece of code, to break it into small functions):
var staminaIncreaseTimer = null;
function configureStaminaIncrease(delay) {
if (staminaIncreaseTimer !== null)
clearInterval(staminaIncreaseTimer);
staminaIncreaseTimer = setInterval(function () {
increaseStamina();
}, delay);
}
function increaseStamina() {
stamina += 1;
document.getElementById("stamina").innerHTML = stamina;
}
Solution with an array (suggested by Jay Harris)
var roadIndex = road-1;
var ROAD_DELAYS = [1400, 1300, 1200, /*...*/];
var DEFAULT_DELAY = 1500;
if (roadIndex < ROAD_DELAYS.length) {
configureStaminaIncrease(ROAD_DELAYS[roadIndex]);
} else {
configureStaminaIncrease(DEFAULT_DELAY);
}
Solution with a switch instead of you if-elseif mess:
switch (road) {
case 1:
configureStaminaIncrease(1400);
break;
case 2:
configureStaminaIncrease(1300);
break;
case 3:
configureStaminaIncrease(1200);
break;
//and so on...
default:
configureStaminaIncrease(1500);
}

How to create substrings, replacing substrings?

With the following code I turn a string:
1Z3245799938735257
into:
1Z›324579š99›38735257
But now, I want to split the areas, between "›" and "š" and between "›" and the end of that string, into pairs.
So that the sub result is:
1Z›32,45,79š99›38,73,52,57
Now I want to change the pairs into String.fromCharCode(pair+32)
So that the sub result is:
1Z›#,M,oš99›F,i,T,Y
Replacing the "," with ""
So that the ultimate result is
1Z›#Moš99›FiTY
How can I do this?
var chars = new Array();
chars["00"] = " "; chars["01"] = "!"; chars["02"] = "“"; chars["03"] = "#"; chars["04"] = "$";
chars["05"] = "%"; chars["06"] = "&"; chars["07"] = "’"; chars["08"] = "("; chars["09"] = ")";
chars["10"] = "*"; chars["11"] = "+"; chars["12"] = ","; chars["13"] = "-"; chars["14"] = ".";
chars["15"] = "/"; chars["16"] = "0"; chars["17"] = "1"; chars["18"] = "2"; chars["19"] = "3";
chars["20"] = "4"; chars["21"] = "5"; chars["22"] = "6"; chars["23"] = "7"; chars["24"] = "8";
chars["25"] = "9"; chars["26"] = ":"; chars["27"] = ";"; chars["28"] = "<"; chars["29"] = "=";
chars["30"] = ">"; chars["31"] = "?"; chars["32"] = "#"; chars["33"] = "A"; chars["34"] = "B";
chars["35"] = "C"; chars["36"] = "D"; chars["37"] = "E"; chars["38"] = "F"; chars["39"] = "G";
chars["40"] = "H"; chars["41"] = "I"; chars["42"] = "J"; chars["43"] = "K"; chars["44"] = "L";
chars["45"] = "M"; chars["46"] = "N"; chars["47"] = "O"; chars["48"] = "P"; chars["49"] = "Q";
chars["50"] = "R"; chars["51"] = "S"; chars["52"] = "T"; chars["53"] = "U"; chars["54"] = "V";
chars["55"] = "W"; chars["56"] = "X"; chars["57"] = "Y"; chars["58"] = "Z"; chars["59"] = "[";
chars["60"] = "\\"; chars["61"] = "]"; chars["62"] = "^"; chars["63"] = "_"; chars["64"] = "`";
chars["65"] = "a"; chars["66"] = "b"; chars["67"] = "c"; chars["68"] = "d"; chars["69"] = "e";
chars["70"] = "f"; chars["71"] = "g"; chars["72"] = "h"; chars["73"] = "i"; chars["74"] = "j";
chars["75"] = "k"; chars["76"] = "l"; chars["77"] = "m"; chars["78"] = "n"; chars["79"] = "o";
chars["80"] = "p"; chars["81"] = "q"; chars["82"] = "r"; chars["83"] = "s"; chars["84"] = "t";
chars["85"] = "u"; chars["86"] = "v"; chars["87"] = "w"; chars["88"] = "x"; chars["89"] = "y";
chars["90"] = "z"; chars["91"] = "{"; chars["92"] = "|"; chars["93"] = "}"; chars["94"] = "~";
var charsv = new Array();
charsv[" "] = 128; charsv["!"] = 01; charsv['"'] = 02; charsv["#"] = 03; charsv["$"] = 04;
charsv["%"] = 05; charsv["&"] = 06; charsv["’"] = 07; charsv["("] = 08; charsv[")"] = 09;
charsv["*"] = 10; charsv["+"] = 11; charsv[","] = 12; charsv["-"] = 13; charsv["."] = 14;
charsv["/"] = 15; charsv["0"] = 16; charsv["1"] = 17; charsv["2"] = 18; charsv["3"] = 19;
charsv["4"] = 20; charsv["5"] = 21; charsv["6"] = 22; charsv["7"] = 23; charsv["8"] = 24;
charsv["9"] = 25; charsv[":"] = 26; charsv[";"] = 27; charsv["<"] = 28; charsv["="] = 29;
charsv[">"] = 30; charsv["?"] = 31; charsv["#"] = 32; charsv["A"] = 33; charsv["B"] = 34;
charsv["C"] = 35; charsv["D"] = 36; charsv["E"] = 37; charsv["F"] = 38; charsv["G"] = 39;
charsv["H"] = 40; charsv["I"] = 41; charsv["J"] = 42; charsv["K"] = 43; charsv["L"] = 44;
charsv["M"] = 45; charsv["N"] = 46; charsv["O"] = 47; charsv["P"] = 48; charsv["Q"] = 49;
charsv["R"] = 50; charsv["S"] = 51; charsv["T"] = 52; charsv["U"] = 53; charsv["V"] = 54;
charsv["W"] = 55; charsv["X"] = 56; charsv["Y"] = 57; charsv["Z"] = 58; charsv["["] = 59;
charsv["\\"] = 60; charsv["]"] = 61; charsv["^"] = 62; charsv["_"] = 63; charsv["`"] = 64;
charsv["a"] = 65; charsv["b"] = 66; charsv["c"] = 67; charsv["d"] = 68; charsv["e"] = 69;
charsv["f"] = 70; charsv["g"] = 71; charsv["h"] = 72; charsv["i"] = 73; charsv["j"] = 74;
charsv["k"] = 75; charsv["l"] = 76; charsv["m"] = 77; charsv["n"] = 78; charsv["o"] = 79;
charsv["p"] = 80; charsv["q"] = 81; charsv["r"] = 82; charsv["s"] = 83; charsv["t"] = 84;
charsv["u"] = 85; charsv["v"] = 86; charsv["w"] = 87; charsv["x"] = 88; charsv["y"] = 89;
charsv["z"] = 90; charsv["{"] = 91; charsv["|"] = 92; charsv["}"] = 93; charsv["~"] = 94;
charsv["š"] = 104; charsv["›"] = 105; charsv["œ"] = 106;
var x = this.getField("S1").valueAsString;
var out = this.getField("B1");
var m = 0;
var transformedString = x.replace(/[A-Z0-9]{2}/g, function(a) {
if (isNaN(a)) {
if (m > 0)
a = 'š' + a;
m = -1;
} else {
if (parseInt(a) > 94) {
if (m != 0)
a = 'š' + a;
m = 2;
} else {
a = String.fromCharCode(parseInt(a) + 32);
if (m == 2 || m == -1)
a = '›' + a;
m = 1;
}
}
return a;
});
transformedString = transformedString;
out.value = transformedString;
thank you so far.
But now comes the last few steps:
first: get the values from every single char from the array "charsv".
second: sum of char-values: (104*1)+(char-value1*1)+(char-value2*2)+(char-value3*3) and so on.
third: get the modulo of the sum. (sum of char-values % 103).
fourth: get the char of this value from the array "chars". if " ", replace with "€".
fifth: add "š" at the beginning from "transformedString" add the char from fourth and an "œ" at the end of "transformedString".
jsfiddle (adaptation of my answer to How to split and rebuild a string?)
var m = 0; // 0 = init, -1 = alpha-num, 1 = numeric < 95, 2 = numeric > 94
var transformedString = "1Z3245799938735257".replace(/[A-Z0-9]{2}/g, function(a) {
if (isNaN(a)) {
if (m > 0)
a = 'š' + a;
m = -1;
} else {
if (parseInt(a) > 94) {
if (m != 0)
a = 'š' + a;
m = 2;
} else {
a = String.fromCharCode(parseInt(a) + 32); // the only additional line
if (m == 2 || m == -1)
a = '>' + a;
m = 1;
}
}
return a;
});
function convert(x) {
return x.replace(/›[^š]*/g, function (y) {
var s = "›";
for (var i = 1; i < y.length; i += 2) {
var code = parseInt(y.substring(i, i + 2));
s += String.fromCharCode(code + 32);
}
return s;
});
}
var result = convert("1Z›324579š99›38735257"); // 1Z›#Moš99›FiTY
Here is my attempt, might be a little much.
function transform(str) {
return str.replace(/›[^š]+/g, function(m) {
m = m.slice(1).replace(/../g, function(c) {
return String.fromCharCode(parseInt(c) + 32)
})
return '›' + m
});
}
var result = transform('1Z›324579š99›38735257') // 1Z›#Moš99›FiTY
I think you can use a regex like this:
/(\d{2})(?=\d+)/g
With substitution $1,.
[Regex Demo]

Categories

Resources