Get row number from grid index - javascript

I got this grid with each square's index showing in the middle (0-63):
Then I have a function which needs to return the row number when I pass it an index.
The row number can be between 0-7.
Function that should do this, with a poor attempt at retrieving the row number currently inside it:
function (index) {
return Math.floor(index / 7);
}
Example outputs (index -> row output):
0 -> 0
5 -> 0
7 -> 0
8 -> 1
23 -> 2
35 -> 4
43 -> 5
63 -> 7
I looked at this question which I thought was about the same issue as I'm having but the answer there did not give the correct output so I must've misunderstood it.
How can I change my function to return the correct row number from a given index?

You need to divide by column count which are 8:
function (index) {
return Math.floor(index / 8);
}

Related

'Swap' values within a range

I get data that is a floating point value between 1 and 7.
1 is bad, 7 is good.
Sometimes, when I get that data, I'd like to 'reverse' it, as the questions were written negatively, where 1 is good, and 7 bad.
A score of 1 should be 'swapped' to a score of 7.
A score of 2 should be 'swapped' to a score of 6.
A score of 2.5 should be 'swapped' to a score of 4.5.
Originally I thought I could use an array:
const array = [1,2,3,4,5,6,7]
return array[answer] - 1;
or Math.abs(answer - 6) but neither are going to work out.
I think you don't need an array, if your values are fixed (from 1 to 7) you can just substract from 8:
return 8 - answer;
Here is a simple function to achieve that:
function reverseRating(rating) {
return 8 - rating
}
The following function accomplishes this:
function reverseOnScale(answer, from, to) {
answerMinus = answer - from;
reversedMinus = Math.abs(answerMinus - (to - from));
reversed = reversedMinus + from;
return reversed;
}
Usage example:
reverseOnScale(5, 1, 7);
Outputs the value 3.

Creating and initializing arrays

I'm learning about Javascript arrays. I understand most of the code, except line 4. Can you please explain what the code in line 4 does, and how it has this output.
var fibonacci = []; //{1}
fibonacci[1] = 1; //{2}
fibonacci[2] = 1; //{3}
for(var i = 3; i < 20; i++){
fibonacci[i] = fibonacci[i-1] + fibonacci[i-2]; ////{4}
}
for(var i = 1; i<fibonacci.length; i++){ //{5}
console.log(fibonacci[i]); //{6}
}
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
Thank you.
it's Fibonacci sequence. How it works? every number after the first two is the sum of the two preceding ones that's why you have to initialize the first two terms
fibonacci[1] = 1;
fibonacci[2] = 1;
on line 1 you create the array and after intializing the first two terms you loop, starting from the third index var i = 3 to fill the array fibonacci with all the terms of the sequence. How? it assigns the sum of the two previous terms to the current one.
ps: in javascript arrays start at index 0
At Line 2 & 3 you set the values for item 1 & 2. Since fibonacci is sum of two values before, the line 4 just sums up the two values before for each element from 3 to 19.

Script not working when get value from a formula

I found this script (below) and it works when simply typing in the number zero into Column G but it does not work when the result of column G comes from subtracting the values of column E and F. When the values become 0, it is left in the sheet "default". Any help would be appreciated. I have searched and find nothing about how this works when applied to the result of a formula. Thanks!
function onEdit() {
// moves a row from a sheet to another when a magic value is entered in a column
// adjust the following variables to fit your needs
// see https://productforums.google.com/d/topic/docs/ehoCZjFPBao/discussion
var sheetNameToWatch = "default";
var columnNumberToWatch = 7; // column A = 1, B = 2, etc.
var valueToWatch = 0;
var sheetNameToMoveTheRowTo = "toMoveTo";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
sheet.deleteRow(range.getRow());
}
}
the script works as intended. I created a test spreadsheet with the script and did the following tests:
Enter random numbers in Column A — as expected nothing happened
Entered 0 in Column A — as expected nothing happened.
Entered random number in Column G (7) — nothing happened
Entered 0 in Column G — that row was moved to the other sheet
Entered 5 in columns E and F and =E3-F3 which equals to 0 (in that order) — row was moved to the other sheet
Entered 5 in column E and 4 in column F and in column G I wrote the formula =E3-F3 which resulted in 1. After that, I changed F4 to value 5 and formula recalculates to 0 — as expected, the row is not moved.
Please make sure that the formula actually returns a value of 0. It may be that your cell is formatted to write 0.1 as 0 and the formula returns a number 0 < n < 1 which would not be moved as it must be exactly 0 in order for this to work. If you wish to move values greater than 0 but less than 1, introduce a Math.floor() against the read value.
In test scenario 6 we have to note that the script does not account for changes made to any column other than G (column number 7) because you are only watching var range = sheet.getActiveCell(); so if you change anything other than the column where you are supposed to have 0 it will not do anything because of the following if statement part range.getColumn() == columnNumberToWatch

Nested While Loop

I am having trouble getting this nested while loop to work. I am using a Javascript based program that reads from 2 table columns based on the Header of the table. I am able to read from the tables but the loop is not working. My goal is to find when the neutral size of a wire becomes larger than the phase size and print the wire size at which this occurs. The code is as follows.
CSA = 130
NeutCSA = 0
i = 0
j = 0
while (NeutCSA < CSA){
j = 0
while (NeutCSA < CSA){
NeutCSA = colWireSize[i] * colNumberWires[j]
if (colNumberWires[j] < 18){
j = j + 1
}
}
if (colWireSize[i] < 10){
i = i + 1
}
}
result = colNumberWires[j]
The tables look like this
colWireSize
2
3
4
5
6
7
8
9
10
colNumberWires
6
7
8
9
10
11
12
13
14
15
16
17
18
The program will find the condition and end the loop but not in the order I need it to. I need it to start at the first row of WireSizes and then loop through and multiple that value by NumberWires. If this value is less then CSA, go to the next WireSize and repeat the process until NeutCSA > CSA.
Thanks
Pat
Your outer loop does not loop as the inner loop will always advance NeutCSA to >= CSA.
Did you mean to set the exact same conditional for each loop?

Javascript record keystroke timings

I want to record the time between each keystroke (just one key to start with, the 'A' key) in millseconds.
After the user finishes his thing he can submit and check out the timings between each key stroke. Like:
1: 500
2: 300
3: 400
4: 500
5: 100
6: 50
7: 50
8: 25
I believe this is possible with Javascript, is it?
Sure:
var times = [];
// add an object with keycode and timestamp
$(document).keyup(function(evt) {
times.push({"timestamp":evt.timeStamp,
"keycode":evt.which})
});
// call this to get the string
function reportTimes() {
var reportString = "";
for(var i = 0; i < times.length - 1; ++i) {
reportString += (i+1) + ": " + (times[i+1].timestamp - times[i].timestamp) + " ";
}
return reportString; // add this somewhere or alert it
}
I added the keycode just in case you wanted it later; it's not necessary for your exact problem statement.
Clarification from comments discussion:
The for loop only goes to up to times.length - 2 (since i is always strictly less than times.length - 1), so there is no issue about times[i+1] being outside the bounds of the array. For example, if you do five key presses and therefore have a times array with five elements (indexed from 0 to 4):
1st pass: times[1].timestamp - times[0].timestamp
2nd pass: times[2].timestamp - times[1].timestamp
3rd pass: times[3].timestamp - times[2].timestamp
4th pass: times[4].timestamp - times[3].timestamp
Then the loop terminates, because setting i to 4 triggers the termination condition:
= i < times.length - 1
= 4 < 5 - 1
= 4 < 4
= false [i cannot be set to 4 by this loop]
Thus, times[i+1] is always a validly indexed element, because i is at most one less than the maximum index.

Categories

Resources