javascript split without losing first element (not RegEx) - javascript

I have a string containing ones and zeros split by "," and ";".
var x = "1,1,0;1,0,0;1,1,1;"; x.split(";");
This wil output an array with just two strings: 1,0,0 and 1,1,1.
What I want is to put all of these numbers in a two dimensional array:
1 1 0
1 0 0
1 1 1
If there is a smarter way than just split the string, please let me know.
Otherwise, please tell me how to fix the problem above.

You need to put quotes around your string.
Commentors are correct, your array contains all 3 strings. did you forget that array indices start at 0, not 1?
x.split does not modify x, it returns an array
You probably want something like this
var str = "1,1,0;1,0,0;1,1,1";
var arr = str.split(";");
for (var i = 0, len = arr.length; i < len; i++)
{
arr[i] = arr[i].split(",");
}
and to verify the result
for (var i = 0, len = arr.length; i < len; i++)
{
for (var j = 0, len2 = arr[i].length; j < len2; j++)
{
document.write(arr[i][j] + " | ");
}
document.write("<br>");
}

given the string:
var x = "1,1,0;1,0,0;1,1,1";
you can get a two dimensional array of zeros and ones this way:
var st = x.split(";")
var twoDimensionalArray = st.map(function(k){
return k.split(",");
});
of course, thanks to JS method chaining, you can do the whole thing this way:
var twoDimTable = x.split(";").map(function(k){
return k.split(",");
});
the result:
[
["1","1","0"],
["1","0","0"],
["1","1","1"]
]
well, to get the result as
[
[1,1,0],
[1,0,0],
[1,1,1]
]
you can do a loop and for each value k within the array do k = +k;
and you will get numbers instead of strings. However, JavaScript will do the casting
for you when you use these values within an operation with a number.

Related

How to shuffle characters in a vertical inverted pattern?

I have a string "ABCDEFGHIJKLMN" that I need to shuffle in a specific manner. To do that, I write the characters sequentially in columns bottom -> top and then left -> right (4 chars per column for example) until all characters are done. If the last column is not complete, then the empty spaces need to be on the bottom (this is very important). Like so:
D H L N
C G K M
B F J
A E I
The shuffle is accomplished by producing a new string reading the block of letters as we read text, in rows left -> right:
"DHLNCGKMBFJAEI"
The cases where the columns are not complete (word.size % column_height !=0) complicate things considerably.
I came up with a few solutions, but I'm not sure if there is a simpler (ie, shorter OR easier to read) and more elegant way of coding this problem. My solutions either have an ugly, separate block of code to handle the final incomplete column or seem way too complicated.
My question is, could it be done better?
If you don't want any spoilers and decide to try and figure it out for yourself, stop reading now. If you want to work from what I fiddled so far, then a working piece of code is
var result = "";
var str = "ABCDEFGHIJKLMN";
var nr_rows = 4;
var current_row = 4;
var columns = Math.floor(str.length / nr_rows);
var modulus_table = str.length % nr_rows;
var modulus_position = -1;
for (var i = 0; i < nr_rows; i++) {
for (var j = 0; j < columns; j++) {
result += str[current_row + j * nr_rows - 1];
}
if (modulus_table > 0) {
result += str[str.length + modulus_position];
modulus_table--;
modulus_position--;
}
current_row--;
}
console.log(result);
Moving on to arrays, the next example would loop through each character, placing it correctly in a matrix-like array, but it doesn't work. The array needs to be created another way. For another example of this issue, see How to create empty 2d array in javascript?. This would also need an ugly hack to fix the last characters on the last incomplete column aligning to the bottom instead of the top.
var result = [[],[]];
var str = "ABCDEFGHIJKLMN";
var nr_rows = 4;
var row = nr_rows - 1;
var column = 0;
for (var i = 0; i < str.length; i++) {
result[row][column] = str[i];
row--;
if (row < 0) {
row = nr_rows;
column++;
}
}
console.log(result);
This last method goes full matrix array, but it quickly becomes complicated, since it needs to loop through the array in 3 different directions. First, create a dummy array with the characters in the wrong place, but where the 'undefined' positions correspond to those that should be left empty. That is acomplished by populating the array 'rotated 90ยบ' from the reading orientation.
Without this first step, the empty positions would be stacked at the bottom instead of the top.
A second pass is required to re-write the caracters in the correct places, skipping any holes in the matrix using the 'undefined' value. This check is made for every position and there is no separate block of code to handle an incomplete last line.
A third pass then reads every character in order to form the final shuffled string. All this seems way too complicated and confusing.
// matrix populated top->bottom and left->right
// with the characters in the wrong place
// but the undefined postions in the correct place of the empty positions
var matrix = [];
var str = "ABCDEFGHIJKLMN";
var rows = 4;
var columns = Math.ceil(str.length / rows);
var k = 0;
for (var i = 0; i < rows; i++) {
matrix[i] = [];
for (var j = columns - 1; j >= 0; j--) {
matrix[i][j] = str[k];
k++;
}
}
// populate the matrix with the chars in the correct place and the 'undefined' positions left empty
var k = 0;
for (var i = 0; i < rows; i++) {
for (var j = 0; j < columns; j++) {
if (matrix[i][j] != undefined) {
matrix[i][j] = str[k];
k++;
}
}
}
// read matrix in correct direction and send to string, skipping empty positions
var result = "";
for (var j = columns - 1; j >= 0; j--) {
for (var i = 0; i < rows; i++) {
if (matrix[i][j] != undefined) {
result += matrix[i][j];
}
}
}
console.log(result);
What if you just split/reverse the array into column groups, and convert to rows?
const result = str.match(/.{1,4}/g) // split string into groups of 4
.map(i => i.split('').reverse()) // reverse each group (bottom to top, and solves the last col issue)
.reduce((res, col) => { // reduce the groups into rows
col.forEach((c, i) => res[i] += c) // concat each string char to the right row
return res
}, ['','','','']) // initialise empty strings per row
.join('') // join the rows up
Fiddle here
If you wish to return a string, I don't see why any intermediate result should use an array when it doesn't have to. The following could use one less array, but it's convenient to use split and an array to control the while loop rather than mutate the string.
The idea is to fill the strings from the bottom up until the column is full, then keep adding from the bottom of each column until it runs out of characters to assign. The row to start filling from is based on how many characters are left and how many rows there are.
Rather than building strings, it could build arrays but then generating a string requires multiple joins.
It can also produce results where there are insufficient slots for all the characters, so a result using 9 characters from 10 or more using a 3x3 "matrix" (see last example).
function verticalShuffle(s, rows, cols) {
var result = [''];
s = s.split('');
while (s.length && result[0].length < cols) {
for (var i = (rows < s.length? rows : s.length) -1 ; i>=0; i--) {
if (!result[i]) result[i] = '';
result[i] += s.splice(0,1)[0] || '';
}
}
return result.join('');
}
var s = 'ABCDEFGHIJKLMN';
console.log(verticalShuffle(s, 4, 4)); // DHLNCGKMBFJAEI
console.log(verticalShuffle(s, 6, 3)); // FLNEKMDJCIBHAG
// Only use 9 characters
console.log(verticalShuffle(s, 3, 3)); // CFIBEHADG
This uses plain ed3 functionality that will run in any browser. I don't see the point of restricting it to ECMAScript 2015 or later hosts.
If interpret Question correctly, you can use for loop, String.prototype.slice() to to populate arrays with characters of string. Use Array.prototype.pop() within recursive function to get last element of array until each array .length is 0.
To create array
[
["D","H","L","N"],
["C","G","K","M"],
["B","F","J"],
["A","E","I"]
]
from vertically inverted string you can use for loop, String.prototype.slice() to set array of arrays containing elements having .length 4, or 3 once .length of parent array is 2, having been set with two arrays containing four elements
var str = "ABCDEFGHIJKLMN";
function fnVerticalInvert(str, arr, res) {
if (!arr && !res) {
arr = []; res = "";
}
if (str) {
for (var i = 0; i < str.length; i += 4) {
arr.push([].slice.call(str.slice(i, i + 4)));
}
}
for (var i = 0; i < arr.length; i++) {
if (arr[i].length) {
res += arr[i].pop()
}
}
if (arr.some(function(curr) {return curr.length}))
return fnVerticalInvert(null, arr, res);
for (var i = 0, l = 4, j = 0, n = l - 1, k; i < l; i++, j += l) {
if (i === l / 2) k = j;
arr[i] = [].slice.call(res.slice(!k ? j : k, !k ? j + l : k + n));
if (k) k += n;
}
return {str: res, arr:arr};
};
var res = fnVerticalInvert(str);
console.log(res);

Creating 2D matrix from vector array in javascript. - missing ) after argument list

I am very new in programming and learning javascript. The idea is to create matrix like
0 1 2 3 4
1 0 1 2 3
2 1 0 1 2
3 2 1 0 1
4 3 2 1 0
My edited script looks like this
var arr = [0,1,2,3,4];
var array = [];
for (j=0; j<arr.length; j++)
{
for (i=j; i<arr.length; i++)
{
array [j][i] = arr [j];
array [i][j] = arr [j];
}
}
var arrheight = array [j][i];
var arrwidth = array [i][j];
console.log(arrheight);
console.log(arrwidth)
But I am getting this new error "TypeError: Cannot set property '0' of undefined".
What am I doing wrong? I would also love to know alternative and more sophisticated method of producing such matrix.
Okay buddy, here ya go.
var linear = [0,1,2,3,4];
var dimensional = [];
var i,j;
for (i=0; i<linear.length; i++) {
dimensional[i] = [];
for (j=0; j<linear.length; j++) {
dimensional[i][j] = Math.abs(j-i);
}
}
console.log(dimensional);
Welcome to programming and the Javascript language!
There's a few problems with your code. Your current problem is that you've capitalized the word 'For' -- it should be for (note the lowercase. Javascript is case sensitive).
You have not declared your i and j variables in your for loop. You'll want to declare those like this:
for (var i = 0; i < arr.length; i++)
Next, you're assigning to the variable newarray without declaring it. You'll want to declare newarray as follows:
var newarray;
This should get you started! I highly recommend http://eloquentjavascript.net/ to continue your coding!
It's true that for statements are lowercase and JS statements terminate with a semicolon. Also, 'new' is an operator, so I would try to avoid using it.
I'm not sure I'm understanding your objective, but you can build a two dimensional array with the following code:
var x = new Array(5);
for (var i = 0; i < x.length; i++) {
x[i] = [1,2,3,4,5];
}
console.log(x);
Good luck! I hope this helps.

For loop withoit indexes javascript

I want to display an array without showing of indexes. The for loop returns the array indexes which is not showing in usual declaration.
I want to send an array like [1,2,3 ...] but after retrieving from for loop, I haven't the above format. How can I store my values as above.
var a = [];
for (var i = 1; i < 8; i++) {
a[i] = i;
};
console.log(a);
Outputs:
[1: 1, 2: 2 ...]
Desired output:
[1,2,3]// same as console.log([1,2,3])
Array indices start at zero, your loop starts at 1, with index 0 missing you have a sparse array that's why you get that output, you can use push to add values to an array without using the index.
var a = [];
for (var i = 1; i < 8; i++) {
a.push(i);
};
console.log(a);
The problem is that you start your array with 1 index, making initial 0 position being empty (so called "hole" in array). Basically you treat array as normal object (which you can do of course but it defeats the purpose of array structure) - and because of this browser console.log decides to shows you keys, as it thinks that you want to see object keys as well as its values.
You need to push values to array:
var a = [];
for (var i = 1; i < 8; i++) {
a.push(i);
};
I have to disagree with the answers provided here. The best way to do something like this is:
var a = new Array(7);
for (var i = 0; i < a.length; i++) {
a[i] = i + 1;
}
console.log(a);
Your code is making each index equal to i, so use it this way
var a = [];
for (var i = 1; i < 8; i++) {
a.push(i);
};
console.log(a);

take from a string specific values

Hello I want to take fro a string i javascript specific values , the string has this format
[
{
"st_asgeojson": "{\"type\":\"MultiLineString\",\"coordinates\":[[[23.4582348,37.5062675],[23.4577141,37.5066109],[23.4572601,37.5070038],[23.4566746,37.507301],[23.455698,37.5076256],[23.4549737,37.5079214],[23.4545445,37.5080235],[23.4538579,37.5078873],[23.4325504,37.5231202],[23.4324646,37.5234265],[23.4324646,37.5236308],[23.4326363,37.5237669]]]}"
},
{
"st_asgeojson": "{\"type\":\"MultiLineString\",\"coordinates\":[[[23.4568043,37.5042114],[23.4566078,37.5040436],[23.4567394,37.5038528],[23.4571075,37.5037422],[23.4575424,37.5035515],[23.4580841,37.5031548],[23.4589958,37.5027237]]]}"
}
]
from this string i want to make an new 2d array , in this array i want to put only the coordinates.
for example i want to have in first row the first st_asgeojson coordinates [23.4582348,37.5062675],....,[23.4326363,37.5237669] and the second row the other st_asgeojson coordinates [23.4568043,37.5042114],[23.4566078,37.5040436],......,[23.4589958,37.5027237].
Is this posible to do it ?
i try to str.split("[ ]") but is show me the same as the string i have first.
The original string is JSON, so you first have to convert it to an array with JSON.parse():
var arr = JSON.parse(str);
Then the value of the st_asgeojson property is another JSON-encoded object, so you'll have to parse that as well:
var first_coords = JSON.parse(arr[0].st_asgeojson).coordinates[0][0];
var second_coords = JSON.parse(arr[1].st_asgeojson).coordinates[0][0];
There's no [ ] anywhere in the string, so I'm not sure what you expected str.split('[ ]') to achieve. Did you mean to use a regexp, str.split(/ /) to split it on the spaces?
To get them all with a loop do:
var arrLeng = arr.length;
for (var i = 0; i < arrLeng; i++) {
var coordArray = JSON.parse(arr[i].st_asgeojson).coordinates;
var coordArrLeng = coordArray.length;
for (var j = 0; j < coordArrLeng; j++) {
var coords = coordArray[0][0];
var coordLeng = coords.length;
for (var k = 0; k < coordLeng; k++) {
alert(coords[k]);
}
}
}

java script array assignation mistake

var UserBoard = new Array(20,20);
for(var i = 0; i < 21; ++i){
for(var j = 0; j < 21; ++j){
UserBoard[i,j] = 0;
}
}
document.write(UserBoard[3,5]);
UserBoard[4,5]=1;
document.write(UserBoard[3,5]);
http://jsfiddle.net/XbyqN/2/
it's quite simple but I don't know why does this. Alert should be 0, not 1 since I've initialized the 2d array to 0.
Can someone explain me why?
Let's break it down
var UserBoard = new Array(20,20);
You are creating an array with two slots, both of them containing the value "20" (int). So your array is [20, 20]
Next, your loop :
for(var i = 0; i < 21; ++i){
for(var j = 0; j < 21; ++j){
UserBoard[i,j] = 0;
}
}
Two dimensional arrays are not defined like this. In that case, only the "j" counter does something. The "i" is simply ignored. So you end up with an array as follow : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Next, the assignement :
UserBoard[4,5]=1;
Is equivalent to :
UserBoard[5]=1;
And your alert :
alert("test: " + UserBoard[3,5]);
Is equivalent to :
alert("test: " + UserBoard[5]);
That's why you get "1" as alert.
If you want two dimensional arrays, you should use the following notation :
UserBoard[4][5] = 1;
Read it all here on MDN : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array
You want UserBoard[i][j] instead of UserBoard[i,j].
Multidimensional arrays don't work as you seem to think they work. They're, in fact, arrays of arrays.
Use this :
var UserBoard = new Array(20);
for(var i = 0; i < 20; ++i){
UserBoard[i] = new Array(20);
for(var j = 0; j < 20; ++j){
UserBoard[i][j] = 0;
}
}
I suggest you start using console.log and Chrome's developer tool to debug your code (or Firebug). Try this at the end of your code and then type the F12 key :
console.log(UserBoard);
The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand.
var UserBoard = new Array(20,20); // [20, 20]
for(var i = 0; i < 21; ++i){
for(var j = 0; j < 21; ++j){
UserBoard[i,j] = 0; // UserBoard[j] = 0
}
}
UserBoard[4,5]=1; // UserBoard[5] = 1
alert("test: " + UserBoard[3,5]); // UserBoard[5]
What you want is:
var UserBoard = [];
for (var i = 0; i < 20; i++) { // i < 20
UserBoard[i] = [];
for (var j = 0; j < 20; j++) {
UserBoard[i][j] = 0;
}
}
UserBoard[4][5]=1;
alert("test: " + UserBoard[3][5]);
When creating a new array using the array constructor (new Array), the arguments have different meanings, depending on the type and total number of arguments.
var array20long = new Array(20);// = [undefined,undefined,undefined,....].length === 20
var arrayString = new Array('foo');// = ['foo']
var yourArray = new Array(20,20);// = [20,20]
Put simply: passing 1 integer to the array constructor, creates an array with length equal to the int passed, passing several integers will result in 1, 1 dimensional array with a length equal to the total number of argumens. In your case, two integers creating an array with 2 values. Each index will be initialized to its corresponding argument. In your case: index 0 === 20, index 1 === 20, if you had written new Array(20,10), the result would be an array like [20,10].
You get the basic idea.It is important to note that accessing multi dimensional arrays using a comma won't work: instead of writing arr[1,2] you should have written arr[1][2]. Google some introductory tutorials to JavaScript, it won't hurt... and you'll soon learn why using the array constructor isn't the best way of creating arrays

Categories

Resources