I want to display a block of numbers of the input with input times
basic web pages
var j= "";
var n=prompt("Enter the size of the box");
for(var i=1; i<=n; i++)
{
document.write(i +"<br>");
}
if the given input is 5 the output should be as follows:
o/p
12345
12345
12345
12345
12345
prompt() method only returns a string. So, first you need to convert the string to a number using parseInt()
var num = prompt('Enter the size of the box: ');
num = parseInt(num);
for(let i = 1; i <= num; i++) {
for(let j = 1; j <= num; j++) {
document.write(j+' ');
}
document.write('</br>');
}
var j= "";
var n=prompt("Enter the size of the box");
var x = [ ...Array(n*1+1).keys() ].join('').substring(1);
for(var i=1; i<=n; i++)
{
document.write(x +"<br>");
}
This will solve your problem .
var j= "";
var n=prompt("Enter the size of the box");
for(var i=1; i<=n; i++)
{
for(var j = 1; j<=n ; j++ ){
document.write(j);
}
document.write("<br>");
}
I guess you are new to programming and this example wants to teach you nested loops.
You'll have to split up your task to the smallest possible steps and think about what is happening in your loops. The best way is to work "inside-out".
So first you think about what is a single step you want to do: printing a number.
After that you'll think about how often you want to print it in a row.
After that you'll think about how you want to separate the rows.
In the final step you'll think about how many rows you want to print.
The code above will work for you now, but if you don't think about it, you'll fail at the next similar problem.
Give a man a fish and you'll feed him for a day. Teach a man to fish, and you'll feed him for a lifetime! ;-)
Related
I'm trying to create a program that generates a a pyramid of stars. The amount of layers the pyramid has are dependent on the user's input. For example, if the user entered a 2, it would display something like
*
***
^As you can see, it adds two stars per layer and the number of layers is too. I've already made the basic code for this. I have figured out how to make the layers, but I can't seem to figure out how to get the number of layers right. I figured out of how get 2 stars to appear on each new layer, but am having trouble centering and properly displaying the right amount of layers. (My pyramid is centered poorly and it isn't symmetrical. Here is my code right now. I added some comments.
For some context, I'll include the a picture of how I want the pyramid to look.
As you can see, when the user enters 4 for instance, there are 4 layers, and each layer increases by two on each side. However, with my current code, when the user enter a 4, there aren't four layers and I'm not sure how to achieve that. Please don't use CSS to achieve the centering, I want to keep it JavaScript only.
var num = prompt("Enter a number..");
for (var i = 1; i <= num; i = i + 2) {
for (var j = 1; j <= (num - i); j = j + 1) {
document.write(" ");
}
for (var k = 1; k <= i; k = k + 1) {
document.write("* ");
}
document.write("<br/>");
}
you need to fix the spaces of course but the reason you can't get the expected number of rows is you are increasing i +2. i needs to be i++
EDIT: Spaces are also fixed.
var num = prompt("Enter a number..");
for (var i = 1; i <= num; i++) {
for (var j = 0; j < num-i; j++) {
document.write(" ");
}
for (var k = 1; k <= i; k++) {
document.write("*");
}
document.write("<br/>");
}
You should think it is a matrix and each row length depends with your layer bunber which formula is n*2-1 and set to it in middle of row length
This is my first post here and I am having trouble wording a question, so please bear with me as I have been on this issue for hours.
My friend and I have thought of a fun little function that is supposed to guess the user's name (through an <input> tag) in a certain amount of trials using the random number function to access string letters from an alphabet array numbered 0-25. The function is also supposed to give the user the number of trials it took to guess their name.
I keep getting a non-responsive script, (line 33 - The line containing the second "for loop").
var goal = document.getElementById("your_Name").value;
var alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
var goalArray = goal.split("");
var trials = 0;
var guessArray = new Array();
var i;
var n;
for (i = 0; i < goalArray.length; i++){
guessArray.push(alphabet[Math.floor(Math.random()*26)]);
}
while (goalArray != guessArray){
trials++;
guessArray = [];
for (n = 0; n < goalArray.length; n++){
guessArray.push(alphabet[Math.floor(Math.random()*26)]);
}
}
document.getElementById("appendomatic").innerHTML = "It took " + guessArray + " trials to guess correctly";
Any help or attempt to help would be immensely appreciated!
In case anyone was wondering: This little idea of ours was to test the randomness of Javascript's random function through trials (he made the same program in MatLab, so we are going to compare results of the random functions from both languages).
goalArray != guessArray is always true since they are two separate arrays; even if they contain the same elements.
Since they appear to just be arrays of individual letters in a-z you could compare them with something like goalArray + '' != guessArray, because the toString() of the arrays will compare correctly.
This is how I eventually got it to work (by nesting the while loop and second for loop in another for loop):
var goal = document.getElementById("your_Name").value;
var goalArray = goal.split("");
var alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
var trials = 0;
var guessArray = [];
for (i = 0; i < goalArray.length; i++){
guessArray.push(alphabet[Math.floor(Math.random()*26)]);
}
for (x = 0; x < goalArray.length; x++){
while (goalArray[x] != guessArray[x]){
trials++;
guessArray = [];
for (n = 0; n < goalArray.length; n++){
guessArray.push(alphabet[Math.floor(Math.random()*26)]);
}
}
document.getElementById("appendomatic").innerHTML = "It took " + trials + " trials to guess correctly";
}
}
I'd like my output has 6 random numbers but my code only displays 4 numbers, and the numbers are the same.
var x = Math.floor(Math.random()*41);
for(var i=0; i<7; i++){
document.write(x + "</br>");
i++;
}
Could someone please help?
You need to move .random() inside your loop. Also, remove i++ from your loop, because that is already done by for().
Demo:
Script:
for( var i=0; i<6; i++ ){
var x = Math.floor(Math.random()*41);
document.write(x + "</br>");
}
You don't need the i++ inside the form, it's 6 not 7 and you need to calculate the random numbers inside the for loop.
for(var i=0; i<6; i++){
var x = Math.floor(Math.random()*41);
document.write(x + "</br>");
}
Why are you incrementing i twice? Also, if all the numbers are going to be random, you need to execute the Math function inside of the loop.
for(var i = 0; i < 6; i++) {
document.write(Math.floor(Math.random()*41) + "<br>");
}
You don't need to increment your counter twice. Also, the random() needs to be inside your loop.
var x = Math.floor(Math.random()*41);
for(var i=0; i<6; i++){
document.write(x + "</br>");
x = Math.floor(Math.random()*41);
}
you are assigning the var x with a random number before starting the loop, it remains same throughout the execution
try placing it inside the loop,also about incrementing the i, you are doing it in the for loop already so you do not need to do it inside the loop
var x = Math.floor(Math.random()*41);//place it inside the loop
for(var i=0; i<7; i++){
document.write(x + "</br>");
i++;//remove this
}
I am having an issue with a javascript for loop. I am adding up the elements of an array, but for some reason my loop adds in the first entry twice! There was a similar topic on here before (http://stackoverflow.com/questions/3121670/for-loop-repeats-first-loop-twice) but the author didn't go into his resolution in detail, just that it was "somethin stupid" he did. Can anyone tell me what I'm doing stupid??
for(j=0;j<ARRAY.length;j++)
{TOTAL += ARRAY[j];}
The output is used in a HTML table and it is displaying correctly, it's just the doubled first entry that's the issue!
Any help would be greatly appreciated.
var TOTAL = 0;
for ( var j = 0, len = ARRAY.length; j < len; j++ ) {
TOTAL += ARRAY[j];
}
MDN suggest to use a variable to hold the array length. In addition check your scripts with JSLint.
make sure you declare with var...
Nice little bit of prompting debugging in there too.
for(var j=0; j < ARRAY.length; j++) {
{
TOTAL += ARRAY[j];
//alert("The count of J is now " + j);
}
Thanks for all of your help. Since my original approach hit a dead end I looked into using a function to do the trick. The following works:
Array.prototype.sum = function() {
for (var j = 0, L = this.length, sum = 0; j < L; sum += this[j++]);
return sum;
}
I then call ARRAY.sum() when creating my html table.
I found the above solution on http://www.codingforums.com/showthread.php?t=218803
I am NOT talking about adding elements together, but their values to another separate variable.
Like this:
var TOTAL = 0;
for (i=0; i<10; i++){
TOTAL += myArray[i]
}
With this code, TOTAL doesn't add mathematically element values together, but it adds them next to eachother, so if myArr[1] = 10 and myArr[2] = 10 then TOTAL will be 1010 instead of 20.
How should I write what I want ?
Thanks
Sounds like your array elements are Strings, try to convert them to Number when adding:
var total = 0;
for (var i=0; i<10; i++){
total += +myArray[i];
}
Note that I use the unary plus operator (+myArray[i]), this is one common way to make sure you are adding up numbers, not concatenating strings.
A quick way is to use the unary plus operator to make them numeric:
var TOTAL = 0;
for (var i = 0; i < 10; i++)
{
TOTAL += +myArray[i];
}
const myArray = [2, 4, 3];
const total = myArray.reduce(function(a,b){ return +a + +b; });
Make sure your array contains numbers and not string values. You can convert strings to numbers using parseInt(number, base)
var total = 0;
for(i=0; i<myArray.length; i++){
var number = parseInt(myArray[i], 10);
total += number;
}
Use parseInt or parseFloat (for floating point)
var total = 0;
for (i=0; i<10; i++)
total+=parseInt(myArray[i]);