I am creating a website application that allows users to select a seat, if it is not already reserved, and reserve it.
I have created a very round about way of getting the seats that are previously reserved using iFrames, however that was temporarily, now I need to make it secure and "proper javascript code" using proper practices. I have no clue what AJAX (or JSON) is, nor how to add it to this code, but it needs to get the file "seatsReserved"+this.id(that is the date)+"Que.html" and compare the string of previously reserved seats to see which class to make the element. If this is horrible, or if any of the other things could work better, I am open to criticism to everything. Thank you all!
Here is the javascript code:
A little side note, all of the if statements are due to different amount of seats in each row
<script>
var i = " 0 ";
var counter = 0;
var leng=0;
document.getElementById("Show1").addEventListener("click", changeDay);
document.getElementById("Show2").addEventListener("click", changeDay);
document.getElementById("Show3").addEventListener("click", changeDay);
function changeDay() {
var iFrame = document.getElementById("seatList");
iFrame.src = "seatsReserved" + this.id + "Que.html";
document.getElementById('date').innerHTML = this.id;
var seatsTaken = iFrame.contentWindow.document.body.innerHTML;
var k = 0;
let = 'a';
var lc = 0;
for (lc = 1; lc <= 14; lc++) {
if (lc == 1) {
leng = 28;
}
else if (lc == 2) {
leng = 29;
}
else if (lc == 3) {
leng = 32;
}
else if (lc == 4 || lc == 6 || lc == 12 || lc == 14) {
leng = 33;
}
else if (lc == 5 || lc == 13) {
leng = 34;
}
else if (lc == 8 || lc == 10) {
leng = 35;
}
else {
leng = 36;
}
for (k = 1; k <= leng; k++) {
if (seatsTaken.indexOf((" " +
let +k + " ")) <= -1) {
seat = document.getElementById(let +k);
seat.removeEventListener("click", selectedSeat);
}
else {
document.getElementById(let +k).className = "openseat";
document.getElementById(let +k).removeEventListener("click", doNothing);
}
}
let = String.fromCharCode(let.charCodeAt(0) + 1);
}
}
function loadChanges() {
var iFrame = document.getElementById("seatList");
var seatsTaken = iFrame.contentWindow.document.body.innerHTML;
var k = 0;
let = 'a';
var lc = 0;
var leng = 0;
for (lc = 1; lc <= 14; lc++) {
if (lc == 1) {
leng = 28;
}
else if (lc == 2) {
leng = 29;
}
else if (lc == 3) {
leng = 32;
}
else if (lc == 4 || lc == 6 || lc == 12 || lc == 14) {
leng = 33;
}
else if (lc == 5 || lc == 13) {
leng = 34;
}
else if (lc == 8 || lc == 10) {
leng = 35;
}
else {
leng = 36;
}
for (k = 1; k <= leng; k++) {
if (seatsTaken.indexOf((" " +
let +k + " ")) <= -1) {
seat = document.getElementById(let +k);
seat.addEventListener("click", selectedSeat);
seat.className = "openseat";
}
else {
document.getElementById(let +k).className = "notAvailible";
document.getElementById(let +k).addEventListener("click", doNothing);
}
}
let = String.fromCharCode(let.charCodeAt(0) + 1);
}
i = " 0 ";
counter = 0;
document.getElementById("seatString").innerHTML = i;
document.getElementById("getSeats").value = i;
document.getElementById("seatnums").innerHTML = counter;
}
i = document.getElementById("seatString").innerHTML;
counter = document.getElementById("seatnums").innerHTML;
function selectedSeat() {
var w = this.id;
var l = (" " + w);
var b = (" " + w + " ");
if (counter < 5) {
if (i.indexOf(b) <= 0) {
this.className = "closedseat";
i = i + b;
i = i.replace(" 0 ", " ");
document.getElementById("seatString").innerHTML = i;
document.getElementById("getSeats").value = i;
counter = counter + 1;
document.getElementById("seatnums").innerHTML = counter;
}
else if (i.indexOf(b) > 0) {
this.className = "openseat";
i = i.replace(b, "");
document.getElementById("seatString").innerHTML = i;
document.getElementById("getSeats").value = i;
counter = counter - 1;
document.getElementById("seatnums").innerHTML = counter;
}
}
else if (i.indexOf(b) > 0) {
this.className = "openseat";
i = i.replace(b, "");
document.getElementById("seatString").innerHTML = i;
document.getElementById("getSeats").value = i;
counter = counter - 1;
document.getElementById("seatnums").innerHTML = counter;
}
}
function doNothing() {
}
var rannum = Math.random() * 1000;
document.getElementById('getConfirmation').value = rannum;
</script>
Related
Please I am working on an extension to discover where apples are on a game website but I keep seeing this error causing it to not work properly. I have tried a few things but they just make the script to not work at all. The error is one line 142 with the attr() function:
Uncaught Typeerror: Cannot read properties of undefined (reading
'split').
This is the full code:
var string_id = 1231414;
var page_title = document.title;
function sender_cheker(min, max)
{
return Math.floor(Math.random() * (max - min + 1)) + min;
}
window.onload = function() {
document.onclick = ({target}) => {
page_title = document.title;
console.log(page_title);
if (page_title.indexOf('Apple Of Fortune') !== -1)
{
let parent = target;
clas_name_parent = parent.parentNode.parentNode.className;
if (clas_name_parent == "fortune-apple-bet__btn fortune-apple-btn fortune-apple-btn--bet")
{
arry_apple_start = creete_new_str();
$('.image_dop_for_provide').remove();
setInterval(print_result_apple,1000);
}
}
}
function creete_new_str()
{
var summ_true_apple = new Array();
var summ_false_apple = new Array();
var array_good_bed_apple = new Array();
summ_true_apple[0] = 4;
summ_true_apple[1] = 4;
summ_true_apple[2] = 4;
summ_true_apple[3] = 4;
summ_true_apple[4] = 3;
summ_true_apple[5] = 3;
summ_true_apple[6] = 3;
summ_true_apple[7] = 2;
summ_true_apple[8] = 2;
summ_true_apple[9] = 1;
summ_false_apple[0] = 1;
summ_false_apple[1] = 1;
summ_false_apple[2] = 1;
summ_false_apple[3] = 1;
summ_false_apple[4] = 2;
summ_false_apple[5] = 2;
summ_false_apple[6] = 2;
summ_false_apple[7] = 3;
summ_false_apple[8] = 3;
summ_false_apple[9] = 4;
var napravlen = 0;
for(i=0;i<10;i++)
{
array_good_bed_apple[i] = new Array();
if (napravlen == 0)
{
for(k=0;k<5;k++)
{
var rand_var_for_this = coinToss();
if (rand_var_for_this == true)
{
if (summ_true_apple[i] > 0)
{
array_good_bed_apple[i][k] = 1;
summ_true_apple[i] --;
}
else
{
array_good_bed_apple[i][k] = 0;
summ_false_apple[i] --;
}
}
else
{
if (summ_false_apple[i] > 0)
{
array_good_bed_apple[i][k] = 0;
summ_false_apple[i] --;
}
else
{
array_good_bed_apple[i][k] = 1;
summ_true_apple[i] --;
}
}
}
}
else
{
for(k=4;k>=0;k--)
{
var rand_var_for_this = coinToss();
if (rand_var_for_this == true)
{
if (summ_true_apple[i] > 0)
{
array_good_bed_apple[i][k] = 1;
summ_true_apple[i] --;
}
else
{
array_good_bed_apple[i][k] = 0;
summ_false_apple[i] --;
}
}
else
{
if (summ_false_apple[i] > 0)
{
array_good_bed_apple[i][k] = 0;
summ_false_apple[i] --;
}
else
{
array_good_bed_apple[i][k] = 1;
summ_true_apple[i] --;
}
}
}
}
if (napravlen == 0)
napravlen = 1;
else
napravlen = 0;
}
return array_good_bed_apple;
}
function print_result_apple()
{
var row_naw = $('div[class="fortune-apple-field__row fortune-apple-row fortune-apple-row--is-active"] button',0).attr('data-item').split('-');
var ind = -1;
for(i=0;i<10;i++)
{
if (i == 0)
ind = 9;
if (i == 1)
ind = 8;
if (i == 2)
ind = 7;
if (i == 3)
ind = 6;
if (i == 4)
ind = 5;
if (i == 5)
ind = 4;
if (i == 6)
ind = 3;
if (i == 7)
ind = 2;
if (i == 8)
ind = 1;
if (i == 9)
ind = 0;
if (row_naw[0] == ind)
{
$('.image_dop_for_provide').remove();
for(k=0;k<5;k++)
{
if (arry_apple_start[i][k] == 1)
$('button[data-item="'+ind+'-'+k+'"]').html('<img class="image_dop_for_provide" src="https://i.ibb.co/WtvVkbC/apple-t-f.png" style="opacity: 0.4; width:100%">');
else
$('button[data-item="'+ind+'-'+k+'"]').html('<img class="image_dop_for_provide" src="https://i.ibb.co/ZWfxcY9/apple-f-t.png" style="opacity: 0.4; width:100%">');
}
}
}
}
function coinToss()
{
return (Math.floor(Math.random() * 2) === 0);
}
};
This message:
Uncaught Typeerror: Cannot read properties of undefined (reading ’split’)
indicates that the string you are trying to call split(‘-') on is undefined. That means that the result of $('div[class="fortune-apple-field__row fortune-apple-row fortune-apple-row--is-active"] button',0).attr('data-item’) must be undefined.
There’s not enough detail to diagnose further.
In my research, I need to generate Fricke Polynomials. Luckily the job has been done for me on a website. However, it is in javascript+html+css so I am not sure how it works as I only use Mathematica. I was wondering if anyone could explain how I could make all of this work on a single javascript code with a single input (and where to put the input). My plan is then to just take the code and embed it in Mathematica as I have seen this is possible. The input is a word say made up of a's and b's say aabbbaa and an output is a polynomial. I do not need to understand the code really.
Thank you
function Polynomial(){
this.data = "";
this.setData = function(str){
this.data = str;
}
this.toString = function(){
var s = this.data;
if (this.data.startsWith("x0y0z0"))
s = s.replace("x0y0z0", "1");
s = s.replace(/x0y0z0/g, "")
.replace(/\s\+\s-/g, "</sup> - ").replace(/\s\+/g, "</sup> +")
.replace(/x/g, "x<sup>").replace(/y/g, "</sup>y<sup>")
.replace(/z/g, "</sup>z<sup>")
.replace(/\s1x/g, " x").replace(/\s1y/g, " y").replace(/\s1z/g, " z");
s += "</sup>";
s = s.replace(/x<sup>0<\/sup>/g, "").replace(/y<sup>0<\/sup>/g, "")
.replace(/z<sup>0<\/sup>/g, "")
.replace(/x<sup>1<\/sup>/g, "x").replace(/y<sup>1<\/sup>/g, "y")
.replace(/z<sup>1<\/sup>/g, "z");
if (s.startsWith("1x"))
s = s.replace("1x", "x");
if (s.startsWith("1y"))
s = s.replace("1y", "y");
if (s.startsWith("1z"))
s = s.replace("1z", "z");
return s;
}
}
function compareMonomial(m1, m2){
m1s = m1.split(/[x|y|z]/g);
m2s = m2.split(/[x|y|z]/g);
if (m1s[3]< m2s[3]) return -1;
else if (m1s[3] > m2s[3]) return 1;
else if (m1s[2]< m2s[2]) return -1;
else if (m1s[2] > m2s[2]) return 1;
else if (m1s[1]< m2s[1]) return -1;
else if (m1s[1] > m2s[1]) return 1;
else return 0;
}
function addMonomial(m1, m2){
m1s = m1.split(/[x|y|z]/g);
m2s = m2.split(/[x|y|z]/g);
coef = parseInt(m1s[0]) + parseInt(m2s[0]);
if (coef == 0)
return "ZERO"
else
return coef + "x" + m1s[1] + "y" + m1s[2] + "z" + m1s[3];
}
function subMonomial(m1, m2){
m1s = m1.split(/[x|y|z]/g);
m2s = m2.split(/[x|y|z]/g);
coef = parseInt(m1s[0]) - parseInt(m2s[0]);
if (coef == 0)
return "ZERO";
else
return coef + "x" + m1s[1] + "y" + m1s[2] + "z" + m1s[3];
}
function mulMonomial(m1, m2){
m1s = m1.split(/[x|y|z]/g);
m2s = m2.split(/[x|y|z]/g);
coef = parseInt(m1s[0]) * parseInt(m2s[0]);
coefx = parseInt(m1s[1]) + parseInt(m2s[1]);
coefy = parseInt(m1s[2]) + parseInt(m2s[2]);
coefz = parseInt(m1s[3]) + parseInt(m2s[3]);
if (coef == 0)
return "ZERO";
else
return coef + "x" + coefx + "y" + coefy + "z" + coefz;
}
function add(p1, p2){
if (p1.data == "")
return p2;
if (p2.data == "")
return p1;
var r = new Polynomial();
p1Mos = p1.data.split(" + ");
p2Mos = p2.data.split(" + ");
var data = "";
var k = 0; var current = p1Mos[0];
for (var i = 0; i < p2Mos.length; i++){
if (k >= p1Mos.length)
data += " + " + p2Mos[i];
else {
while (k < p1Mos.length && compareMonomial(current, p2Mos[i]) < 0){
data += " + " + p1Mos[k]; k++; current = p1Mos[k];
}
if (current == undefined)
data += " + " + p2Mos[i];
else if (compareMonomial(current, p2Mos[i]) == 0){
if (addMonomial(current, p2Mos[i]) != "ZERO")
data += " + " + addMonomial(current, p2Mos[i]);
k++; current = p1Mos[k];
}
else{
data += " + " + p2Mos[i];
}
}
}
while (k < p1Mos.length){
data += " + " + p1Mos[k]; k++
}
r.setData(data.substring(3));
return r;
}
function sub(p1, p2){
if (p2.data == "")
return p1;
if (p1.data == ""){
//need to update here
return p2;
}
var r = new Polynomial();
p1Mos = p1.data.split(" + ");
p2Mos = p2.data.split(" + ");
var data = "";
var k = 0; var current = p1Mos[0];
for (var i = 0; i < p2Mos.length; i++){
p2coef = p2Mos[i].split(/[x|y|z]g/)[0];
if (parseInt(p2coef) < 0)
p2MoNeg = p2Mos[i].substring(1);
else
p2MoNeg = "-" + p2Mos[i];
if (k >= p1Mos.length)
data += " + " + p2MoNeg;
else {
while (k < p1Mos.length && compareMonomial(current, p2Mos[i])< 0){
data += " + " + p1Mos[k]; k++; current = p1Mos[k];
}
if (current == undefined)
data += " + " + p2MoNeg;
else if (compareMonomial(current, p2Mos[i]) == 0){
if (subMonomial(current, p2Mos[i]) != "ZERO")
data += " + " + subMonomial(current, p2Mos[i]);
k++; current = p1Mos[k];
}
else{
data += " + " + p2MoNeg;
}
}
}
while (k < p1Mos.length){
data += " + " + p1Mos[k]; k++
}
r.setData(data.substring(3));
return r;
}
// multiply polynomial p1 (with array of monomials p1Mos) with a monomial m2
function mulPM(p1Mos, m2){
r = new Polynomial();
var data = "";
for (var i = 0; i < p1Mos.length; i++){
if (mulMonomial(p1Mos[i], m2) != "ZERO")
data += " + " + mulMonomial(p1Mos[i], m2);
}
r.setData(data.substring(3)); return r;
}
// multiply polynomial p1 with polynomial p2
function mul(p1, p2){
p1s = p1.data.split(" + ");
p2s = p2.data.split(" + ");
r = new Polynomial();
for (var i = 0; i < p2s.length; i++){
r = add(r, mulPM(p1s, p2s[i]));
}
return r;
}
function Fricke(s){
var w = new Word();
w.setWordString(s);
w.reduce(); s = w.toString();
var r = new Polynomial();
var l = s.length;
// Trivial case or case where 2 consecutive letters among last 4 letter
// are the same
if (l == 0){
r.setData("2x0y0z0");
return r;
}
if (l == 1){
if (s == "a" || s == "A")
r.setData("1x1y0z0");
else
r.setData("1x0y1z0");
return r;
}
if (s.charAt(l-1) == s.charAt(l-2)){
var c = s.charAt(l-1);
var s1 = s.substring(0, l-2);
r = sub(mul(Fricke(s1 + c), Fricke(c + "")), Fricke(s1));
return r;
}
if (l == 2){
if (s == "ab" || s == "ba" || s == "AB" || s == "BA"){
r.setData("1x0y0z1");
return r;
}
else if (s == "aB" || s == "Ba" || s == "Ab" || s == "bA"){
r.setData("1x1y1z0 + -1x0y0z1");
return r;
}
}
if (s.charAt(l-2) == s.charAt(l-3)){
s = s.charAt(l-1) + s.substring(0, l-1);
r = Fricke(s);
return r;
}
if (l == 3){
s = s.charAt(l-1) + s.substring(0, l-1);
r = Fricke(s);
return r;
}
if (s.charAt(l-3) == s.charAt(l-4)){
s = s.substring(l-2, l) + s.substring(0, l-2);
r = Fricke(s);
return r;
}
// Case 2: w = x1Yx2y
var t = s.charCodeAt(l-1) - s.charCodeAt(l-3);
if ( t == 32 || t == -32){
var w1 = new Word();
w1.setWordString(s.substring(l-2, l));
var s2 = s.substring(0, l-2) + w1.bar().toString();
r = sub(mul(Fricke(s.substring(0, l-2)), Fricke(s.substring(l-2, l))),
Fricke(s2));
return r;
}
// Case 3: w = ...Xyxy
t = s.charCodeAt(l-2) - s.charCodeAt(l-4);
if ( t == 32 || t == -32){
r = Fricke(s.charAt(l-1) + s.substring(0, l-1));
return r;
}
// Last case 4: w = ...xyxy
r = sub(mul(Fricke(s.substring(0, l-2)), Fricke(s.substring(l-4, l-2))),
Fricke(s.substring(0, l-4)));
return r;
}
// Helper methods:
function cancel2Char(src, i){
dest = [];
for (var j = 0; j < i; j++)
dest[j] = src[j];
for (var j = i; j < src.length - 2; j++)
dest[j] = src[j+2];
return dest;
}
function trim2Ends(src){
dest = [];
for (var j = 1; j < src.length - 1; j++)
dest[j - 1] = src[j];
return dest;
}
// Function to find the minimum equivalent class of a reduced cyclic word rc.
function minReduceCyclic(rc){
var min = rc.clone();
var W = new Word();
for (var i = 0; i < rc.word.length; i++){
W = rc.permute(i);
if (min.compareTo(W) > 0)
min = W;
}
return min;
}
function canonical(W){
W.reduce();
W.toReduceCyclic();
W = minReduceCyclic(W);
return W;
}
// CLASS Word
function Word(){
this.word = [];
this.setWord = function(w){
this.word = w;
}
this.setWordString = function(s){
// a-z = 1-26 && A-Z = -1 - -26;
for (var i = 0; i < s.length; i++){
var c = s.charCodeAt(i);
if (c >= 65 && c <= 90 )
this.word[i] = -(c - 64);
else if (c >= 97 && c < 122)
this.word[i] = c - 96;
else {
this.word = []; return;
}
}
}
this.length = function(){ return this.word.length; }
}
Word.prototype.isReduce = function(){
for(var i = 0; i < this.word.length - 1; i++)
if (this.word[i] == -this.word[i+1])
return false;
return true;
}
Word.prototype.reduce = function(){
var i = -2;
while(i != this.word.length - 1){
if (this.word.length == 0)
return;
for(i = 0; i < this.word.length - 1; i++)
if (this.word[i] == -this.word[i+1]){
this.word = cancel2Char(this.word, i);
break;
}
}
return;
}
Word.prototype.toReduceCyclic = function(){
this.reduce();
while(this.word[0] == -this.word[this.word.length - 1])
this.word = trim2Ends(this.word);
return;
}
Word.prototype.toString = function(){
var s = "";
for (var i = 0; i < this.word.length; i++){
if (this.word[i] < 0)
s = s + String.fromCharCode(-this.word[i] + 64);
else
s = s + String.fromCharCode(this.word[i] + 96);
}
return s;
}
Word.prototype.bar = function(){
var r = new Word();
var w = r.word;
for (var j = 0; j < this.word.length; j++)
w[j] = - this.word[this.word.length - 1 - j];
return r;
}
Word.prototype.permute = function(i){
var r = new Word();
var w = r.word;
for (var j = 0; j < this.word.length; j++)
w[j] = this.word[(j + i) % this.word.length];
return r;
}
Word.prototype.isEqual = function(W2){
var w1 = this.word;
var w2 = W2.word;
if (w1.length != w2.length)
return false;
for (var i = 0; i < w1.length; i++)
if (w1[i] != w2[i])
return false;
return true;
}
Word.prototype.compareTo = function(W2){
var w1 = this.word;
var w2 = W2.word;
var l = Math.min(w1.length, w2.length);
for (var i = 0; i < l; i++){
if (w1[i] < w2[i])
return -1;
if (w1[i] > w2[i])
return 1;
}
if (w1.length > w2.length)
return 1;
if (w1.length < w2.length)
return -1;
return 0;
}
Word.prototype.clone = function(){
var r = new Word();
w = r.word;
for (var i = 0; i < this.word.length; i++)
w[i] = this.word[i];
return r;
}
Word.prototype.primitiveExp = function(){
w = this.word; l = w.length;
for(var i = 1; i <= l/2; i++){
if (l % i == 0){
found = true;
test:
for (var j = 0; j < l/i; j++)
for (var t = 0; t < i; t++)
if (w[t] != w[t + j * i]){
found = false;
break test;
}
}
if (found)
return l/i;
}
return 1;
}
// Need to debug
function concat(W1, W2){
r = new Word();
var w1 = W1.word;
var w2 = W2.word;
var w = []
for (var i = 0; i < w1.length; i++)
w[i] = w1[i];
for (var i = 0; i < w2.length; i++)
w[i + w1.length] = w2[i];
r.setWord(w);
return r;
}
// These are supposed to be surface word methods
// sw is a word and the order is a map, which
// maps characters in a word (1-k or (-1)-(-k)) to its order in the alphabet
function order(sw){
var w = sw.word;
var o = new Map();
for (var i = 0; i < w.length; i++)
o.set(w[i], i);
return o;
}
function findCharPos(sw, c){
for (var i = 0; i < sw.word.length; i++)
if (sw.word[i] == c)
return i;
}
var time;
$(document).ready(function(){
$("#compute").click(function(){
$("#note").text("Note: Computing.....");
time = new Date().getTime();
setTimeout(function(){
new Promise(compute).then(doneCompute("Note: Done! The computation time is about "));
}, 50);
});
$("#reset").click(function(){
$("#note").text("Note:");
$("#word").val("");
$("#fpOutput").text("");
});
});
function compute(value){
$("#fpOutput").html(Fricke($("#word").val()).toString());
$("#fpOutput").slideDown("slow");
}
function doneCompute(value){
$("#note").text(value + (new Date().getTime() - time - 50) + " milliseconds.");
}
<!DOCTYPE html>
<html>
<head>
<link href="fricke.css" type="text/css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="Word.js"></script>
<script type="text/javascript" src="Polynomial.js"></script>
<script type="text/javascript" src="gui.js"></script>
</head>
<body>
<h2>Computing Fricke Polynomial for word with 2 generators a and b</h2>
<br>
<b>Word: </b>
<input type="text" class="inputText" id="word"></input><br><br>
<br><br>
<button class="button" id="compute">Compute</button>
<button class="button" id="reset">Reset</button>
<br><br>
<div id="note">Note:</div>
<br><br>
<div class="slide">Fricke Polynomial</div>
<div class="output" id="fpOutput"></div>
</body>
</html>
I am trying to make this work, I don't know what is wrong with it.
It used to work with only 3 variables, when I made it longer it stoped and now I cannot make it work even with only 3. Now it gives me "Undefined Undefined Undefined".
What I want is to crate an alarm in a way that it shows the name of whatever has gone under 10. The fields it is comparing from are calculation fields with the REPEATABLESUM() code.
var choice1 = '';
var choice2 = '';
var choice3 = '';
var choice4 = '';
var choice5 = '';
var choice6 = '';
var choice7 = '';
var choice8 = '';
var choice9 = '';
var choice10 = '';
var choice11 = '';
var choice12 = '';
var choice13 = '';
var choice14 = '';
var choice15 = '';
var choice16 = '';
var choice17 = '';
var choice18 = '';
var choice19 = '';
var choice20 = '';
var choice21 = '';
var choice22 = '';
var choice23 = '';
var choice24 = '';
if (NUM($stock_coliblue_24) < 10) {
choice1 = LABEL('coliblue');
}
if (NUM($stock_absorvent_pads) < 10) {
choice2 = LABEL('absorvent_pads');
}
if (NUM($stock_micro_filters) < 10) {
choice3 = LABEL('micro_filters');
}
if (NUM($stock_alkaphotalkalinity) < 10) {
choice4 = LABEL('alkaphot');
}
if (NUM($stock_ammonia) < 10) {
choice5 = LABEL('ammonia');
}
if (NUM($stock_calcicol_calcium_hardness) < 10) {
choice6 = LABEL('calcicol');
}
if (NUM($stock_chloridol_nacl) < 10) {
choice7 = LABEL('chloridol');
}
if (NUM($stock_free_chlorine_dpd1) < 10) {
choice8 = LABEL('chlorine_free_dpd1');
}
if (NUM($stock_total_chlorine_dpd3) < 10) {
choice9 = LABEL('chlorine_total_dpd2');
}
if (NUM($stock_free_copper_n1) < 10) {
choice10 = LABEL('copper_free_n1');
}
if (NUM($stock_total_copper_n2) < 10) {
choice11 = LABEL('copper_total_n2');
}
if (NUM($stock_fluoride) < 10) {
choice12 = LABEL('fluoride');
}
if (NUM($stock_hardicol_total_hardness) < 10) {
choice13 = LABEL('hardicol');
}
if (NUM($stock_iron_hr) < 10) {
choice14 = LABEL('iron_hr');
}
if (NUM($stock_iron_mr) < 10) {
choice15 = LABEL('iron_mr');
}
if (NUM($stock_magnecol_magnesium) < 10) {
choice16 = LABEL('magnecol');
}
if (NUM($stock_manganese) < 10) {
choice17 = LABEL('manganese');
}
if (NUM($stock_nitratest_nitrate) < 10) {
choice18 = LABEL('nitratest');
}
if (NUM($stock_nitricol_nitrite) < 10) {
choice19 = LABEL('nitricol');
}
if (NUM($stock_phosphate) < 10) {
choice20 = LABEL('phosphate');
}
if (NUM($stock_potassium) < 10) {
choice20 = LABEL('potassium');
}
if (NUM($stock_silica) < 10) {
choice21 = LABEL('silica');
}
if (NUM($stock_sulphate) < 10) {
choice22 = LABEL('sulphate');
}
if (NUM($stock_sulphitest_sulphite) < 10) {
choice23 = LABEL('sulphitest');
}
if (NUM($stock_zinc) < 10) {
choice24 = LABEL('zinc');
}
if (choice1 == null && choice2 == null && choice3 == null && choice4 == null && choice5 == null && choice6 == null && choice7 == null && choice8 == null && choice9 == null && choice10 == null && choice11 == null && choice12 == null && choice13 == null && choice14 == null && choice15 == null && choice16 == null && choice17 == null && choice18 == null && choice19 == null && choice20 == null && choice21 == null && choice22 == null && choice23 == null && choice24 == null) {
SETRESULT('');
} else {
CONCAT (choice1 + choice2 + choice3 + choice4 + choice5 + choice6 + choice7 + choice8 + choice9 + choice10 + choice11 + choice12 + choice13 + choice14 + choice15 + choice16 + choice17 + choice18 + choice19 + choice20 + choice21 + choice22 + choice23 + choice24);
}
user array
var choice = [];
var concatStr = "";
if (NUM($stock_coliblue_24) < 10) {
choice[0] = 'coliblue';
}
if (NUM($stock_absorvent_pads) < 10) {
choice[1] = 'absorvent_pads';
}
if (choice.length == 0){
SETRESULT('');
} else {
concatStr = arr.join(" ");
}
var choice = []; /*Using array notation instead of using 24 separate variables*/
var concatStr = "";
if (NUM($stock_coliblue_24) < 10) {
choice[0] = 'coliblue'; /*storing value in array rather than in variables*/
}
,..
,..
if (NUM($stock_zinc) < 10) {
choice[23] = LABEL('zinc');
}
/* Using for loop to iterate through array to check for null values. This is easier than having '==' null check for all 24 variables*/
for(var i=0;i<24;i++) {
if(choice[i] == null) {
SETRESULT('');
break;
}
}
/*If none of the values are null, then concat the array elements. Array.join is so easier than concatenating 24 string variables.*/
if(choice.length==24) {
concatStr = choice.join(" ");
}
Hope this explains.
from the below code I'm trying to get common words(from two strings given) without converting the strings into array. The below code is getting and displaying the common words but the problem is, this code is not removing all the duplicates as it shows all the common words without removing duplicates. I tried searching, but the solution is to use split() and filter(). Is there any other way to remove duplicates.
Thanks so much in advance.
function common() {
var str1 = "is hello and he is the only hello is"
var str2 = "is hello you and is and he and is the only";
var min = 0;
var max = 0;
var count = 0;
var count1 = 0;
var count2 = 0;
var out = '';
var out2 = '';
var out3 = '';
var len1 = str1.length;
var len2 = str2.length;
var output = '';
var temp = 0;
var temp1 = 0;
for (m = 0; m < str1.length; m++) {
temp1 = 0;
if (str1.charAt(m) == " " || m == str1.length - 1) {
count1++;
if (m == str1.length - 1) {
out1 = str1.slice(temp, m + 1);
} else {
out1 = str1.slice(temp, m);
}
for (i = temp1; i < str2.length; i++) {
if (str2.charAt(i) == " " || i == str2.length - 1) {
if (i == str2.length - 1) {
out2 = str2.slice(temp1, i + 1);
} else {
out2 = str2.slice(temp1, i);
}
temp1 = i + 1;
if (out1 == out2) {
if (out3.indexOf(out1) == -1) {
out3 += out1 + ' ';
} else if (out3.indexOf(out1) >= 0) {
var r = out3.indexOf(out1);
while (out3.charAt(r) != " ") {
r++;
}
if (r != out1.length) {
out3 += out1 + ' ';
}
}
}
}
}
temp = m + 1;
}
}
console.log(out3);
out = document.getElementById("tarea3");
out.value = out3;
}
<textarea id="tarea"></textarea>
<textarea id="tarea2"></textarea>
<textarea id="tarea3"></textarea>
<button type="button" onclick="common()">Run</button>
No arrays no regex you can get the common words like this. The rest is up to your processing.
var str1 = "is hello and he is the only hello is",
str2 = "is hello you and is and he and is the only",
lut = {},
stc = "",
i = 0;
while (i <= str1.length) {
if (str1[i] !== " " && i < str1.length) {
stc+=str1[i++];
} else {
lut[stc] = "unmatch";
stc = "";
++i;
}
}
i = 0;
stc = "";
while (i <= str2.length) {
if (str2[i] !== " " && i < str2.length) {
stc+=str2[i++];
} else {
lut[stc] = lut[stc] ? "common" : "unmatch";
stc = "";
++i;
}
}
console.log(lut);
I'm using an example based on the following example:
http://jsfiddle.net/5tt7d3e6/
In this, a function is created to turn a number into words.
The function is processed in the following HTML
<input type="text" name="number" placeholder="Number OR Amount" onkeyup="word.innerHTML=convertNumberToWords(this.value)" />
<div id="word"></div>
The above allows you to enter a number into a textbox. The function translates what you type as a number into words.
Is there an easy way to set a div which already holds the number, instead of typing it to display?'
Such as:
<div data="innerHTML=convertNumberToWords(1233213)"></div>
You can hookup an event handler for onreadystatechange and inside that you can put your logic.
Fiddle 1
document.onreadystatechange = function() {
word.innerHTML = convertNumberToWords(1233213);
};
However if you want show data based on existing value inside input, first put an id attribute for input
<input id="number" ... />
And then in JS:
document.onreadystatechange = function() {
word.innerHTML = convertNumberToWords(number.value);
};
Fiddle 2
Suggestion: use switch statment in convertNumberToWords()
switch(amount){
case 0: 'zero'; break;
default: 'Please enter number only!'; break;
}
If you want to write to the document as it is processed and not later.
<div id="word">
<script>
document.write(convertNumberToWords(12233456))
</script>
</div>
This is the recommended way though:
<div id="word"></div>
<script>
document.getElementById('word').innerHTML = convertNumberToWords(12233456);
</script>
--
<script>
function convertNumberToWords(amount) {
var words = new Array();
words[0] = '';
words[1] = 'One';
words[2] = 'Two';
words[3] = 'Three';
words[4] = 'Four';
words[5] = 'Five';
words[6] = 'Six';
words[7] = 'Seven';
words[8] = 'Eight';
words[9] = 'Nine';
words[10] = 'Ten';
words[11] = 'Eleven';
words[12] = 'Twelve';
words[13] = 'Thirteen';
words[14] = 'Fourteen';
words[15] = 'Fifteen';
words[16] = 'Sixteen';
words[17] = 'Seventeen';
words[18] = 'Eighteen';
words[19] = 'Nineteen';
words[20] = 'Twenty';
words[30] = 'Thirty';
words[40] = 'Forty';
words[50] = 'Fifty';
words[60] = 'Sixty';
words[70] = 'Seventy';
words[80] = 'Eighty';
words[90] = 'Ninety';
amount = amount.toString();
var atemp = amount.split(".");
var number = atemp[0].split(",").join("");
var n_length = number.length;
var words_string = "";
if (n_length <= 9) {
var n_array = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0);
var received_n_array = new Array();
for (var i = 0; i < n_length; i++) {
received_n_array[i] = number.substr(i, 1);
}
for (var i = 9 - n_length, j = 0; i < 9; i++, j++) {
n_array[i] = received_n_array[j];
}
for (var i = 0, j = 1; i < 9; i++, j++) {
if (i == 0 || i == 2 || i == 4 || i == 7) {
if (n_array[i] == 1) {
n_array[j] = 10 + parseInt(n_array[j]);
n_array[i] = 0;
}
}
}
value = "";
for (var i = 0; i < 9; i++) {
if (i == 0 || i == 2 || i == 4 || i == 7) {
value = n_array[i] * 10;
} else {
value = n_array[i];
}
if (value != 0) {
words_string += words[value] + " ";
}
if ((i == 1 && value != 0) || (i == 0 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Crores ";
}
if ((i == 3 && value != 0) || (i == 2 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Lakhs ";
}
if ((i == 5 && value != 0) || (i == 4 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Thousand ";
}
if (i == 6 && value != 0 && (n_array[i + 1] != 0 && n_array[i + 2] != 0)) {
words_string += "Hundred and ";
} else if (i == 6 && value != 0) {
words_string += "Hundred ";
}
}
words_string = words_string.split(" ").join(" ");
}
return words_string;
}
</script>
<div id="word">
<script>
document.write(convertNumberToWords(12233456))
</script>
</div>
Fiddle Link
change the value of textbox and press click button it will return into word
<input id="check"type="text" name="number" placeholder="Number OR Amount" />
<div id="word"></div>
<input type="button" id="button" value="click">
js code
$("#button").click(function()
{
var number = $('#check').val();
$("#word").html(toWords(number));
});
var th = ['','thousand','million', 'billion','trillion'];
var dg = ['zero','one','two','three','four', 'five','six','seven','eight','nine']; var tn = ['ten','eleven','twelve','thirteen', 'fourteen','fifteen','sixteen', 'seventeen','eighteen','nineteen']; var tw = ['twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety']; function toWords(s){s = s.toString(); s = s.replace(/[\, ]/g,''); if (s != parseFloat(s)) return 'not a number'; var x = s.indexOf('.'); if (x == -1) x = s.length; if (x > 15) return 'too big'; var n = s.split(''); var str = ''; var sk = 0; for (var i=0; i < x; i++) {if ((x-i)%3==2) {if (n[i] == '1') {str += tn[Number(n[i+1])] + ' '; i++; sk=1;} else if (n[i]!=0) {str += tw[n[i]-2] + ' ';sk=1;}} else if (n[i]!=0) {str += dg[n[i]] +' '; if ((x-i)%3==0) str += 'hundred ';sk=1;} if ((x-i)%3==1) {if (sk) str += th[(x-i-1)/3] + ' ';sk=0;}} if (x != s.length) {var y = s.length; str += 'point '; for (var i=x+1; i<y; i++) str += dg[n[i]] +' ';} return str.replace(/\s+/g,' ');}