pattern number, javascript, loop

Viewed 38

I am trying to print this pattern using javascript.

but I need to enter by the user the following: for example: enter a starting number: 2 insert a final number: 5 enter the jump :2

so far i try this,

let beginGetal = Number(prompt("Geef een begingetal in:"));
let eindGetal = Number(prompt("Geef een eindgetal in:"));
let sprong = Number(prompt("Geef een sprong in:"));
let string = "";
let count = beginGetal;

for (let i = 1; i <=eindGetal; i++) {

    for (let j = 1; j <= i; j++) {
       string += count +"," ;

        count++;
    }
    string += "\n";
}
console.log(string);

>2
>3,4,5,
>6,7,8,9,10,
>11,12,13,14,15,16,17,
>18,19,20,21,22,23,24,25,26


   

like this

1 Answers

UPDATE

So it would seem that I misunderstood the requirement. What we need is to print all numbers between start and end, by splitting them on multiple lines, which line n containing a maximum of 1 + 2*(n-1) numbers.

// I used hardcoded values here since prompt is not standard node.js
let beginGetal = 2;
let eindGetal = 26;
let sprong = 2;

let currentNumber = beginGetal;
let currentRowSize = 1;
let currentRow = [];
let resultString = "";

while(currentNumber <= eindGetal) {
    currentRow.push(currentNumber);
    // if the row has reached its size or if the current number is the
    // current number is the last number, we add the line to the result
    if (currentRow.length === currentRowSize || currentNumber === eindGetal) {
        if (resultString) {
            resultString += '\n'; // add return line except for the first line (for first line resultString === '', as such it is falsy)
        }
        // create the string for the current line and add it to the result
        resultString += currentRow.join(',');
        // reset currentRow for the next line
        currentRow = [];
        // Increment the row size by sprong
        currentRowSize += sprong;
    }
    // increment the number by one
    currentNumber++;
}

console.log(resultString);

Initial answer

If I understand correctly you want the user to input a starting number, an end number and a "jump" which should be the step by which numbers should be incremented in your number list. Then you'd like to print all those numbers separated by a comma.

Here is a way to do so:

let beginGetal = Number(prompt("Geef een begingetal in:"));
let eindGetal = Number(prompt("Geef een eindgetal in:"));
let sprong = Number(prompt("Geef een sprong in:"));
let string = "";

const numbers = [];
let num = beginGetal;

while(num <= eindGetal) {
  numbers.push(num); // add the number
  num += sprong; // increment the number by the jump
}
if (num - sprong < eindGetal) {
  // this is only needed if we absolutely want the final number to be printed. If final number - start number is not a multiple of the jump, we manually add the final number to the array
  // E.g. start=2, end=6, jump=3 => at this stage of the code we would have numbers=[2,5] and num=8 
  // So 8-3 is inferior to 6 so we add 6 to the numbers array
  numbers.push(eindGetal);
}
// create a string containing the numbers separated by a comma
const resultString = numbers.join(',');
console.log(resultString);
Related