Can anyone see mistake in this javascript code? Filling 2D array - javascript

The issue with my code is that it should return 26x26 matrix filled with a-z on every row, although I tried many ways to initialize or fill the matrix, I always got error or matrix filled with empty strings(current code state).
Can somebody help me? In other words I need function fill2DMatrix() to return matrix where every row contains letters from initArray, so far it doesn't change values and stays as empty Array
<script>
var rawInput = document.getElementById("input");
var initArray = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
/* Returns filled alphabet 2D array */
function init2DMatrix() {
var twoDimArr = [];
for (i = 0; i < 26; i++) {
twoDimArr[i] = "";
for(j = 0; j < 26; j++){
twoDimArr[i][j] = "";
}
}
return twoDimArr;
}
function fill2DMatrix() {
var counter = 0;
var blankSpacesArr = [];
blankSpacesArr = init2DMatrix();
for (var i = 0; i < 26; i++) {
for(var j = 0; j < 26; j++){
blankSpacesArr[i][j] = initArray[j];
}
}
return blankSpacesArr;
}
function print() {
var beaufortMatrix = fill2DMatrix();
for (i = 0; i < initArray.length; i++) {
document.getElementById("output").innerHTML += beaufortMatrix[i] + "<br>";
}
}
</script>

Please Try like this.
function init2DMatrix()
{
var arr = [];
for (var i=0; i<26; i++) {
arr[i] = [];
}
return arr;
}
function fill2DMatrix() {
var blankSpacesArr = [];
blankSpacesArr = init2DMatrix();
for (var i = 0; i < 26; i++) {
for(var j = 0; j < 26; j++){
blankSpacesArr[i][j] = initArray[j];
}
}
return blankSpacesArr;
}

You already have a array of 26 alphabets. All you need to do is loop it 26 times into a new array.
Try this -
var initArray = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
var myarray=new Array(26)
for (i=0; i <26; i++) {
myarray[i]=new Array(initArray);
}
console.log(myarray);

Related

Pushing spliced items returns nested array

I am trying to push 3 random items from an array into a new array. I use splice() to grab an item and remove it from the old array. When I push() the items, I would like to get them back in a single array.
For example:
["b", "f", "a"]
This is my code:
const letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
let newletters = [];
for (let i = 0; i < 3; i++) {
newletters.push(letters.splice(Math.floor(Math.random() * letters.length), 1));
}
console.log(newletters);
It seems like I am pushing the spliced items as arrays into the new array, is there a way to fix this?
[
[
"b"
],
[
"f"
],
[
"a"
]
]
You could spread ... the array from Array#splice.
Or take the first element with and index.
letters.splice(Math.floor(Math.random() * letters.length)[0]
const letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
let newletters = [];
for (let i = 0; i < 3; i++) {
newletters.push(...letters.splice(Math.floor(Math.random() * letters.length), 1));
}
console.log(newletters);
You can use the function Array.prototype.concat instead.
const letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
let newletters = [];
for (let i = 0; i < 3; i++) {
newletters = newletters.concat(letters.splice(Math.floor(Math.random() * letters.length), 1));
}
console.log(newletters);
Use the ... operator inside the push statement.
const letters = ['a', 'b', 'c', 'd', 'e'];
const newLetters = []
newLetters.push(...letters.splice(1, 2)) // Equal to newLetters.push('b', 'c')
console.log(newletters)
const letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
let newletters = [];
for (let i = 0; i < 3; i++) {
newletters.push(letters.splice(Math.floor(Math.random() * letters.length), 1));
}
console.log(newletters.flat());

Index characters from an array based on user input

I have an array of the entire alphabet from a to z. I also have an input field. I want to be able to find the index of each character from the input field in the alphabet array but my function doesn't work. I've tried storing the text from the input field into an array, and I've tried using a named function for it as well but neither worked.
<input type="text" id="plaintext" placeholder="Plaintext">
<div id="start"><div id="start_text">Start</div></div>
let plaintext = document.getElementById("plaintext");
let alph = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
let startB = document.getElementById("start");
let plainParser = [];
startB.addEventListener('click', () => {
for(let i=0; i < alph.length; i++){
console.log(alph.findIndex( () => plainParser.push(plaintext.value.split(''))));
};
});
A shortcut without needing the array is use the charCode of each character.
a starts at 97
const str = 'abc';
for(let s of str){
console.log(s.charCodeAt(0) - 97);
}
I want to … find the index of each character from the input field in the alphabet array
Then instead of looping from 0 to 25:
for(let i=0; i < alph.length; i++){
you should loop over every character from the input:
for (let c of plaintext.value) {
I want to … find the index of each character from the input field in the alphabet array
You have the character, so find the index:
alph.indexOf(c)
v‘là.
let plaintext = document.getElementById("plaintext");
let alph = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
let startB = document.getElementById("start");
startB.addEventListener('click', () => {
for (let c of plaintext.value) {
console.log(alph.indexOf(c));
}
});
Here's a slightly refactored version of what I think you are looking for:
const alphabet = 'abcdefghijklmnopqrstuvwxyz';
const result = document.querySelector(".result");
const plaintext = document.getElementById("plaintext");
const startB = document.querySelector(".start");
startB.addEventListener('click', () => {
const source = plaintext.value;
result.innerText = '';
if (!source) return;
const indices = [];
for (let char of source) {
indices.push(alphabet.indexOf(char));
}
result.innerText = indices.join(', ');
});
<input type="text" id="plaintext" placeholder="Plaintext">
<button class="start">Start</button>
<div class="result" style="font-family: monospace;"></div>

checking pair value in array

here i wanna ask about how to check the data in the array if not same value/data on next index, push it on new array,
here is the example:
function check(arr){
let text = "";
let newArr = [];
for(let i = 0 ; i < arr.length-1 ; i++){
if(arr[i] !== arr[i+1]){
text = arr[i] + ' and ' + arr[i+1];
newArr.push(text)
}
}
return newArr
};
console.log(check([ 'A', 'A', 'M', 'Y', 'I', 'W', 'W', 'M', 'R', 'Y' ]))
// output "A and M", "A and Y", "I and W", "W and M", "R and Y"]
console.log(check([ 'a', 'b', 'j', 'j', 'i', 't' ]))
my result here is not i want, it was repeated the data which i already push . in newArr
i want the ouput like this :
["A and M", "A and Y", "I and W", "W and M", "R and Y"]
because of each array not the same initial,
i hope this question makes sense
You can do the following :
function check(arr) {
let text = "";
let newArr = [];
for (let i = 0, next = 0; i < arr.length; i++) {
if (next == 2) {
next = 0;
i += 2;
}
if (arr[i + 1] !== undefined) {
if (arr[i + 2] !== undefined) {
text = arr[i] + ' and ' + arr[i + 2];
} else {
text = arr[i] + ' and ' + arr[i + 1];
}
newArr.push(text)
}
next += 1;
}
return newArr
};
console.log(check(['A', 'A', 'M', 'Y', 'I', 'W', 'W', 'M', 'R', 'Y']))
console.log(check(['a', 'b', 'j', 'j', 'i', 't']))

Avoid adding element to array if it's been added before

I have a very hard time formulating my question, I hope you guys understand what I mean.
Is there a way to make sure elements never end up together again?
Example, If A, B, C and D has been in the same new array before, they shouldn't be able to be together again.
This is my code,
let xGrps = 3; // Number of groups
let xSize = 4; // Number of items per group
let allItems = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L']; // All Items
let newGrps = []; // New groups
for (let i = 0; i < xGrps; i++) {
let tempGrp = "";
for(let j = 0; j < xSize; j++) {
tempGrp += allItems[0] + ",";
allItems.shift();
}
newGrps[i] = tempGrp;
console.log(newGrps[i]);
}
I belive I should use something like this maybe,
let blockGrps = [{
A: ['B'],
B: ['C','D'],
C: ['E'],
// and so on
}];

Fetching JavaScript array elements after consecutive occurrence of an element

I have a JavaScript array like:
var myArray = ['a', 'x', 'b', 'x', 'x', 'p', 'y', 'x', 'x', 'b', 'x', 'x'];
I want to fetch only those elements of the array that come after 2 consequent occurrences of a particular element.
i.e. in the above array, I want to fetch all the elements that come after consequent 'x', 'x'
So my output should be:
'p'
'b'
I have a solution like :
var arrLength = myArray.length;
for (var i = 0; i < arrLength; i++) {
if(i+2 < arrLength && myArray[i] == 'x' && myArray[i+1] == 'x') {
console.log(myArray[i+2]);
}
};
This satisfies my needs, but it is not so generic.
For eg. if I have to check for 3 consequent occurrences, then again I have to add a condition inside if for myArray[i+2] == 'x' and so on.
Could anyone provide a better way to fetch the elements?
The functional way would be to use recursion. With an ES6 spread, you can pretty much emulate the terseness of a truly 'functional' language :-)
var myArray = ['a', 'x', 'b', 'x', 'x', 'p', 'y', 'x', 'x', 'b', 'x', 'x'];
function reducer(acc, xs) {
if (xs.length > 2) {
if (xs[0] === xs[1]) {
// add the third element to accumulator
// remove first three elements from xs
// return reducer([xs[2], ...acc], xs.slice(3));
// or per Nina's question below
return reducer([xs[2], ...acc], xs.slice(1));
} else {
// remove first element from xs and recurse
return reducer(acc, xs.slice(1))
}
} else {
return acc;
}
}
console.log(reducer([], myArray));
A generic straight forward approach for any comparable content.
function getParts(array, pattern) {
return array.reduce(function (r, a, i) {
i >= pattern.length && pattern.every(function (b, j) {
return b === array[i + j - pattern.length];
}) && r.push(a);
return r;
}, []);
}
function p(o) {
document.write('<pre>' + JSON.stringify(o, 0, 4) + '</pre>');
}
p(getParts(['a', 'x', 'x', 'x', 'x', 'p', 'y', 'x', 'x', 'b', 'x', 'x'], ['x', 'x']));
p(getParts(['a', 'x', 'b', 'x', 'x', 'p', 'y', 'x', 'x', 'b', 'x', 'x'], ['a', 'x', 'b']));
p(getParts(['a', 'b', 'c', 'd', 'z', 'y', 'a', 'b', 'c', 'd', 'x', 'x'], ['a', 'b', 'c', 'd']));
p(getParts([41, 23, 3, 7, 8, 11, 56, 33, 7, 8, 11, 2, 5], [7, 8, 11]));
You can try following logic
var myArray = ['a', 'x', 'b', 'x', 'x', 'p', 'y', 'x', 'x', 'b', 'x', 'x'];
function search(ch, times) {
var splitStr = "";
for(var i = 0; i < times; i++) {
splitStr += ch;
} // Generate the split string xx in the above case.
var str = myArray.join(''); // Join array items into a string
var array = str.split(splitStr); // Split the string based on split string
var result = {};
// iterate on the array starting from index 1 as at index 0 will be string before split str
for (var i = 1 ; i < array.length; i++) {
if(array[i] !== "") {
result[array[i].substring(0,1)] = ''; // A map in order to avoid duplicate values
}
}
return Object.keys(result); // return the keys
}
console.dir(search('x',2));
Here is a straightforward iterative solution. We maintain an array consecutive of consecutive elements. If that array gets to length 2, then the next element is printed and consecutive is reset.
var arr = ['a', 'x', 'b', 'x', 'x', 'p', 'y', 'x', 'x', 'b', 'x', 'x'];
var REPEATS_NEEDED = 2;
var consecutive = [arr[0]];
for (var i = 1; i < arr.length; i++) {
if (consecutive.length === REPEATS_NEEDED) {
console.log(arr[i]);
consecutive = [arr[i]];
continue;
}
// either add to or reset 'consecutive'
if (arr[i] === consecutive[0]) {
consecutive.push(arr[i]);
} else {
consecutive = [arr[i]];
}
};
You can create an additional function isItGood like this:
var myArray = ['a', 'x', 'b', 'x', 'x', 'p', 'y', 'x', 'x', 'b', 'x', 'x'];
var arrLength = myArray.length;
for (var i = 0; i < arrLength; i++) {
isItGood(myArray, i, 'x', 2);
};
function isItGood(arr, i, elem, total) {
for ( var j = 0 ; j < total ; j++ ) {
if ( i + total >= arr.length || arr[i+j] != elem ) {
return;
}
}
console.log(arr[i+total]);
// just to see the result (no need to open a console)
document.getElementById('p').innerHTML+=("<br/>"+arr[i+total]);
}
<p id="p">Result: </p>
If I had to write this in Scala instead of JavaScript I could just do it in one line.
myArray.sliding(3).filter(l => l(0) == 'x' && l(1) == 'x').map(l => l(2))
So I guess I could do it the same way in JS if I implement the sliding function myself.
e.g.
function sliding(array, n, step) {
if(!step) step = 1;
var r = [];
for(var i = 0; i < array.length - n + 1; i += step) {
r.push(array.slice(i, i + n));
}
return r;
}
var result = sliding(myArray, 3).filter(l => l[0] === "x" && l[1] === "x").map(l => l[2]);
The only downside here is that this runs slower than a more iterative approach. But that only matters for very big arrays.
Try using for loop using variables referencing previous index, current index, next index of array
var myArray = ["a", "x", "b", "x", "x", "p", "y", "x", "x", "b", "x", "x"];
for (var res = [], curr = 0, prev = curr - 1, match = curr + 1
; curr < myArray.length - 1; curr++, prev++, match++) {
if (myArray[curr] === myArray[prev]) res.push(myArray[match]);
};
console.log(res);
document.body.textContent = res;

Categories