I want to create a loop which increments the column name of excel from a starting point.
For Example:
If I start the column name from X so as long as I keep increment the character by 1 It should return the output like - Y, Z, AA, AB,..., AZ, BA, BB,... .
The starting of Excel column name is A and code should be able to return the column name with atleast 5 characters i.e. ZZZZZ, untill loop continues.
This is what I tried, but can't handle for increasing column name after Z:
var i3;
var text3 = "";
var c;
for(i3 = 65; 90 >= i3; i3++) {
c = String.fromCharCode(i3);
text3 += c + ", ";
}
Haven't done any javascript in several years so this is purely logic-based.
Could have a while loop going which calculates the MOD(i3Check,26)>0 with MOD being function to check what the remaining is of i3Check/26, i.e. 27/26 = 1
Something like this in your for loop (don't forget var i3Check):
c = ""
i3Check = i3
While(MOD(i3Check,26)>0){
c = c+String.fromCharCode(26);
i3Check = i3Check - 26;
}
If(i3Check>0){
c = String.fromCharCode(i3Check) + c;
}
I have no way to test this nor do I know the syntax anymore for javascript but it should be close :p
Related
The question is as follows:
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string s, int numRows);
Example 1:
Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Example 2:
Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P I N
A L S I G
Y A H R
P I
I have written the following code, but I am stuck in terms of how to flag the row as one time to be downward moving, where I increment the start row, but when it's zigzagging back to the top, it should be decremented. I am unable to figure out the logic to make this work without affecting the downward movement. Any help would be appreciated.
const convert = (s, numRows) => {
let startRow = 0
let endRow = numRows - 1
let startColumn = 0
let endColumn = Math.floor((s.length / 2) - 1)
s = s.split('')
let results = []
// to setup the columns
for (let i = 0; i < numRows; i++) {
results.push([])
}
while (startRow <= endRow && startColumn <= endColumn && s.length) {
for (let i = startRow; i <= endRow; i++) {
results[i][startColumn] = s.shift()
}
for (let i = endRow - 1; i >= startRow; i--) {
results[i][startColumn + 1] = s.shift()
startColumn++
}
//this line seems to be the issue
startRow++
}
return results
}
console.log(convert('PAYPALISHIRING', 4))
I rewrote your while loop as follows where I simply walk a "zigzag" pattern! Hopefully, it is simple enough to understand.
let c=0, row=0,col=0, down=0;
while(c<s.length) {
results[row][col]=s[c];
if(down==0) { // moving down
row++;
if(row==numRows) {
down = 1;
col++;
row-=2;
}
} else { // moving up
row--;
col++;
if(row==0) {
down=0;
}
}
c++;
}
Ps. Above code does not handle numRows < 3 so you have to manage them before this loop.
My precalculus is a little rusty, but the logic behind this problem seems like a sine wave. I made a math error somewhere in creating the sin equation that prevents this from working (r never equals c with the current paramaters), but hopefully this will help if this is the direction you choose to go in.
/*If x-axis is position in string, and y-axis is row number...
n=number of rows
Equation for a sin curve: y = A sin(B(x + C)) + D
D=vertical shift (y value of mid point)
D=median of 1 and n
n: median:
1 1
2 1.5
3 2
4 2.5
5 3
6 3.5
7 4
median=(n+1)/2
D=(n+1)/2
A=amplitude (from the mid-point, how high does the curve go)
median + amplitude = number of rows
amplitude = number of rows - median
A=n-D
C=phase shift
Phase shift for a sin curve starting at its lowest point: 3π/2
(so at time 1, row number is 1, and curve goes up from there)
C=3π/2
Period is 2π/B
n p
3 4
4 6
5 8
6 10
period=2(n-1)
2(n-1)=2π/B
B(2(n-1)=2π
B=2π/2(n-1)
B=π/(n-1)
Variables:
s = string
n = number of rows
c = current row number being evaluated
p = position in string
r = row number
*/
var output='';
function convert(s,n) {
D=(n+1)/2
A=n-D
C=(3*Math.PI)/2
B=Math.PI/(n-1)
for (c=1;c<=n;c++) { //loop from 1st row to number of rows
for (p=1;p<=s.length;p++) { //loop from 1st to last character in string
r=A*Math.sin(B*(p+C))+D //calculate the row this character belongs in
if (r==c) { output+= s.charAt(r) } //if the character belongs in this row, add it to the output variable. (minus one because character number 1 is at position 0)
}}
//do something with output here
}
I really need your help with this since this goes well beyond my level of capability of javascript coding.
I'd like to design a function that would accomplish one of the following two scenarios:
If there is no dash and number at the end of the string var 'fileno', then rewrite the string fileno and add the dash and then the count at the end.
var fileno = 'test'
var c = 4
fileno = 'test-4'
If there is already a dash and then a number at the end of the string, replace the dash-number with the new info below:
var fileno = 'test-2'
var c = 3
fileno = 'test-3'
You may use regular expression with String.prototype.replace():
fileno = fileno.replace(/-\d+$|$/, '-' + c);
It literally means: replace -{number} or nothing at the end of the string with -{c}.
var c = 3;
console.log( 'test'.replace(/-\d+$|$/, '-' + c) );
console.log( 'test-4'.replace(/-\d+$|$/, '-' + c) );
if(fileno[fileno.length - 2] === "-"){
fileno = fileno.substr(0,-1) + c;
} else {
fileno += "-" + c;
}
Just either append a new number if there is already a - in the second last position or append a new one.
I have to spreadsheets. I want the program to look at Row A on spreadsheet Ind and see if it is a 1 or 0. if it is a one on the active sheet "return" I want it to grab the date from Row D in spreadsheet "Ind" and post it onto Spreadhseet "return". I can't figure this out and I have it working on VBA in excel.
Any help would be greatly appreciated.
function myFunction() {
X = 5;
Y = 2;
Z = 1;
Count = 4560;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source_sheet = ss.getSheetByName("Ind");
var target_sheet = ss.getSheetByName("Returns");
while (Z < Count){
if (source_sheet.getRange("A" & X) = 1) {
var buydate = source_sheeet.getRange("D" & X).getValues()
target_sheet.getRange("A" & Y) = buydate
target_sheet.getRange("B" & Y) = "Buy"
Y = Y + 1
} else if (source_sheeet.Range("C" & X) = 2) {
var selldate = source_sheeet.Range("D" & X).getvalues()
target_sheet.getRange("A" & Y) = selldate
target_sheet.getRange("B" & Y) = "Sell"
Y = Y + 1
}
X = X + 1
Z = Z + 1
}}
This line:
if (source_sheet.getRange("A" & X) = 1) {
Is using an ampersand, and it should be a plus sign. To concatenate strings in JavaScript, use a plus sign.
Also, source_sheet.getRange() will return a range, not a value, so it's never going to equal 1. You would need to use something like the following:
if (source_sheet.getRange("A" + X.toString()).getValue() === 1) {
And use triple equal signs for an equality check. JavaScript is constantly attempting to coerce variables into the type that seems correct. So, it might convert the number in the variable "X" to a string, but you can also use the toString() method.
getValues() returns a two-dimensional array. Each inner array represent a row. Each element in the inner array represents a cell in a row.
If you only want to get one value, use getValue() (no "s" on the end) instead of getValues().
var buydate = source_sheet.getRange("D" + X.toString()).getValue();
You are trying to set the value by using an equal sign. That won't work. You need to use the setValue() or setValues() method.
target_sheet.getRange("A" + Y.toString()).setValue(buydate);
By not using the var key word in your assignments, the variables automatically become "global" variables.
X = 5;
Y = 2;
Z = 1;
There's no need to make them global variables in this case, I don't think.
var X = 5,
Y = 2,
Z = 1;
You can declare multiple variables all at the same time.
Let's say i have three variables:
a, b, c
and i set them such values:
2,1,3
I have such string:
ilovemama
how could i change char position, via block's of three, in my case i have three blocks:
ilo
vem
ama
let's try on first block:
1 2 3
i l o
and i must change this position's via my a,b,c:
2 1 3
l i o
And so over, and then concat this block's into one line...
I think that i explain it normally.
I can do this on jQuery, but i can't imagine, how to do this on pure JS. i try a little bit, but this is without sense(
var string = 'some string'
a = string.charAt(0),
b = string.charAt(1),
c = string.charAt(2); // and so on
var newString = b + a + c; //oms
var otherString = c + b + a; //mos
.charAt(0) will select the first leter of the string(the one with index 0) and so on.
assigning the values to vars you can manipulate the string as I understand you want to do
for blocks,
doing;
var string='some string';
var a = string.slice(0, 3),
b = string.slice(3, 7),
c = string.slice(7, 11); and so on
Then the same
var newString = c +a +b; // will be = 'ringsome st'
To find an Index as you request in the comment you can use;
var str = "Hello There",
indexOfr = str.indexOf("r");
console.log(indexOfr); // outputs 9
A function could be;
function dynamo(string) {
var len = string.length-1,
parts = 3,
whereToCut = Math.floor(len/parts);
var a = string.slice(0, whereToCut),
b = string.slice(whereToCut, (whereToCut *2)),
c = string.slice((whereToCut *2), len+1);
return b + a + c;
//(or you could hwere some code to see what order you want, i dont understand your request there)
}
dynamo('what do you really want to do??');
//returns "u really wwhat do yoant to do??"
My code:
function calculate(sides) {
var sides = prompt("Triangle side lengths in cm
(number,number,number)"); //String will be size of 4
var nsides = sides.split(" "); //Splits content into array format
//Convert Array instances to integer values a,b,c
for(var loop=0;loop<=nsides.length;loop++) {
if(nsides[loop]!=",")
a = nsides[loop];
if(nsides[loop]!=",")
b = nsides[loop];
if(nsides[loop]!=",")
c= nsides[loop];
} //End for
//Area Calculation
var s = (a+b+c)*0.5 ; //represents the semiperimeter
var area = Math.sqrt(s*(s-a)*s(s-b)*(s-c)) //area calculation
//Result
sides = alert("The triangle's area is " + area + " square cm");
} //End function
//Main calculate(length);
I'm looking to set side a, b, and c to integers; however in order to do that I have to go through the array (I first converted it to an array from a string)
I'm going to add in some standard validation later; as of now I can't seem to place the values from the string entered into 3 separate integers being a b and c.
Other than that, is there a better way i can go about this?
Thanks.
Maybe I misunderstand your question, but is this what you're looking for?
var sides = prompt("Triangle side lengths in cm (number,number,number)");
var nsides = sides.split(",");
var a = +nsides[0];
var b = +nsides[1];
var c = +nsides[2];
//Area Calculation
//...
Note the use of + to force the strings from the array into numbers.
function calculate() {
var sides = prompt("Triangle side lengths in cm (number,number,number)"),
nsides = sides.split(","),
a = parseFloat(nsides[0]),
b = parseFloat(nsides[1]),
c = parseFloat(nsides[2]),
s = (a + b + c) / 2,
area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
alert("The triangle's area is " + area + " square cm");
return area; // return the area
}
First of all I removed your parameter, it was totally unnecessary and was overwritten by the declaration of sides in the first line. Then I changed the split to , so it follows your instructions. Then you need to parse the string to integers using parseInt and specifiying the radix 10, then you can go on with your calculations. Just a last thing, you wrote Math.sqrt(s*(s-a)*s(s-b)*(s-c)), see that s(s-b) causes an exception because you are using a number to be called as a function.