Infinite "for" loop [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm writing code on Javascript for a project breakout game, and right now I'm adding the bricks. Somehow, I've created myself an infinite loop. I know that this is the correct code to at least put the bricks on the screen, but the code simply won't run on my computer, it just breaks. Could someone determine where the infinite loop is created? Thanks!
//Brick Constants
var NUM_ROWS = 8;
var BRICK_TOP_OFFSET = 10;
var BRICK_SPACING = 2;
var NUM_BRICKS_PER_ROW = 10;
var BRICK_HEIGHT = 10;
var SPACE_FOR_BRICKS = getWidth() - (NUM_BRICKS_PER_ROW + 1) * BRICK_SPACING;
var BRICK_WIDTH = SPACE_FOR_BRICKS / NUM_BRICKS_PER_ROW;
//Paddle Constants
var PADDLE_WIDTH = 80;
var PADDLE_HEIGHT = 15;
var PADDLE_OFFSET = 10;
//Ball Constants
var BALL_RADIUS = 15;
var brick;
function start(){
addBricks();
}
function addBricks(){
var brickSepY = BRICK_TOP_OFFSET;
for(var i = 0 ; i < NUM_ROWS ; i++){
var brickSepX = BRICK_SPACING;
for(var j = 0 ; j < NUM_BRICKS_PER_ROW ; i++){
brick = new Rectangle(BRICK_WIDTH, BRICK_HEIGHT);
brick.setPosition(brickSepX, brickSepY);
add(brick);
}
}
}

Change:
for(var j = 0 ; j < NUM_BRICKS_PER_ROW ; i++)
// here ^^^
to:
for(var j = 0 ; j < NUM_BRICKS_PER_ROW ; j++)
// here ^^^

You are increasing the I of the first for change for j
function addBricks(){
var brickSepY = BRICK_TOP_OFFSET;
for(var i = 0 ; i < NUM_ROWS ; i++){
var brickSepX = BRICK_SPACING;
for(var j = 0 ; j < NUM_BRICKS_PER_ROW ; **i++**){
brick = new Rectangle(BRICK_WIDTH, BRICK_HEIGHT);
brick.setPosition(brickSepX, brickSepY);
add(brick);
}
}
}

Related

Can't figure out how to set the values of an array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am new to javascript and trying to loop through an array to set each element equal to a random number. I am wondering why this works:
function randomize() {
for (let i = 1; i < 16; i++) {
let random = Math.floor(Math.random() * 30);
document.getElementById(i).style.paddingTop = random + "%";
}
}
but when I am trying to use an array instead of a variable, doesn't:
function randomize() {
const sortArray = [];
for (let i = 1; i < 16; i++) {
let sortArray[i] = Math.floor(Math.random() * 30);
document.getElementById(i).style.paddingTop = sortArray[i] + "%";
}
}
This is a syntax error. You should remove let in the array[i] declaration
function randomize() {
const sortArray = [];
for (let i = 1; i < 16; i++) {
sortArray[i] = Math.floor(Math.random() * 30);
document.getElementById(i).style.paddingTop = sortArray[i] + "%";
}
}
You are declaring sortArray again.
You have already declared sortArray as
const sortArray=[]; //sort array is already declared here
for (let i = 1; i < 16; i++) {
sortArray[i] = Math.floor(Math.random() * 30); //No need to declare again
document.getElementById(i).style.paddingTop = sortArray[i] + "%";
}}

I am not getting proper output with nested for-loop in javascript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm supposed to get the multiplication value of the multi-dimensional array. but I am getting '1' as output whatever values being changed in array.
function arrayMultiplyer(arr){
var multi = 1;
for(var i=0;i < arr.length; i++){
for(var j =0; j<arr[i];j++){
multi *= arr[i][j];
}
}
return multi;
}
var multi = arrayMultiplyer([[2,33],[33,2],[5,6,9]]);
console.log(multi);
You need to check arr[i].length in the j loop.
function arrayMultiplyer(arr){
var multi = 1;
for(var i=0;i < arr.length; i++){
for(var j =0; j<arr[i].length;j++){ // you need to check arr[i].length here
multi *= arr[i][j];
}
}
return multi;
}
var multi = arrayMultiplyer([[2,33],[33,2],[5,6,9]]);
console.log(multi);
You can simply do this in two lines.
function arrayMultiplyer(arr){
let flattenedArray = arr.flat();
return flattenedArray.reduce((x, y) => x * y);
}
var multi = arrayMultiplyer([[2,33],[33,2],[5,6,9]]);
console.log(multi);
An alternative solution using .reduce()
function arrayMultiplier(arr) {
return arr.reduce((tot,arr2) =>
arr2.reduce((subTot, n) =>
subTot * n
, tot)
, 1);
}

let vs var performance [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I've been reading about ES6 Let keyword vs existing var keyword.
I've got few questions. I understand that "scoping" is the only difference between let and var but what does it mean for the big picture?
function allyIlliterate() {
//tuce is *not* visible out here
for( let tuce = 0; tuce < 5; tuce++ ) {
//tuce is only visible in here (and in the for() parentheses)
};
//tuce is *not* visible out here
};
function byE40() {
//nish *is* visible out here
for( var nish = 0; nish < 5; nish++ ) {
//nish is visible to the whole function
};
//nish *is* visible out here
};
Now my questions:
Does let posses any memory(/performance) advantage over var?
Other than browser support, what are the reasons why i should be using let over var?
Is it safe to start using let now over var in my code workflow?
Thanks,
R
let is much slower than var in node.js. Version v6.3.0 anyway. Sometimes this is dramatic. The code below is about three times slower if you replace var with let:
function collatz() {
var maxsteps = 0;
var maxval = 0;
var x = 1;
var n;
var steps;
while (x < 1000000) {
steps = 0;
n = x;
while (n > 1) {
if (n & 1)
n = 3*n + 1;
else
n = n / 2;
steps += 1;
}
if (steps > maxsteps) {
maxsteps = steps;
maxval = x;
}
x += 1;
}
console.log(maxval + ' - ' + maxsteps + ' steps');
}
collatz();

Project Euler number 8

They asked me to learn JavaScript for work, so I'm doing some of the solutions on Project Euler to help me learn.
My solution for problem 8 works for the 4 number example they give, if I change the 13s to 4s, but it doesn't get the right answer for 13 digits as the question wants.
I solved this problem forever ago in C and don't remember it giving me this much trouble. Am I doing something wrong with JavaScript or is this a error with my code? In either case, please point me in the right direction.
My code:
var numbas = "\
73167176531330624919225119674426574742355349194934\
96983520312774506326239578318016984801869478851843\
85861560789112949495459501737958331952853208805511\
12540698747158523863050715693290963295227443043557\
66896648950445244523161731856403098711121722383113\
62229893423380308135336276614282806444486645238749\
30358907296290491560440772390713810515859307960866\
70172427121883998797908792274921901699720888093776\
65727333001053367881220235421809751254540594752243\
52584907711670556013604839586446706324415722155397\
53697817977846174064955149290862569321978468622482\
83972241375657056057490261407972968652414535100474\
82166370484403199890008895243450658541227588666881\
16427171479924442928230863465674813919123162824586\
17866458359124566529476545682848912883142607690042\
24219022671055626321111109370544217506941658960408\
07198403850962455444362981230987879927244284909188\
84580156166097919133875499200524063689912560717606\
05886116467109405077541002256983155200055935729725\
71636269561882670428252483600823257530420752963450";
var biggestProduct = 0;
var currentProduct = 0;
var currentNumbas = {};
function product (proddedNumbas){
var productHolda = 1;
for (var i = 0; i < 13; i++)
productHolda *= proddedNumbas[i];
return productHolda;
}
for (var j = 0; j < 1000; j++)
for (var i = 0; i < 13; i++){
currentNumbas[i] = numbas[j+i];
currentProduct = product(currentNumbas);
if (currentProduct > biggestProduct)
biggestProduct = currentProduct;
}
console.log(biggestProduct);
Your loop should be
for (var j = 0; j < 1000; j++) {
for (var i = 0; i < 13; i++) {
currentNumbas[i] = numbas[j+i];
currentProduct = product(currentNumbas);
}
if (currentProduct > biggestProduct)
biggestProduct = currentProduct;
}
You just needed to move the if clause outside of the inner loop; this compares the product after it is complete.
This gives the expected answer 23514624000.

Filling up a 2D array with random numbers in javascript

I'm really sorry if anything like this has been posted here before but I couldn't find anything, I'm kinda new to the site still!
So for a while now I've been learning a bit about game development through html5 and javascript and I stumbled upon making tileset maps, I now have a tileset and an 2D array that I want to put certain tiles in (the number varies between 6 and 10 in this case).
I figured it could be a cool function to make the map choose between a small set of similar tiles so I don't have to specifically number every tile in the array(just define the type)
The method I have currently is probably the best for being able to define types but I want something that looks a bit cleaner and/or information to why my "cleaner" version dosen't work.
var ground = [
[tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile()],
[tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile()],
[tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile()],
[tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile()],
[tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile()],
[tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile()],
[tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile()],
[tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile()],
[tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile(),tile()]];
function tile() {
var y = (Math.random() * 5 | 0) + 6;
return y;
}
This is the code I've been using so far, I have to edit every element of the code with the tile() function to get a random number in each one, what I wanted to have was something like this:
for (var i = 0 ; i < 15; i++) {
for (var j = 0; j < 9; j++) {
ground[[i],[j]] = (Math.random() * 5 | 0) + 6;
}
}
to fill the array without having to add the function to each spot.
I have a feeling that I'm missing a return function or something along those lines but honestly I have no idea.
You were thinking in the right direction but there are some errors in your code ;)
You have to initialize the array first before you can push elements into it.
And you were counting i++ twice
Javascript
var ground = []; // Initialize array
for (var i = 0 ; i < 15; i++) {
ground[i] = []; // Initialize inner array
for (var j = 0; j < 9; j++) { // i++ needs to be j++
ground[i][j] = (Math.random() * 5 | 0) + 6;
}
}
Maybe even better (reusable)
function createGround(width, height){
var result = [];
for (var i = 0 ; i < width; i++) {
result[i] = [];
for (var j = 0; j < height; j++) {
result[i][j] = (Math.random() * 5 | 0) + 6;
}
}
return result;
}
// Create a new ground with width = 15 & height = 9
var ground = createGround(15, 9);
Here's a quick example. I've created a function that will take in a width and height parameter and generate the size requested. Also I placed your tile function inside generate ground to keep it private, preventing other script from invoking it.
var ground = generateGround(10, 10); //Simple usage
function generateGround(height, width)
{
var ground = [];
for (var y = 0 ; y < height; y++)
{
ground[y] = [];
for (var x = 0; x < width; x++)
{
ground[y][x] = tile();
}
}
return ground;
function tile()
{
return (Math.random() * 5 | 0) + 6;
}
}
http://jsbin.com/sukoyute/1/edit
Try removing the comma from...
ground[[i],[j]] = (Math.random() * 5 | 0) + 6;
...in your 'clean' version. Also, your incrementing 'i' in both for loops:
for (var i = 0 ; i < 15; i++) {
for (var j = 0; j < 9; i++) {
Hopefully these changes make it work for you :)

Categories

Resources