i require matrixo function that in random.js, but in server.js but program cant find this function. Where do i need require random .js for fixing?
server.js
var matrix = matrixo(40, 40);
let random = require('./modules/random.js');
random.js
function matrixo(m) {
var matrix = [];
for (var i = 0; i < m; i++) {
matrix.push([]);
for (var j = 0; j < m; j++) {
matrix[i][j] = Math.floor(Math.random() * 3);
}
for (var j = 0; j < m; j++) {
matrix[i][j] = Math.floor(Math.random() * 4);
}
for (var j = 0; j < m + 3; j++) {
matrix[i][j] = Math.floor(Math.random() * 5);
}
for (var j = 0; j < m; j++) {
matrix[i][j] = Math.floor(Math.random() * 6);
}
for (var j = 0; j < m; j++) {
matrix[i][j] = Math.floor(Math.random() * 7);
}
}
return matrix;
}
module.exports = matrixo;
error - ReferenceError: matrixo is not defined
You need to assign the return value (which is the exported value) to the variable you are trying to use, and you need to do it before you use that variable.
let matrixo = require('./modules/random.js');
var matrix = matrixo(40, 40);
Related
When I run the third line alone and log test2darr it returns a 2D array filled with 6's in a 3x3 matrix
But when I run the fourth line and log test2darr again, it returns:
[4, 5, 4]
[5, 6, 5]
[4, 5, 4]
(as well as for secondtest)
Though it should return the same array of 6's for test2darr and on assign the 2d array to secondtest
const n = 3;
const filler = new Array(n * n);
const test2darr = fill2DarrFromArr(filler.fill(6));
const secondtest = pileReduce(test2darr);
Here is my code for fill2DarrFromArr and pileReduce:
function pileReduce(_cells) {
_cells = fillEmpty(_cells);
for (let j = 0; j < _cells.length; j++) { //The Algorithm itself is not important
for (let i = 0; i < _cells.length; i++) { // But there might be some assignment problem that I missed
if (_cells[j][i] >= 4) {
_cells[j][i] = _cells[j][i] - 4;
if (j !== _cells.length - 1) _cells[j + 1][i]++;
if (j !== 0) _cells[j - 1][i]++;
if (i !== _cells.length - 1) _cells[j][i + 1]++;
if (i !== 0) _cells[j][i - 1]++;
}
}
}
return _cells;
}
function fill2DarrFromArr(_arr) {
let sideLength = Math.sqrt(_arr.length);
let out = create2DArr(sideLength, sideLength);
for (let j = 0; j < sideLength; j++) {
for (let i = 0; i < sideLength; i++) {
out[j][i] = _arr[j * sideLength + i];
}
}
return out;
}
function create2DArr(_n, _m) {
let _arr = new Array(_n);
for (let j = 0; j < _m; j++) {
_arr[j] = new Array(_m);
}
return _arr;
}
function fillEmpty(_arr) {
for (let j = 0; j < _arr.length; j++) {
for (let i = 0; i < _arr.length; i++) {
if (!_arr[j][i]) _arr[j][i] = 0;
}
}
return _arr;
}
Passing an array into a function doesn't create a copy of that array. Your functions are modifying the contents of the passed arrays, therefore they have side effects.
I would like align row in function of the Name (column B & D), for exemple on the picture (1) the 2 red rectangles should be on the same line like for the 2 green rectangles but I don't know why, it doesn't work, here the code :
function onOpen(event){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("test1");
var data = sheet.getDataRange().getValues();
for(var j = 1; j < data.length; j++) {
if(data[j][1].trim() != '') {
for (var i = 1; i < data.length; i++) {
if(data[i][3].trim() != '') {
if(data[j][1] == data[i][3]) {
if(j != i) {
var j1 = j + 1;
var i1 = i + 1;
sheet.getRange('D'+j1+':E'+j1).moveTo(sheet.getRange('F1:G1'));
sheet.getRange('D'+i1+':E'+i1).moveTo(sheet.getRange('D'+j1+':E'+j1));
sheet.getRange('F1:G1').moveTo(sheet.getRange('D'+i1+':E'+i1));
}
break;
}
}
}
}
}
}
I'm pretty sure this code should work
Oddly it works if I'm running the code step by step like this, firstly :
for(var j = 1; j < 2; j++)
after
for(var j = 2; j < 3; j++)
after
...
until
for(var j = 6; j < 7; j++)
Well I found a workaround :)
function onOpen(event){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("test1");
var data = sheet.getDataRange().getValues();
var blank = 0;
// I count the blank case so I don't run the code effortlessly on blank case
for(var j = 1; j < data.length; j++) {
if(data[j][1].trim() == '')
blank++;
}
var length = data.length - blank;
// I don't make a real switch in fact, I'm using a tempory space where I order the data
for(var j = 1; j < length; j++) {
for (var i = 1; i < length; i++) {
if(data[j][1] === data[i][3]) {
var j1 = j + 1;
var i1 = i + 1;
sheet.getRange('D'+i1+':E'+i1).moveTo(sheet.getRange('F'+j1+':G'+j1)); // column F & G is the tempory space where I put the ordered data
break;
}
}
}
// I move back the tempory space ordered now to his initial place
sheet.getRange('F2:G'+length).moveTo(sheet.getRange('D2:E'+length));
}
The algorithm is taken from LeetCode: https://leetcode.com/problems/maximum-product-of-word-lengths/description/
Here is the jsperf I created (I have some local tests which gives the same result): https://jsperf.com/maximum-product-of-word-lengths
Here is the first "slow" implementation:
function maxProduct (words) {
if (!words || !words.length) return 0;
let len = words.length;
let values = [];
// console.log(values)
for (let i = 0; i < len; ++i) {
let tmp = words[i];
let num = 0, len = tmp.length;
for (let j = 0; j < len; ++j) {
num |= 1 << (tmp.charCodeAt(j) - 'a'.charCodeAt(0));
}
values[i] = {
num: num,
len: tmp.length
};
}
let maxProduct = 0;
for (let i = 0; i < len; ++i) {
for (let j = 0; j < len; ++j) {
if ((values[i].num & values[j].num) == 0) {
maxProduct = Math.max(maxProduct, values[i].len * values[j].len);
}
}
}
return maxProduct;
};
Here is the "fast" implementation:
function maxProductFast (words) {
var temp = [];
for(var i = 0; i < words.length; i++){
var tempObj = {};
tempObj.item = words[i];
var num = 0;
for(var j = 0; j < words[i].length; j++){
num |= 1 << (words[i].charCodeAt(j) - 97);
}
tempObj.num = num;
temp.push(tempObj);
}
var res = 0;
for(var i = 0; i < temp.length; i++){
for(var j = i + 1; j < temp.length; j++){
var item1 = temp[i];
var item2 = temp[j];
if((item1.num & item2.num) == 0) {
res = Math.max(res, item1.item.length * item2.item.length);
}
}
}
return res;
}
They're not the same. The second algorithm has a loop with a complexity of (n*(n+1))/2 where each progressive step is from i+1 to the length of temp. the first algorithm has a two nested for loops each with a cost of n^2. the complexity of both will reduce to O(n^2). I believe that both of these will have a similar performance with a significantly large enough set.
The reason you would do n+1 for each sub iteration is because you are trying to find the max of any pair of items. if you place your elements in a grid you will notice that any diagonal pair a_3 * a_2 = a_2 * a_3 produces the same value. you can basically halve the collection and save a few cycles.
I can't understand why does it say it will create an infinite loop.It doesn't seem like it will.What's the problem?
function smallestCommons(arr) {
var max = arr.reduce(function (a, b) {
return Math.max(a, b);
});
var min = arr.reduce(function (a, b) {
return Math.min(a, b);
});
var allNums = [];
for (var i = min; i <= max; i++) {
for (var j = 1; j <= (max ^ 3); i++) {
allNums.push(i * j);
}
}
}
smallestCommons([1, 6]);
for(var j=1;j<=(max^3);i++)
Should be
for(var j=1;j<=(max^3);j++)
Looks like j is never increasing so it always matches the condition in j<=(max^3) inside your for loop.
I think there is a typo:
for(var j=1;j<=(max^3);i++) {
should be
for (var j=1; j <= (max^3); j++) {
You have to change for(var j = 1 ; j <= (max^3) ; i++) to for(var j = 1 ; j <= (max^3) ; j++)
Your nested for loop never increments your var j.
for (var i = min; i <= max; i++) {
for (var j = 1; j <= (max ^ 3); j++) {
allNums.push(i * j);
}
}
I'm trying to print all prime number between 0 and 100, but when executing this code the browser's tab just outputs nothing!!
for(var i = 2; i < 100; i++)
{
var prime = [];
for(var j = 0; j <= i; j++)
{
var p = i % j;
}
if(p != 0) prime.push(i);
else continue;
}
for(var k = 0; k < prime.length; k++)
{
document.writeln(prime[k], "<br>");
}
Try this one. I have also optimise the code (you only need to check upto sqrt(i) ).
var prime = [];
prime.push(2); //smallest prime
var flag = 0;
for(var i = 3; i < 100; i=i+2) //skip all even no
{
for(var j = 3; j*j <= i; j=j+2) //check by upto sqrt(i), skip all even no
{
if(i % j == 0) {
flag = 0;break; //not a prime, break
}
flag = 1;
}
if (flag == 1) prime.push(i); //prime, add to answer
}
for(var k = 0; k < prime.length; k++)
{
document.writeln(prime[k], "<br>");
}
Because you blank your list of primes EVERY loop cycle, move it outside the for loop
You need to make your variable prime outside of your loop
This is the code you have re-written
var prime = [];
for(var i = 2; i < 100; i++)
{
for(var j = 0; j <= i; j++)
{
var p = i % j;
}
if(p != 0) prime.push(i);
else continue;
}
for(var k = 0; k < prime.length; k++)
{
document.writeln(prime[k], "<br>");
}
I'm a fan of the sieve of Eratosthenes.
The following code should do what you wanted.
var prime = Array(101).fill(true);
for (var i = 2; i < 100; ++i){
if (prime[i]){
document.writeln(i, "<br>");
for (var j = i*i; j < 100; j += i){
prime[j] = false;
}
}
}
Or since it's only up to 100 you could just manually type the list (but, hey where's the learning if you do it that way?).
(1) Move prime outside the for loop, (2) start j at 2 and end when j < i, (3) check when p == 0 with a boolean flag and break inner loop.
var prime = []; //put prime out here so it does not reassign
for(var i = 2; i < 100; i++)
{
var isPrime = true;
for(var j = 2; j < i; j++) //start j at 2
{
var p = i % j;
if(p == 0)
{
isPrime = false;
break;
}
}
if(isPrime) prime.push(i);
}
for(var k = 0; k < prime.length; k++)
{
document.writeln(prime[k], "<br>");
}