I am a beginner and I've found some useful examples for what I want to do. The problem is the examples that I've found don't have enough comments for me to understand what is going on. So, I hope someone can help me implement the code I've found into the code that I already have. I'm making a text manipulation area to use to play with cipher text. It's all being done inside a single HTML text area. I've got a functions called, "function G_Group(size, count)", that breaks the text into rows and columns of choice and it is working great. The next tool that I want to add will transpose this matrix from (x,y) to (y,x). Because I have the "function G-Group" function, I don't believe I need to slice anything. I found a bit of JavaScript transposition code at http://rosettacode.org/wiki/Matrix_transposition#JavaScript but I don't know how to change the values to add it to what I've got already.
Function G_Group(size, count) is called like this.
<input type= button value="Grouping" onclick = "return G_Group(0, 0)" title="grouping" />
And here is the how i break text up into rows and columns:
function G_Group(size, count)
{
if (size <= 0)
{
size = document.encoder.group_size.value;
if (size <= 0)
{
alert('Invalid group size');
return false;
}
}
if (count <= 0)
{
count = document.encoder.group_count.value;
if (count <= 0)
{
alert('Invalid group count');
return false;
}
}
var t = document.encoder.text.value;
var o = '', groups = 0;
t = Tr(t, " \r\n\t");
while (t.length > 0)
{
if (o.length > 0)
{
o += ' ';
}
if (groups >= count)
{
o += "\n";
groups = 0;
}
groups ++;
o += t.slice(0, size);
t = t.slice(size, t.length);
}
document.encoder.text.value = o;
return false;
}
And this is the code that I want modify to transpose the array.
function Matrix(ary) {
this.mtx = ary
this.height = ary.length;
this.width = ary[0].length;
}
Matrix.prototype.toString = function() {
var s = []
for (var i = 0; i < this.mtx.length; i++)
s.push( this.mtx[i].join(",") );
return s.join("\n");
}
// returns a new matrix
Matrix.prototype.transpose = function() {
var transposed = [];
for (var i = 0; i < this.width; i++) {
transposed[i] = [];
for (var j = 0; j < this.height; j++) {
transposed[i][j] = this.mtx[j][i];
}
}
return new Matrix(transposed);
}
I am aware that I may be approaching this all wrong. And I'm aware that the questions I have are very basic, I'm a little embarrassed to ask these simple questions. Please excuse me. I'm 43 years old and had c programming in college 20 years ago. I'm pretty good with HTML and CSS but I'm lacking in a lot of areas. Hope someone can help me with this. Thanks.
Related
This is my first ever question, please give me feedback if I suck at this. Thank you
I am trying to create a JavaScript terminal game from a paid course. The game is called 'Find My Hat'.
One section of the problem requires me to create a static method called generateField(). This method generates a field, on the field there are 4 possible characters:
hat = '^';
hole = 'O';
fieldCharacter = '░';
pathCharacter = '*';
The fieldCharacter is the background for the game area. The game area is comprised of nested arrays. I haven't had problems creating the field, but I also want to replace random fieldCharacters with holes. I tried this with Math.random and nested loops to iterate over the nested arrays. I have not been able to figure out how to get this to work.
Here is the code below, sorry if I missed any details, I will try respond to everyone I can.
class Field {
constructor(field) {
this._field = field;
}
print() {
this._field = field.join('\n');
console.log(this._field.replace(/,/g, ''))
}
static generateField(height, width) {
let finalArray = []
for (let i = 0; i < height; i++) {
finalArray.push([fieldCharacter.repeat(width)]);
}
for (let i = 0; i < finalArray.length; i++) {
let randomHole = () => {
let result = Math.random() * width;
return result;
}
for (let j = 0; j < finalArray[i].length; j++) {
if (randomHole() > width - 2) {
finalArray[i][j] = hole;
}
}
}
return finalArray.join('\n').replace(/,/g, '');
}
}
console.log(Field.generateField(5, 10));
One Random Output:
O
O
░░░░░░░░░░
░░░░░░░░░░
O
Example for good output:
░░░░O░░░░░
░░░O░OO░░░
░░O░░░O░░░
░░░░O░░░░░
OO░░░░O░░O
I did some changes to your code and use the stackoverflow snippet to show you how it works
String.prototype.replaceAt = function(index, replacement) {
return this.substr(0, index) + replacement + this.substr(index + replacement.length);
}
hat = '^';
hole = 'O';
fieldCharacter = '░';
pathCharacter = '*';
class Field {
constructor(field) {
this._field = field;
}
print() {
this._field = field.join('\n');
console.log(this._field.replace(/,/g, ''))
}
static generateField(height, width) {
let finalArray = []
for (let i = 0; i < height; i++) {
finalArray.push(fieldCharacter.repeat(width));
}
//i dont know why you did this loop but i changed the inside for marking random holes
for (let i = 0; i < finalArray.length; i++) {
//pick random x and y
let randomHoleX = Math.floor(Math.random() * width);
let randomHoleY = Math.floor(Math.random() * height);
//becouse you save array of strings you need to find the correct array and replace the char at the correct index.
finalArray[randomHoleY] = finalArray[randomHoleY].replaceAt(randomHoleX, hole);
}
return finalArray.join('\n').replace(/,/g, '');
}
}
console.log(Field.generateField(5, 10));
I feel stupid because I´m stuck with a basic. I have three set of classes that contains paragraphs and I want to change the background color of each one depending on day (with New Dat.getDay().
I dont know how to mix for loop and if statements for each set of classes correctly. I guess it´s something simple but I missing it!
function changecolor() {
var d = new Date();
var n = d.getDay();
var weekda = document.getElementsByClassName('weekdays');
var sat = document.getElementsByClassName('saturday');
var dom = document.getElementsByClassName('sun-fer');
for (var i = 0; i < weekda.length && i < sat.length && i < dom.length; i++)
if (n > 0 || n < 6) {
weekda[i].setAttribute("style", "background-color:#0091ea;color:white;");
}
else if (n == 6) {
sat[i].setAttribute("style", "background-color:#0091ea;color:white;");
} else {
dom[i].setAttribute("style", "background-color:#0091ea;color:white;");
}
}
}
changecolor();
You need to group conditions. Read more about Operator precedence
for (var i = 0;( (i < weekda.length) && (i < sat.length) && (i < dom.length)); i++){
// your code
}
Part of the problem may be you have background-color:#0091ea;color:white; for all three options. Therefore you may not be seeing any change.
Personally I would break this up a little to make it more flexible and a little easier to read (and maintain). For example:
function changecolor() {
var d = new Date();
var e = null;
var s = null;
switch(d.getDay()) {
case 6:
e = document.getElementsByClassName('saturday');
s = "background-color:#0091ea;color:white;";
break;
case 0:
e = document.getElementsByClassName('sun-fer');
s = "background-color:#0091ea;color:green;";
break;
default:
e = document.getElementsByClassName('weekdays');
s = "background-color:#0091ea;color:blue;";
}
// now update the color
updateItem(e,s);
}
function updateItem(e,s) {
var i, max = e.length;
for(i=0;i<max;i++) {
e[i].setAttribute("style",s);
}
}
I'm trying to create a simulation of 150 mouses moving inside a 20x20 grid in p5.js (A processing like libary). First I'm spawning 150 mouses random places and everything goes fine. But after I have spawned the mouses I am trying to make them move to one of their neighbors. Instead of moving to one of the neighbors and make the current square empty it stays on the one that it already was one + it moves to the next one so instead of having 150 mouses i suddenly have 300... I have tried changing the code for hours but I can't find the proplem... Here is my code:
var w = 40;
var grid = [];
var mouses = 10;
var mouseAmount = [];
var Mouse;
var current;
function setup() {
createCanvas(800, 800);
cols = floor(width/w)
rows = floor(height/w)
// frameRate(60);
for (var j = 0; j < rows; j++) {
for ( var i = 0; i < cols; i++) {
var cell = new Cells(i,j);
grid.push(cell);
}
}
amount = new Amount;
}
function draw() {
background(51);
for ( var i = 0; i < grid.length; i++) {
grid[i].show();
}
amount.run();
}
function index(i, j) {
if (i < 0 || j < 0 || i > cols-1 || j > rows-1 ) {
return -1;
}
return i + j * cols;
}
function Cells(i, j) {
this.i = i;
this.j = j;
this.active = false;
this.moveCell = function() {
var neighbors = [];
var top = grid[index(i, j -1)];
var right = grid[index(i+1, j)];
var bottom = grid[index(i, j+1)];
var left = grid[index(i-1, j)];
if (top) {
neighbors.push(top)
}
if (right) {
neighbors.push(right)
}
if (bottom) {
neighbors.push(bottom)
}
if (left) {
neighbors.push(left)
}
if(neighbors.length > 0) {
var r = floor(random(0, neighbors.length));
return neighbors[r];
} else {
return undefined;
}
}
this.show = function() {
var x = this.i*w;
var y = this.j*w;
stroke(255);
noFill();
rect(x,y,w,w);
if(this.active == true) {
fill(155, 0, 255, 100)
rect(x, y, w, w)
}
}
}
function Amount() {
this.run = function() {
var r = floor(random(grid.length))
for (var i = 0; i < mouses; i++) {
var mouse = grid[r];
mouseAmount.push(mouse)
}
if (mouseAmount.length < 1499) {
for (var i = 0; i < mouseAmount.length; i++) {
mouseAmount[i].active = true;
}
}
if (mouseAmount.length > 1499) {
Next();
}
}
}
function Next(i,j) {
for (var i = 0; i < mouseAmount.length; i++) {
current = mouseAmount[i];
var nextCell = current.moveCell();
if (nextCell) {
nextCell.active = true;
current.active = false;
current = nextCell;
}
}
}
Thank you in advance :)
I don't really understand exactly what your code is supposed to do, but a few things stand out to me about your code:
Problem One: I don't understand how you're iterating through your grid array. You seem to be iterating over mouseAmount, which seems to hold random cells from the grid for some reason? That doesn't make a lot of sense to me. Why don't you just iterate over the grid array directly?
Problem Two: You then move the cells randomly to a neighbor, but you don't take into account whether the neighbor is already active or not. I'm not sure what you want to happen, but this seems a bit strange.
Problem Three: Usually with simulations like this, you have to copy the next generation into a new data structure instead of modifying the data structure as you step through it.
The biggest problem is that you haven't really explained what you want your code to do, or what this code does instead, or how those two things are different. But if I were you, I'd make the following changes:
Step One: Iterate over your grid array in a more reasonable way. Just iterate over every index and take the appropriate action for every cell. If I were you I would just use a 2D array and use a nested for loop to iterate over it.
Step Two: Make sure your logic for moving to a neighbor is correct. Do you want cells to move to already active cells?
Step Three: Make a copy of the grid before you modify it. Think about it this way: as you iterate over the grid, let's say you move a cell down one row. Then you continue iterating, you'll reach the newly active cell again. In other words, you'll touch the same active cell twice in one generation, which is definitely going to mess you up.
A word of advice: get this working for a single active cell first. It's really hard to tell what's going on since you have so many things going on at one time. Take a step back and make sure it works for one active cell before moving up to having a whole grid.
I'm trying do make a random post widget to work on my blog (blogger), but the browser keeps giving me an error saying that the script is unresponsive:
"A script on this page may be busy, or it may have stopped responding. You can stop the script now, open the script in the debugger, or let the script continue.
Script: http://testeshoon.blogspot.pt/:4792"
The script came with the template.
Have tried other scripts from the web but they all do the same.
I'm trying to solve this for the past 2 days, but can't figure out what the problem is.
Can anyone help me?
Really need this to work, at least, in 2 widgets.
Tried scripts for recent posts and those work just fine, but the ones for random doesn't.
<ul id='random-posts'>
<script type='text/javaScript'>
var rdp_numposts = 2;
var rdp_snippet_length = 100;
var rdp_info = 'yes';
var rdp_comment = 'Comments';
var rdp_disable = 'Comments Disabled';
var rdp_current = [];
var rdp_total_posts = 0;
var rdp_current = new Array(rdp_numposts);
function totalposts(json) {
rdp_total_posts = json.feed.openSearch$totalResults.$t
}
document.write('<script type=\"text/javascript\" src=\"/feeds/posts/default/-/noticias?alt=json-in-script&max-results=0&callback=totalposts\"><\/script>');
function getvalue() {
for (var i = 0; i < rdp_numposts; i++) {
var found = false;
var rndValue = get_random();
for (var j = 0; j < rdp_current.length; j++) {
if (rdp_current[j] == rndValue) {
found = true;
break
}
};
if (found) {
i--
} else {
rdp_current[i] = rndValue
}
}
};
function get_random() {
var ranNum = 1 + Math.round(Math.random() * (rdp_total_posts - 1));
return ranNum
};
</script>
<script type='text/javaScript'>
var _0x3eb4=["\x65\x6E\x74\x72\x79","\x66\x65\x65\x64","\x24\x74","\x74\x69\x74\x6C\x65","\x63\x6F\x6E\x74\x65\x6E\x74","\x73\x75\x6D\x6D\x61\x72\x79","","\x72\x65\x70\x6C\x61\x63\x65","\x6C\x65\x6E\x67\x74\x68","\x73\x75\x62\x73\x74\x72\x69\x6E\x67","\x20","\x6C\x61\x73\x74\x49\x6E\x64\x65\x78\x4F\x66","\x26\x23\x31\x33\x33\x3B","\x6C\x69\x6E\x6B","\x74\x68\x72\x24\x74\x6F\x74\x61\x6C","\x72\x65\x6C","\x61\x6C\x74\x65\x72\x6E\x61\x74\x65","\x68\x72\x65\x66","\x70\x75\x62\x6C\x69\x73\x68\x65\x64","\x6D\x65\x64\x69\x61\x24\x74\x68\x75\x6D\x62\x6E\x61\x69\x6C","\x75\x72\x6C","\x68\x74\x74\x70\x3A\x2F\x2F\x33\x2E\x62\x70\x2E\x62\x6C\x6F\x67\x73\x70\x6F\x74\x2E\x63\x6F\x6D\x2F\x2D\x35\x53\x6F\x56\x65\x31\x4B\x36\x4A\x53\x6B\x2F\x55\x74\x6C\x30\x4F\x4F\x6D\x75\x63\x41\x49\x2F\x41\x41\x41\x41\x41\x41\x41\x41\x46\x36\x45\x2F\x68\x51\x67\x68\x67\x44\x5F\x45\x4A\x64\x51\x2F\x73\x31\x36\x30\x30\x2F\x6E\x6F\x5F\x74\x68\x75\x6D\x62\x2E\x70\x6E\x67","\x3C\x6C\x69\x3E","\x77\x72\x69\x74\x65","\x3C\x61\x20\x68\x72\x65\x66\x3D\x22","\x22\x20\x72\x65\x6C\x3D\x22\x6E\x6F\x66\x6F\x6C\x6C\x6F\x77\x22\x3E\x3C\x69\x6D\x67\x20\x61\x6C\x74\x3D\x22","\x22\x20\x73\x72\x63\x3D\x22","\x22\x2F\x3E\x3C\x2F\x61\x3E","\x3C\x64\x69\x76\x3E\x3C\x61\x20\x68\x72\x65\x66\x3D\x22","\x22\x20\x72\x65\x6C\x3D\x22\x6E\x6F\x66\x6F\x6C\x6C\x6F\x77\x22\x20\x74\x69\x74\x6C\x65\x3D\x22","\x22\x3E","\x3C\x2F\x61\x3E\x3C\x2F\x64\x69\x76\x3E","\x79\x65\x73","\x3C\x73\x70\x61\x6E\x3E\x3C\x64\x69\x76\x20\x20\x63\x6C\x61\x73\x73\x3D\x22\x72\x70\x2D\x69\x6E\x66\x6F\x22\x3E","\x2F","\x20\x2D\x20","\x3C\x2F\x64\x69\x76\x3E\x3C\x2F\x73\x70\x61\x6E\x3E","\x3C\x62\x72\x2F\x3E\x3C\x64\x69\x76\x20\x63\x6C\x61\x73\x73\x3D\x22\x72\x70\x2D\x73\x6E\x69\x70\x70\x65\x74\x22\x3E","\x3C\x2F\x64\x69\x76\x3E\x3C\x64\x69\x76\x20\x73\x74\x79\x6C\x65\x3D\x22\x63\x6C\x65\x61\x72\x3A\x62\x6F\x74\x68\x22\x3E\x3C\x2F\x64\x69\x76\x3E\x3C\x2F\x6C\x69\x3E","\x3C\x73\x63\x72\x69\x70\x74\x20\x74\x79\x70\x65\x3D\x22\x74\x65\x78\x74\x2F\x6A\x61\x76\x61\x73\x63\x72\x69\x70\x74\x22\x20\x73\x72\x63\x3D\x22\x2F\x66\x65\x65\x64\x73\x2F\x70\x6F\x73\x74\x73\x2F\x64\x65\x66\x61\x75\x6C\x74\x3F\x61\x6C\x74\x3D\x6A\x73\x6F\x6E\x2D\x69\x6E\x2D\x73\x63\x72\x69\x70\x74\x26\x73\x74\x61\x72\x74\x2D\x69\x6E\x64\x65\x78\x3D","\x26\x6D\x61\x78\x2D\x72\x65\x73\x75\x6C\x74\x73\x3D\x31\x26\x63\x61\x6C\x6C\x62\x61\x63\x6B\x3D\x72\x61\x6E\x64\x6F\x6D\x5F\x70\x6F\x73\x74\x73\x22\x3E\x3C\x2F\x73\x63\x72\x69\x70\x74\x3E"];function random_posts(_0x2f3cx2){for(var i=0;i<rdp_numposts;i++){var _0x2f3cx4=_0x2f3cx2[_0x3eb4[1]][_0x3eb4[0]][i];var _0x2f3cx5=_0x2f3cx4[_0x3eb4[3]][_0x3eb4[2]];if(_0x3eb4[4] in _0x2f3cx4){var _0x2f3cx6=_0x2f3cx4[_0x3eb4[4]][_0x3eb4[2]];} else {if(_0x3eb4[5] in _0x2f3cx4){var _0x2f3cx6=_0x2f3cx4[_0x3eb4[5]][_0x3eb4[2]];} else {var _0x2f3cx6=_0x3eb4[6];} ;} ;_0x2f3cx6=_0x2f3cx6[_0x3eb4[7]](/<[^>]*>/g,_0x3eb4[6]);if(_0x2f3cx6[_0x3eb4[8]]<rdp_snippet_length){var _0x2f3cx7=_0x2f3cx6;} else {_0x2f3cx6=_0x2f3cx6[_0x3eb4[9]](0,rdp_snippet_length);var _0x2f3cx8=_0x2f3cx6[_0x3eb4[11]](_0x3eb4[10]);_0x2f3cx7=_0x2f3cx6[_0x3eb4[9]](0,_0x2f3cx8)+_0x3eb4[12];} ;for(var _0x2f3cx9=0;_0x2f3cx9<_0x2f3cx4[_0x3eb4[13]][_0x3eb4[8]];_0x2f3cx9++){if(_0x3eb4[14] in _0x2f3cx4){var _0x2f3cxa=_0x2f3cx4[_0x3eb4[14]][_0x3eb4[2]]+_0x3eb4[10]+rdp_comment;} else {_0x2f3cxa=rdp_disable;} ;if(_0x2f3cx4[_0x3eb4[13]][_0x2f3cx9][_0x3eb4[15]]==_0x3eb4[16]){var _0x2f3cxb=_0x2f3cx4[_0x3eb4[13]][_0x2f3cx9][_0x3eb4[17]];var _0x2f3cxc=_0x2f3cx4[_0x3eb4[18]][_0x3eb4[2]];if(_0x3eb4[19] in _0x2f3cx4){var _0x2f3cxd=_0x2f3cx4[_0x3eb4[19]][_0x3eb4[20]];} else {_0x2f3cxd=_0x3eb4[21];} ;} ;} ;document[_0x3eb4[23]](_0x3eb4[22]);document[_0x3eb4[23]](_0x3eb4[24]+_0x2f3cxb+_0x3eb4[25]+_0x2f3cx5+_0x3eb4[26]+_0x2f3cxd+_0x3eb4[27]);document[_0x3eb4[23]](_0x3eb4[28]+_0x2f3cxb+_0x3eb4[29]+_0x2f3cx7+_0x3eb4[30]+_0x2f3cx5+_0x3eb4[31]);if(rdp_info==_0x3eb4[32]){document[_0x3eb4[23]](_0x3eb4[33]+_0x2f3cxc[_0x3eb4[9]](8,10)+_0x3eb4[34]+_0x2f3cxc[_0x3eb4[9]](5,7)+_0x3eb4[34]+_0x2f3cxc[_0x3eb4[9]](0,4)+_0x3eb4[35]+_0x2f3cxa)+_0x3eb4[36];} ;document[_0x3eb4[23]](_0x3eb4[37]+_0x2f3cx7+_0x3eb4[38]);} ;} ;getvalue();for(var i=0;i<rdp_numposts;i++){document[_0x3eb4[23]](_0x3eb4[39]+rdp_current[i]+_0x3eb4[40]);} ;
</script>
</ul>
Let's suppose that you want k distinct numbers (random post indexes) from n possible numbers. Here is an implementation
function getRandomSubset(n, k) {
if (k > n) {
//impossible
return;
}
//initializing the result set as empty
var output = [];
//creating a variable
var input;
//we need k distinct random numbers
while (k-- > 0) {
//we get a random integer, 0 <= input < n
input = Math.floor(Math.random() * n);
//if it is duplicated, increment it inside the n'th modulo class until a new value is found
while (output.indexOf(input) >= 0) {
input = (input + 1) % n;
}
//store the new distinct value into the results
output[output.length] = input;
}
return output;
}
There are many other possible ways.
I need to make a grid that looks like this in javascript using one function:
x---------
xx--------
xxx-------
xxxx------
xxxxx-----
xxxxxx----
xxxxxxx---
xxxxxxxx--
xxxxxxxxx-
xxxxxxxxxx
and this:
x---------
xx--------
xxx-------
xxxx------
xxxxx-----
xxxxx-----
xxxx------
xxx-------
xx--------
x---------
//Build Matrix
function initMatrix(max) {
var myMatrix = [];
var i;
for(i = 0; i < row; i++){
myMatrix.push([]);
var j;
for(j = 0; j < col; j++) {
if(j < max) {
myMatrix[myMatrix.length - 1].push('*');
} else {
myMatrix[myMatrix.length - 1].push('-');
}
}
}
return myMatrix;
}
More of this code can be viewed here
https://jsfiddle.net/0yc7acev/
Thanks in advanced for the help!
Hi I've made you this document that explains how to do this and has both solutions: https://tonicdev.com/tonic/stars-example . Let me know if you have any questions!
Edit: I realized after the fact that you wanted them in a matrix, not a string, so I've made a slight modification to put them in a matrix for you: https://tonicdev.com/tonic/stars-matrix (the old one still shows how to put them all in string).
The solution at the end is just:
function drawStars(starCounts)
{
return starCounts.map(function (starCount)
{
return (new Array(starCount + 1).join("*") +
new Array(lineLength - starCount + 1).join("-")).split("")
});
}
where starCounts is an array.