Modifying an ArrayBuffer with index access [duplicate] - javascript

I create a buffer and then a Uint8Array on it, but the array does not have any values. I would expect it to have the values of the buffer. This is an easily reproducible example:
var buf = new ArrayBuffer(32);
for (var index = 0; index < 32; index++) buf[index] = index;
console.log(buf);
var arr = new Uint8Array(buf);
console.log(arr);
The thing I tried in reality is a date format converter like this:
//Buffers and views
function convertDateTimeToFormat(date, initialFormat, endFormat) {
var buf = new ArrayBuffer(14);
var result = new Uint8Array(buf);
console.log(date);
var initialPositions = {};
var endPositions = {};
var sizeSoFar = 0;
for (var c of initialFormat) {
if (c === 'y') {
initialPositions.y = new Uint32Array(date, sizeSoFar, 1);
} else {
initialPositions[c] = new Uint8Array(date, sizeSoFar, 2);
}
sizeSoFar += ((c === 'y') ? 4 : 2);
}
sizeSoFar = 0;
for (var c of endFormat) {
if (c === 'y') {
endPositions.y = new Uint32Array(buf, sizeSoFar, 1);
} else {
endPositions[c] = new Uint8Array(buf, sizeSoFar, 2);
}
sizeSoFar += ((c === 'y') ? 4 : 2);
}
for (var key in initialPositions) {
var limit = (key === 'y') ? 4 : 2;
for (var index = 0; index < limit; index++) endPositions[c][index] = initialPositions[c][index];
}
return result;
}
//2019-03-01 13:03:50
var buf = new ArrayBuffer( 14 );
buf[0] = 2;
buf[1] = 0;
buf[2] = 1;
buf[3] = 9;
buf[4] = 0;
buf[5] = 3;
buf[6] = 0;
buf[7] = 1;
buf[8] = 1;
buf[9] = 3;
buf[10] = 0;
buf[11] = 3
buf[12] = 5;
buf[13] = 0;
console.log(convertDateTimeToFormat(buf, "yMdHms", "yMdHms"));
console.log(convertDateTimeToFormat(buf, "yMdHms", "MdyHms"));
But due to the behavior I described at the start of this question, the results are all zeroes.
This works but it's not elegant, because it expects a date format and if I am to ensure that the input is agnostic to date formats, then the code will become very complicated:
//Buffers and views
var results = {};
var buf = new ArrayBuffer( 4 );
results.uint32 = new Uint32Array(buf);
results.int8 = new Uint8Array(buf);
results.uint8 = new Int8Array(buf);
results.int8[2] = -1;
console.log(results);
results.int8[2] = 0;
results.int8[1] = -1;
console.log(results);
results.int8[1] = 0;
results.int8[0] = -1;
console.log(results);
//Buffers and views
function convertDateTimeToFormat(date, format) {
var buf = new ArrayBuffer(14);
var result = new Uint8Array(buf);
var positions = {
y: 0,
M: 4,
d: 6,
H: 8,
m: 10,
s: 12
};
for (var index = 0; index < 14; index++) {
result[index] = date[positions[format[index]]++];
}
return result.join("");
}
var results = {};
//2019-03-01 13:03:50
var buf = new ArrayBuffer( 14 );
buf[0] = 2;
buf[1] = 0;
buf[2] = 1;
buf[3] = 9;
buf[4] = 0;
buf[5] = 3;
buf[6] = 0;
buf[7] = 1;
buf[8] = 1;
buf[9] = 3;
buf[10] = 0;
buf[11] = 3
buf[12] = 5;
buf[13] = 0;
console.log(convertDateTimeToFormat(buf, "yyyyMMddHHmmss"));
console.log(convertDateTimeToFormat(buf, "MMddyyyyHHmmss"));

An ArrayBuffer is just an object representing a slice of memory. It has a fixed size, that's it. It does not have properties that represent the contents, for that you'll need a typed array as a view on the buffer. Your code is just assigning properties to the buffer which works as it is an object, but it doesn't actually manipulate the byte contents which stay zero.
Don't explicitly instantiate the buffer at all if you don't need it. Just write
var arr = new Uint8Array(32);
for (var index = 0; index < 32; index++) arr[index] = index;
console.log(arr.buffer);
console.log(arr);
In your actual code, it seems you want to use an Uint8Array to store the numbers in individual bytes. And probably you should just pass that array instead of the underlying buffer into the function.
You can create it like this:
const arr = Uint8Array.of(2,0,1,9,0,3,0,1,1,3,0,3,5,0);
// or Uint8Array.from([2,0,1,9,0,3,0,1,1,3,0,3,5,0])
// or new Uint8Array(2,0,1,9,0,3,0,1,1,3,0,3,5,0]);
const buf = arr.buffer;

Related

Find longest sequence of zeros in binary representation of an integer in JavaScript?

Well in this question I have to find the longest sequence of zeros in the binary representation of an integer.
After a lot of hard work, I'm able to find the longest sequence of zeros with my logic which I changed many times.
I have one problem with my logic which is if I input an integer that has no gap in it has to give me an answer 0 which I tried to make by myself but I failed.
Right now if I input an integer that has no gap it gives me output infinity.
I want my answer to print 0 when the binary representation of an integer has no gap in it.
var dn = prompt("Enter a number: ");
var bn = new Array();
var i = 0;
var binary = [];
while (dn != 0) {
bn[i] = dn % 2;
dn = Math.floor(dn / 2);
i++;
}
for (var j = i - 1; j >= 0; j--) {
binary.push(bn[j]);
}
console.log(binary.join(""));
var currentGap = 0;
var gaps = [];
var len = binary.length;
for (var k = 0; k < len; k++) {
if (binary[k] == 0) {
currentGap++;
if (binary[k + 1] == 1) {
gaps.push(currentGap);
currentGap = 0;
}
}
}
console.log(Math.max(...gaps));
You could add initialize gaps with 0, or add condition when gaps remains empty to print 0 else max of gaps.
var dn = prompt("Enter a number: ");
var bn = new Array();
var i = 0;
var binary = [];
while (dn != 0) {
bn[i] = dn % 2;
dn = Math.floor(dn / 2);
i++;
}
for (var j = i - 1; j >= 0; j--) {
binary.push(bn[j]);
}
console.log(binary.join(""));
var currentGap = 0;
//var gaps = []
var gaps = [0];
var len = binary.length;
for (var k = 0; k < len; k++) {
if (binary[k] == 0) {
currentGap++;
if (binary[k + 1] == 1) {
gaps.push(currentGap);
currentGap = 0;
}
}
}
//if (gaps.length === 0)
// console.log(0)
//else
// console.log(Math.max(...gaps));
console.log(Math.max(...gaps));
You can simply print 0 if your gaps array has no length.
var dn = prompt("Enter a number: ");
var bn = new Array();
var i = 0;
var binary = [];
while (dn != 0) {
bn[i] = dn % 2;
dn = Math.floor(dn / 2);
i++;
}
for (var j = i - 1; j >= 0; j--) {
binary.push(bn[j]);
}
console.log(binary.join(""));
var currentGap = 0;
var gaps = [];
var len = binary.length;
for (var k = 0; k < len; k++) {
if (binary[k] == 0) {
currentGap++;
if (binary[k + 1] == 1) {
gaps.push(currentGap);
currentGap = 0;
}
}
}
console.log(gaps.length ? Math.max(...gaps) : 0); // <-- This
I apologize if I'm misunderstanding your question, but would this satisfy your problem?
const s = prompt("Enter a number:");
const longest = Math.max(...parseInt(s).toString(2).split('1').map(s=>s.length));
console.log(longest);
just change var gaps = [];
to var gaps = [0];
You can also do that...
let dn = parseInt(prompt("Enter a number: "))
if (isNaN(dn)) console.log('Not a Number!')
else
{
let
dnStr = dn.toString(2) // binary convertion
, count = 0
, zVal = '0'
;
while (dnStr.includes(zVal) )
{
count++
zVal += '0'
}
console.log( `${dnStr}\n--> ${count}` )
}
If you want to do your own binary convertion, prefert o use Javasscript binary opérators:
let dn = parseInt(prompt("Enter a number: "))
if (isNaN(dn)) console.log('Not a Number!')
else
{
let gap = [0]
let Bin = []
let count = 0
for (;;)
{
Bin.unshift( dn & 1 ) // --> dn % 2
if (dn & 1)
{
gap.push(count)
count = 0
}
else count++
dn >>>=1 // --> dn = Math.floor(dn / 2)
if (!dn) break // --> if (dn === 0 )
}
console.log(Bin.join(''))
console.log(Math.max(...gap))
}
I had a go at this and this is what I came up with as a function:
function binaryGap(N){
let binary = Math.abs(N).toString(2); // Convert to Binary string
let binarySplit = binary.split("1"); // Split the string wherever there is "1"
let tempBinaryArr = []; // create an empty array to store the element from Splice
let max = 0; // Setting the max length to zero to compare with each element
// Removing the zeros at the end
if(binarySplit[binarySplit.length -1] == 0){
tempBinaryArr = binarySplit.splice(binarySplit.length -1, 1);
}
//Looping through each element of binarySplit to compare with variable max
for(let j=0; j < binarySplit.length; j++){
if(max < binarySplit[j].length){
max = binarySplit[j].length;
}
}
return `Longest Sequence of Zeros: ${max}`;
}
If I understood the task correctly, you can arrange the logic in something like this. In case if the input comes as a string you don't need the .toString() part
function splitByZero(str) {
const eachSubstr = str
.toString()
.split(1)
.filter((e) => e); //to remove empty values in the new array
let maxLength = 0;
for (const substr of eachSubstr) {
if (maxLength < substr.length) {
maxLength = substr.length;
}
}
console.log(maxLength);
}
splitByZero(111011100011);
function findLongestDistance(){
findGap("1000100100001");
findGap("1000100");
findGap("000100");
findGap("10000");
findGap("000001");
findGap("111111")
}
function findGap(n){
var split = n.split(/1/g);
var longestZeros = 0;
if(split.length>2){
for(var i in split){
var len = split[i].length;
if(len>longestZeros){
longestZeros = len;
}
}
console.log(longestZeros);
return longestZeros;
}
console.log(0);
return 0;
}

JavaScript create two dimensional array

I'm new to JavaScript, I'm trying to solve leetcode question 37. I need to a create a blank two dimensional array, I initially used the method in the comments; however, it doesn't work correctly, it will change all the value. Then, I used the for loop method to create array and currently it worked correctly. But I still cannot figured out why this will happen, could anyone explain the reason why this will happen, is this because of shallow copy?
var solveSudoku = function (board) {
// let rows = new Array(9).fill(new Array(10).fill(0)),
let rows = new Array(9);
for (let i = 0; i < 9; i++) {
rows[i] = new Array(10).fill(0);
}
let cols = new Array(9);
for (let i = 0; i < 9; i++) {
cols[i] = new Array(10).fill(0);
}
let boxes = new Array(9);
for (let i = 0; i < 9; i++) {
boxes[i] = new Array(10).fill(0);
}
// let cols = new Array(9).fill(new Array(10).fill(0)),
// boxes = new Array(9).fill(new Array(10).fill(0));
for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
let c = board[i][j];
if (c !== '.') {
let n = parseInt(c),
bx = Math.floor(j / 3),
by = Math.floor(i / 3);
// 0代表为使用,1为使用过
rows[i][n] = 1;
console.log(i, n)
cols[j][n] = 1;
// box索引
boxes[by * 3 + bx][n] = 1;
}
}
}
fill(board, 0, 0)
function fill(board, x, y) {
// 完成填充条件
if (y === 9) return true;
// 下一个点的坐标
let nx = (x + 1) % 9,
// 判断进入是否下一行
ny = (nx === 0) ? y + 1 : y;
// 如果已经填充,则进入下一个点
if (board[y][x] !== '.') return fill(board, nx, ny);
// 没有被填充过
for (let i = 1; i <= 9; i++) {
let bx = Math.floor(x / 3),
by = Math.floor(y / 3),
box_key = by * 3 + bx;
if (!rows[y][i] && !cols[x][i] && !boxes[box_key][i]) {
rows[y][i] = 1;
cols[x][i] = 1;
boxes[box_key][i] = 1;
board[y][x] = i.toString();
console.log(board[y][x])
// 递归向下一个点求解
if (fill(board, nx, ny)) return true;
// 恢复初始状态
board[y][x] = '.';
boxes[box_key][i] = 0;
rows[y][i] = 0;
cols[x][i] = 0;
}
}
return false;
}
console.log(board);
};
The problem with fill(), at least with object, is that it passes the same object, by reference, to all element of the array. So if you mutate this object, then it will mutate every object of every arrays.
Note that in your case, you are creating a new Array object using it's constructor ( new Array() ) which makes them objects.
const matrix = new Array(5).fill(new Array(5).fill(0));
console.log(matrix);
In the previous snippet, you can see that the values of the other rows, from the second one to the end, are reference to the initial row.
To get around that, you can fill you array with empty values and then use the map() to create unique object for each position in the array.
const matrix = new Array(5).fill().map(function() { return new Array(5).fill(0); });
console.log(matrix);
As you can see in the previous snippet, all the rows are now their unique reference.
This is the reason all of your values were changed.
I've applied this solution to your code. I wasn't able to test it, because I wasn't sure of the initial parameters to pass.
I've also used anonymous function here ( function() { return; } ), but I would success using arrow function ( () => {} ) instead, if you are comfortable with them. It's cleaner.
var solveSudoku = function (board) {
let rows = new Array(9).fill().map(function() { return new Array(10).fill(0); }),
cols = new Array(9).fill().map(function() { return new Array(10).fill(0); }),
boxes = new Array(9).fill().map(function() { return new Array(10).fill(0); });
for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
let c = board[i][j];
if (c !== '.') {
let n = parseInt(c),
bx = Math.floor(j / 3),
by = Math.floor(i / 3);
// 0代表为使用,1为使用过
rows[i][n] = 1;
console.log(i, n)
cols[j][n] = 1;
// box索引
boxes[by * 3 + bx][n] = 1;
}
}
}
fill(board, 0, 0)
function fill(board, x, y) {
// 完成填充条件
if (y === 9) return true;
// 下一个点的坐标
let nx = (x + 1) % 9,
// 判断进入是否下一行
ny = (nx === 0) ? y + 1 : y;
// 如果已经填充,则进入下一个点
if (board[y][x] !== '.') return fill(board, nx, ny);
// 没有被填充过
for (let i = 1; i <= 9; i++) {
let bx = Math.floor(x / 3),
by = Math.floor(y / 3),
box_key = by * 3 + bx;
if (!rows[y][i] && !cols[x][i] && !boxes[box_key][i]) {
rows[y][i] = 1;
cols[x][i] = 1;
boxes[box_key][i] = 1;
board[y][x] = i.toString();
console.log(board[y][x])
// 递归向下一个点求解
if (fill(board, nx, ny)) return true;
// 恢复初始状态
board[y][x] = '.';
boxes[box_key][i] = 0;
rows[y][i] = 0;
cols[x][i] = 0;
}
}
return false;
}
console.log(board);
};

Repeated sequence of numbers in Javascript

I want to generate a vector of 100 values composed by [1 0]:
This is how I did it in Matlab:
n = 100;
Seq1 = [1 0]; % sequence of 1-0
Vector = repmat(Seq1,(n/2),1); % Creates n/2 sequences of 1-0
The result is a vector like: [1 0 1 0 1 0 1 0...]
Is there a way to get the same result with JavaScript?
You could mimic the function repmat with a while loop.
function repmat(array, count) {
var result = [];
while (count--) {
result = result.concat(array);
}
return result;
}
var nTrials = 100,
Seq1 = [1, 0],
Vector = repmat(Seq1, nTrials / 2);
console.log(Vector);
Assuming you're looking for a way to add a 1 and then a 0, not an array containing 1 and 0:
var myArray = [];
nTrials = 30;
for(i = 1; i<= nTrials/2; i++){
myArray.push(1);
myArray.push(0)
}
document.body.innerHTML = myArray[1];}
https://jsfiddle.net/6seqs6af/1/
FWIW, here is the full repmat implementation in JavaScript.
It uses arrow functions (=>) which isn't available in all browsers.
// Seq1 is an Array (1D vector). We need a Matrix which JavaScript doesn't have
// natively. But we can derive a Matrix type from an Array by adding
// `numberOfRows` and `numberOfColumns` properties as well as a `set` method
function Matrix(numberOfRows, numberOfColumns) {
this.numberOfColumns = numberOfColumns;
this.numberOfRows = numberOfRows;
this.length = numberOfColumns * numberOfRows;
this.fill();
}
Matrix.prototype = Array.prototype;
Matrix.prototype.set = function() {
for (var i = 0; i < arguments.length; i++) {
this[i] = arguments[i];
}
return this;
}
Matrix.prototype.toString = function() {
return this.reduce((acc, x, idx) => acc + (idx % this.numberOfColumns === this.numberOfColumns - 1 ? x + '\n' : x + ', '), '');
}
Matrix.prototype.at = function(row, column) {
return this[row * this.numberOfColumns + column];
}
// Repmap
// ======
function repmat(mat, repeatColumns, repeatRows) {
var numberOfColumns = mat.numberOfColumns * repeatColumns;
var numberOfRows = mat.numberOfRows * repeatRows;
var values = [];
for (var y = 0; y < numberOfRows; y++) {
for (var x = 0; x < numberOfColumns; x++) {
values.push(mat.at(y % mat.numberOfRows, x % mat.numberOfColumns));
}
}
var result = new Matrix(numberOfRows, numberOfColumns);
result.set.apply(result, values);
return result;
}
// Calculation
// ===========
var nTrials = 100;
var seq1 = new Matrix(1, 2);
seq1.set(1, 0);
var vector = repmat(seq1, nTrials / 2, 1);
console.log(vector.toString());

How can I split the below string into a 2dimensional-array:

How can I split the below string into a 2dimensional-array:
Customer::Europe|UK|Scotland|Product::Drinks|Water|
array:
[Customer][Europe]
[Customer][UK]
[Customer][Scotland]
[Product][Drinks]
[Product][Water]
Not sure how to create the array. Haven't coded in years, so be kind
hArray= [];
vArray= [];
var i = j = 0;
var count = hierarchy.search(/[:|]+/);
write(hierarchy);
while (count > 0) {
if (hierarchy.indexOf(":") < hierarchy.indexOf("|") || (hierarchy.indexOf(":") > 0 && hierarchy.indexOf("|") == -1) ) {
hArray[j] = hierarchy.substr(0,hierarchy.indexOf(":"));
hierarchy = hierarchy.slice(hierarchy.indexOf(":")+2);
count = hierarchy.search(/[:|]+/);
j++;
} else
if (hierarchy.indexOf("|") < hierarchy.indexOf(":") {
vArray[i] = hierarchy.substr(0,count);
hierarchy = hierarchy.slice(count+1);
count = hierarchy.search(/[:|]+/);
i++;
}
if (count == -1) break;
//create multiArray ?
}
var source = "Customer::Europe|UK|Scotland|Product::Drinks|Water|";
var parts = source.split(/(\w+::)/);
var result = [];
for (var i = 1; i < parts.length; i += 2) {
var key = parts[i].replace("::", "");
var values = parts[i + 1].split("|");
for (var j = 0; j < values.length - 1; ++j) {
var line = new Array(2);
line[0] = key;
line[1] = values[j];
result.push(line);
}
}
console.log(result);
You can use Array.reduce like this. First, we split on | that is behind any owrd followed by ::. Then we reduce it, by using an array as memo and push an array into the memo, which we finally return.
var arr = input.split(/\|(?=\w+::)/).reduce(function(arr, str){
var array = str.split('::');
return arr.push(str.split('::')[1].split('|').filter(String).map(function(s){
return [array[0], s]
})), arr;
}, []);

Have 42 HTML Elements that need to be placed in 3 arrays. Javascript

Kind of a noob question, but I have a page with 42 checkboxes all placed into an array, but I need to split the array into 3 smaller arrays.
array(0) = smallOne(0);
array(1) = smallTwo(0);
array(2) = smallThree(0)
array(3) = smallOne(1);
array(4) = smallTwo(1);
array(5) = smallThree(1);
And so forth. Is there a method that does this or will I just need to list them all out?
Here's the javascript so far:
function SendForm() {
var elLength = form1.elements.length;
var chk = new Array(42);
var desc = new Array(14);
var local = new Array(14);
var other = new Array(14);
for (i = 0; i < elLength; i++) {
var count = 0;
var type = form1.elements[i].type;
if (type == "checkbox") {
if (form1.elements[i].checked) {
chk(count) = true;
}
else {
chk(count) = false;
}
count++;
}
else {
}
}
}
You could do something like this to assign them:
for (var i = 0; i < 14; i++)
{
var x = i * 3;
desc[i] = chk[x];
local[i] = chk[x + 1];
other[i] = chk[x + 2];
}

Categories

Resources