Home > Software design >  can i use variable as my index value in javascript for loops
can i use variable as my index value in javascript for loops

Time:11-08

var tablNum = prompt("enter numberr");
var tableCout = (tablNum * 10)   1 ;

for (let i = tablNum; i < tableCout; i = i   10) {
    console.log(i)
    
}

This is my code but when I run my code, the loop works. I am trying to make math table generator this is my code but its not working.

var tablNum = prompt("enter number");
var tableCout = (tablNum * 10)   1 ;

for (let i = tablNum; i < tableCout; i = i   10) {
    console.log(i)
    
}

And what I want from my code to do is generate a table which number I type.

CodePudding user response:

You have to convert what you get from prompt from a string to a number.

var tablNum = Number(prompt("enter number"));

CodePudding user response:

Your variable tablNum contains the inputted value by the user as a string, but you are treating it as a number, as a result it is sometimes coerced to a number and other times it is not, for example i = i 10 will treat the i as a string and concatenate "10" with the i, while comparison will treat it as a number.

You must explicitly set the type of that tablNum variable to be number, you can use the parseInt() or Number() functions to do that.

CodePudding user response:

If you want a multipilication table I think this code will help you:

const number = parseInt(prompt('Enter an integer: '));

//creating a multiplication table
for(let i = 1; i <= 10; i  ) {

    // multiply i with number
    const result = i * number;

    // display the result
    console.log(`${number} * ${i} = ${result}`);
}

CodePudding user response:

If you're after creating "times tables" you may find this example useful.

  1. prompt will always return a string so to use that in your loop you'll need to coerce it to an integer either by using the Number constructor or by prefixing a to the prompt.

  2. "times tables" are generally multiples of 12 so we shall use that value in the calculation.

  3. The loop needs to go from n (the number from the prompt) up to the bound of n * 12, and increasing by n on each iteration:

    for (let i = n; i <= bound; i  = n) {
    
  4. If you want a table it might be best use the features available to you as a developer. Here I've created a grid using CSS Grid which allows you specify how many columns/rows you want. I've specified four columns 30 pixels wide: grid-template-columns: repeat(4, 30px).

    Then, in your loop, create some elements (I've given them a class name of box). Here I've created a fragment first to which the elements are added. Once the iteration is complete the fragment is added to the page.

// Coerce the prompt (a string) to an integer
const n = Number(prompt('Enter a number'));

// Set the upper bound for the loop
const bound = n * 12;

// Cache the grid element, and create a
// document fragment
const grid = document.querySelector('.grid');
const frag = document.createDocumentFragment();

// Loop from `n` to the upperbound increase by n
// on each iteration
for (let i = n; i <= bound; i  = n) {

  // Create an element, add a `box` style to it
  // set its text content, and then add it
  // to the fragment
  const box = document.createElement('div');
  box.className = 'box';
  box.textContent = i;
  frag.appendChild(box);
}

// Finally add the fragment to the grid
grid.appendChild(frag);
:root { --grid-width: 4; }
.grid { display: grid; grid-template-columns: repeat(4, 30px); gap: 0.5em; width: 50%;}
.box { display: flex; justify-content: center; align-items: center; border: 1px solid lightgray; width: 30px; height: 30px; font-size: 0.9em; border-radius: 5px; }
<div ></div>

  • Related