I am trying to make an interactive stack application using javascript in browser but I cannot clear my console after every actions.
<script>
var st1 = 10;
var stk1 = new Array(10);
var count = 0;
function push1(v) {
if (st1 === 0) {
rpiseconsole.log("Stack Overflow");
}
else {
st1 = st1 - 1;
stk1[st1] = v;
print1();
}
}
function pop1() {
var temp = stk1[st1];
st1 = st1 + 1;
print1();
return temp;
}
function print1() {
for (var i = st1; i < 10; i++) {
rpiseconsole.log(stk1[i]);
}
};
function doJob() {
var x = document.getElementById("t").value;
push1(x);
document.getElementById("t").value = "";
}
function doJob1() {
var p = pop1();
document.getElementById("t1").value = p;
}
</script>
I am expecting that every time I click on the push button it will print the array of stack and clear the previous results in the console and when I click on the pop button it will take out the last data and print the remaining array into the textarea.
I cannot get the clear option in console and I cannot output my array to the textarea in the same order as I put items into the stack.
Write a method clearConsole() and invoke it before every print command
function clearConsole() {
document.getElementById( "rpiseconsole" ).innerHTML = "";
}
Demo
var st1 = 10;
var stk1 = new Array(10);
var count = 0;
function push1(v) {
if (st1 === 0) {
rpiseconsole.log("Stack Overflow");
} else {
st1 = st1 - 1;
stk1[st1] = v;
print1();
}
}
function pop1() {
var temp = stk1[st1];
st1 = st1 + 1;
print1();
return temp;
}
function print1() {
clearConsole();
for (var i = st1; i < 10; i++) {
rpiseconsole.log(stk1[i]);
}
};
function doJob() {
var x = document.getElementById("t").value;
push1(x);
document.getElementById("t").value = "";
}
function doJob1() {
var p = pop1();
document.getElementById("t1").value = p;
}
function clearConsole() {
document.getElementById( "rpiseconsole" ).innerHTML = "";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js">
</script>
<div>Procedural Stack with Tight Coupling</div>
<div id="rpisesetup">
<textarea id="rpiseconsole"></textarea>
<script src="https://sites.google.com/a/rajeshpatkar.com/library/hub/rpiseconsole.js">
</script>
<textarea id="t"></textarea>
<textarea id="t1"></textarea>
<button onclick="doJob()">push</button>
<button onclick="doJob1()">pop</button>
</div>
Related
I have a code that generates a string of 6 characters, this code generates 3 different characters and 3-digital number in the column
I would like this code to check in this column cell by cell whether the values generated by this generator are unique (they are not repeated). If he finds repetitive value in the cell, I want to generate the value for the cell again.
Thank you in advance for your help
// index.gs
function onOpen() {
SpreadsheetApp.getUi().createMenu("Indexe Master Babe").addItem('Insert that SHIT', 'insertData').addToUi(); // Or DocumentApp or SlidesApp or FormApp.
}
indexService.gs
function insertData() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var coordinates = getCoordinatesToStart(sheet);
if (coordinates) {
var indexArray = prepareIndexArray(); //geting two demension array of indexes
sheet.getRange(coordinates.row, coordinates.column, indexArray.length, 1).setValues(indexArray); // get range from marked cell to array.length cells
} else {
showAlert();
}
}
function getCoordinatesToStart(sheet) {
var activeCell = sheet.getActiveCell();
var row = activeCell.getRow();
var column = activeCell.getColumn();
if (isZeroSafe(row) || isZeroSafe(column)) {
return false;
} else {
return {
row: row,
column: column
};
}
}
function prepareIndexArray() {
var array = [];
var takenStrings = [];
var index = "";
var blankSpace = 0;
for (var i = 1; i <= 1000; i++) {
var indexString = getRandomIndexString();
while (takenStrings.indexOf(indexString) !== -1) {
var indexString = getRandomIndexString();
}
takenStrings.push(indexString);
index = indexString + getIndexNumber(i);
array.push([index]);
};
return array;
}
function getRandomIndexString() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (var i = 0; i < 3; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
};
function getIndexNumber(i) {
var indexNumber = "";
var blankSpace = 3 - getIntegerLength(i);
for (var zeroCount = 0; zeroCount < blankSpace; zeroCount++) {
indexNumber += "0";
};
indexNumber += i;
return indexNumber;
}
function showAlert() {
var ui = SpreadsheetApp.getUi();
var result = ui.alert(
'Please click on any Cell',
ui.ButtonSet.OK);
}
// utilities.gs
function isNull(input) {
return input === null;
}
function isUndefinded(input) {
return input === undefined;
}
function isZero(input) {
return input === 0;
}
function isZeroSafe(input) {
return isUndefinded(input) || isNull(input) || isZero(input)
}
function getIntegerLength(number) {
var stringHelper = number + "";
return stringHelper.length;
}
class Pairs extends Screen {
constructor() {
super();
var pair1 = null;
var nPair1;
var solution;
var rightCounter = 0;
var licao, nS;
}
pairScreen(screen, lesson, nScreen) {
var body = document.body
var nodes = xmlDoc.getElementsByTagName("PAIRS");
this.licao = lesson;
this.nS = nScreen;
this.solution = screen.getElementsByTagName("SOLUTION")[0].textContent.split(" ");
body.innerHTML = '';
Startup.h1(body, "Babel (" + languageName + ")");
Startup.hr(body);
var d = DynamicHTML.div(body, "border:3px solid black; display:table; padding:20px; margin-left:40px");
Startup.h1(d, "Match the pairs");
var p1 = Startup.p(d, "padding-left:40px; word-spacing:50px;");
Startup.text(p1, 16, " ");
Startup.text(p1, 32, " ");
var p2 = Startup.p(d, "padding-left:20px;");
var button;
var i;
var f = function(i) {
Startup.eventHandler2()
}
var original = screen.getElementsByTagName("ORIGINAL")[0].textContent;
var buttons = original.split(" ");
for (i = 0; i < buttons.length; i++) {
button = DynamicHTML.inpuButton(p1, i, buttons[i], "orangered");
Startup.eventHandler2(document.getElementById(i), "onclick", function() {
checkAnswer(buttons[i], i)
});
}
Startup.hr(body);
}
checkAnswer(pair, nPair) {
var index;
if (pair1 = null) {
pair1 = pair;
nPair1 = nPair;
} else {
for (index = 0; index < solution.length; index++) {
if (pair1 == solution[index]) {
if (index % 2 == 0 && solution[index - 1] == pair) {
DynamicHTML.play("general/right_answer.mp3");
rightCounter = rightCounter + 2;
document.getElementById(nPair).disabled = true;
document.getElementById(nPair1).disabled = true;
pair1 = null;
nPair1 = null;
} else if (solution[index + 1] == pair) {
DynamicHTML.play("general/right_answer.mp3");
rightCounter = rightCounter + 2;
document.getElementById(nPair).disabled = true;
document.getElementById(nPair1).disabled = true;
pair1 = null;
nPair1 = null;
} else {
DynamicHTML.play("general/wrong_answer.mp3");
pair1 = null;
nPair1 = null;
}
}
}
}
if (rightCounter == solution.length) {
if (xmlDoc.getElementsByTagName("LESSON")[licao].childNodes[nS + 2] != null) {
var fs = new Screen();
fs.functionScreen(licao, nS + 2);
} else fs.initialScreen();
}
}
}
Wrote this for a JavaScript project I'm working on but when I run it It says Uncaught ReferenceError: checkAnswer is not defined, I'd really appreciate if anyone knew the problem. Thank you!
P.S. Don't know if the checkAnswer function has bugs or note becausa I couldn't test it out, I will when I can run it :)
First you need to bind this at the end of this function:
Startup.eventHandler2(document.getElementById(i), "onclick", function() {
this.checkAnswer(buttons[i], i)
}.bind(this));
Basically this tells the anonymous function "hey I want this to refer to this outer class, not the function itself".
(edited)
You need to either bind checkAnswer in the constructor like-so:
class Pairs extends Screen {
constructor() {
super();
var pair1 = null;
var nPair1;
var solution;
var rightCounter = 0;
var licao, nS;
this.checkAnswer = this.checkAnswer.bind(this);
}
Or use and arrow function which gives you the exact reference to the this checkAnswer like-so:
checkAnswer = (pair, nPair) => {
//your code goes here ...
}
This way you will be able to call this.checkAnswer inside the scope of your pairScreen function or wherever you want to call the function like #Davelunny point out
when trying to get items from local storage that are named
useJSON1, useJSON2 and so on.
i get an infinite loop.
var test = 0;
function loadTasks() {
let i = 1
let taskObject = JSON.parse(localStorage.getItem('useJSON' + i));
while (test < i)
if (taskObject) {
// do somthing;
i++;
} else {
test = i;
}
}
Have you checked your syntax, and brackets?
Should it look more like this?
var test = 0;
function loadTasks() {
var i = 1;
var taskObject = JSON.parse(localStorage.getItem('useJSON' + i));
while (test < i){
if (taskObject) {
`do somthing`;
i++;
} else {
test = i;
}
}
}
This issue is asked already some times. But in the application of google apps script i can't solve this problem.
function two ()
{
var bridgeclubs = SpreadsheetApp.openById("1dfyI1jbz..........TVg4OoixKTz1");
var bridgeclubs_sheet = bridgeclubs.getSheetByName("Sheet1");
var data_bridgeclubs = bridgeclubs_sheet.getDataRange().getValues();
var numRows = bridgeclubs_sheet.getDataRange().getNumRows();
var sprshtname = SpreadsheetApp.getActiveSpreadsheet().getName();
for (var i=1; i<=numRows; i++)
{
if (sprshtname == data_bridgeclubs[i][6])
{
var A = i;
break;
}
}
}
In function one I do a call to function two, where I need this value A:
function one ()
{
two ();
//here I need this value A;
var C= 35 * A;for instance
}
Who can help me?
You can return value from function using return i; and obtain it in within one by var A = two();.
function two() {
var bridgeclubs = SpreadsheetApp.openById("1dfyI1jbz..........TVg4OoixKTz1");
var bridgeclubs_sheet = bridgeclubs.getSheetByName("Sheet1");
var data_bridgeclubs = bridgeclubs_sheet.getDataRange().getValues();
var numRows = bridgeclubs_sheet.getDataRange().getNumRows();
var sprshtname = SpreadsheetApp.getActiveSpreadsheet().getName();
for (var i=1; i<=numRows; i++) {
if (sprshtname == data_bridgeclubs[i][6]) {
return i;
}
}
}
function one() {
var C;
var A = two();
if (A) {
C = 35 * A;
}
}
You can use global variables.
var A;
function two() {
var bridgeclubs = SpreadsheetApp.openById("1dfyI1jbz..........TVg4OoixKTz1");
var bridgeclubs_sheet = bridgeclubs.getSheetByName("Sheet1");
var data_bridgeclubs = bridgeclubs_sheet.getDataRange().getValues();
var numRows = bridgeclubs_sheet.getDataRange().getNumRows();
var sprshtname = SpreadsheetApp.getActiveSpreadsheet().getName();
for (var i=1; i<=numRows; i++) {
if (sprshtname == data_bridgeclubs[i][6]) {
A = i;
break;
}
}
}
function one() {
two();
var C= 35 * A;
}
I want to create a function such that
each time the function runs, the value of variable P will increase;
the number of times the function runs is set by the variable runtimes.
For example:
var runtimes = '25';
var P = '1';
function send(){
//some function
}
After the first run, variable P will be 2 and the second run will start.
How can I do this?
You can call the function from a loop:
var runtime = 25;
var P = 1;
function send(){
P++;
}
for (var i = 0; i < runtime; i++) {
send();
}
Or keep track of the iterations in the function itself and call it once:
var iterations = 0;
var runtime = 25;
var P = 1;
function send(){
P++;
if (++iterations < runtime) {
send();
}
}
send();
var runtime = 25;
var P = 1;
for(var P = 1; P < runtime; send(), P++) { }
function send(){
//some function
}
function sendMultipleTime(){
var runtime = 25;
var P = 1;
while(P<=25)
send();
P++;
}
function send(){
}
You can use a simple for loop, if I've understood the question correctly.
var runtime = 25;
var P = 1;
for (var i = P; i <= runtime; i++) {
function send() {
console.log(i + ' ' + P + ' ' + runtime);
}
send();
P++;
}