3h of sleep and I cant figure this out... :S
I had a working something but I am changing the code a lot and removed it now I can not figure it back out.
I really need help...
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display a random number between 1 and 100.</p>
<button onclick="myFunction()">myFunction()</button>
<button onclick="myFunction2()">myFunction2()</button>
<p id="demo"></p>
<p id="demo2"></p>
<script>
var number = Math.floor((Math.random() * 100) + 1);
var number2 = 0;
function myFunction() {
number2 = Math.floor((Math.random() * 100) + 1);
var x = document.getElementById("demo")
x.innerHTML = number2;
}
//number2 is still 0.
function myFunction2() {
var x = document.getElementById("demo2")
x.innerHTML = number;
}
//number is stuck at the beginning number
</script>
</body>
</html>
I need to generate new numbers from the myFunction2.
How do I achieve that ?
You are already creating random numbers from using myFunction() and it doesn't seem like a problem. In myFunction2() you don't change or really do anything so how do you expect it to keep creating random numbers? Could you make your question a bit more clear?
Edit: Just place this line inside your myFunction2() instead of where you have kept it and it should work:
var number = Math.floor((Math.random() * 100) + 1);
Enter this before you actually assign anything to var x.
number is defined once at JavaScript at Question. You can use the same approach that you did at myFunction, define number again within myFunction2
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display a random number between 1 and 100.</p>
<button onclick="myFunction()">myFunction()</button>
<button onclick="myFunction2()">myFunction2()</button>
<p id="demo"></p>
<p id="demo2"></p>
<script>
var number = 0;
var number2 = 0;
function myFunction() {
number2 = Math.floor((Math.random() * 100) + 1);
var x = document.getElementById("demo")
x.innerHTML = number2;
}
function myFunction2() {
number = Math.floor((Math.random() * 100) + 1)
var x = document.getElementById("demo2")
x.innerHTML = number;
}
</script>
</body>
</html>
Related
My code is to find a random number between two numbers, In context they will be frequencies. But im stuck not displaying the correct values? Thanks in advance for any help.
function noteFinder() {
var x = document.getElementById("userInput").value;
var i = Math.random() * ((x*2) - x);
document.getElementById("demo").innerHTML= i + x;
document.getElementById("demo2").innerHTML= x;
}
So instead of showing "demo" as x + i it is only outputting i. Here is the html if that could be something as well as the codepen project [https://codepen.io/bazookajo66/pen/MPYVJW]
<input type="number" id="userInput"=> </input>
<button onclick="noteFinder()">Submit</button>
<p id="demo"></p>
<p id="demo2"></p>
Note that .value is always a string; the easiest way to convert a string to a number with Javascript is with a plus +:
function noteFinder() {
var x = +document.getElementById("userInput").value;
var i = Math.random() * x;
document.getElementById("demo").innerHTML = i + x;
document.getElementById("demo2").innerHTML = x;
}
So, I was making a program in html that would convert any decimal number to a binary number, which was working perfectly, then I tried to modify it, and screwed it up somehow. The problem seems to be it takes the value at the start of the runtime, rather than always grabbing it at the moment, but it wasn't doing that before. And, I have no idea why it's doing it, and I'm not completely sure what I'd even look up to look for this kind of error, especially when I have no idea what is wrong.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type = "number" id = 'decimal'></input><br/>
<a id = 'binary'></a>
<script>
setInterval(convert(),1);
function convert() {
var nNum = "";
var num = document.getElementById('decimal').value;
while(num != 0) {
nNum = (num % 2) + nNum;
num = Math.floor(num/2);
}
document.getElementById('binary').innerHTML = nNum;
}
</script>
</body>
</html>
any and all help is greatly appreciated. c:
Try:
<script>
setInterval(convert,1);
function convert() {
var nNum = "";
var num = document.getElementById('decimal').value;
while(num != 0) {
nNum = (num % 2) + nNum;
num = Math.floor(num/2);
}
document.getElementById('binary').innerHTML = nNum;
}
</script>
When you pass function as parameter of function you don't call it convert(), you only pass it convert
So I'm trying to get this to roll an amount of dice equal to the specified input. To achieve this, I'm using a for loop, however it's not working.
As I just started coding recently, I have no idea why. Could someone please help?
<!DOCTYPE html>
<html>
<body>
<input type="number" id="diceamount" value="1">
<button onclick="FirstFunction()">Roll!</button>
<p id="display"></p>
<script>
var random = [];
document.getElementById("display").innerHTML = random;
a = document.getElementById("diceamount").innerHTML;
function FirstFunction() {
for (i=0; i<"a"; i++) {
x = Math.floor(Math.random() * 4) + 1;
random.push(x);
document.getElementById("display").innerHTML = random;
}
}
</script>
</body>
</html>
Here is how you could do it. There are several issues highlighted in comments:
function FirstFunction() {
// Reset array within this function
var random = [];
// Get value property, and convert to number (with +)
// And use var!
var a = +document.getElementById("diceamount").value;
// No quotes around a, and use var!
for (var i=0; i<a; i++) {
// Use var!
var x = Math.floor(Math.random() * 4) + 1;
random.push(x);
}
// Only show after the loop, and use textContent
document.getElementById("display").textContent = random;
}
<input type="number" id="diceamount" value="1">
<button onclick="FirstFunction()">Roll!</button>
<p id="display"></p>
Note that the array gets implicitly formatted as a comma separated value string when you display it.
This is not how you define "a".
This is how you do it:
for (i=0; i<a; i++) {
This is how you get the value from the text field:
var b = document.getElementById('textbox_id').value;
Then get the integer value:
var a = parseInt(b);
And then the for loop:
for (i=0; i<a; i++) {
It seems you are doing it wrong ,...
Suppose you want to take the value of input which has id = "diceamount"
so for that you have to store the value of the input in variable.
var a = document.getElementById('diceamount').value;
for(i = ; i<= a; i++){
your code
}
make this question down ; and look for some tutorials for javascript
var random = [];
// not useful? but in your code
document.getElementById("display").innerHTML = random;
function FirstFunction(event) {
// get local value into a and use it the + avoids parseInt
let a = Math.abs(+document.getElementById("diceamount").value);
for (i=0; i<a; i++) {
// x was not declared and should be
let x = Math.floor(Math.random() * 4) + 1;
random.push(x);
}
// could be textContent instead...outside loop
document.getElementById("display").innerHTML = random;
}
<input type="number" id="diceamount" value="1">
<button onclick="FirstFunction()">Roll!</button>
<p id="display">x</p>
Does anyone know how do i generate a new random number so i can store them into different variables? (into temp1 and temp2) I want the program to rerun the "randomnumber" function each time for each new variable so i can store the new random value into them.
<HTML>
<!Foundation Page for building our Javascript programs>
<HEAD>
<TITLE>The Foundation Page </TITLE>
<SCRIPT LANGUAGE = "JavaScript">
function main()
{
totalscore()
leaderboard()
}
function randomnumber()
{
var randomnumber;
randomnumber = Math.random()*3;
return(Math.floor(randomnumber+0.2));
}
function totalscore()
{
var n;
var p;
var total;
var temp1;
var temp2;
var score;
score = 0;
total=0
for (n=0; n<10; n=n+1)
{
number = randomnumber();
if (number == 0)
{
score =score+0;
}
else if (number == 2)
{
score =score+2;
}
else if (number == 3)
{
score =score+3;
}
total=total+score;
}
temp1= total
temp2= total
document.write(total)
document.write(total)
}
</SCRIPT>
<HEAD>
<BODY>
<BODY BGCOLOUR = "WHITE">
<H2>The Foundation Page </H2>
<HR>
<SCRIPT LANGUAGE = "Javascript"> main() </SCRIPT>
<INPUT NAME = "dobutton" TYPE = "button" value = "Start game" on Click = "game()">
<INPUT NAME = "dobutton" TYPE = "button" value = "Leaderboard" on Click = "leader()">
</BODY>
</HTML>
I think I know what you are asking and I'll post working code below as well as an explanation of why you are getting the same random number.
You have a for loop is generating the score and and returning a value that you are storing into total. The problem is you are assigning that number to both temp1 AND temp 2, so they are now the same number, then you are writing both numbers to the page. What you need to do is have 2 different calculations. the below code will fix it and give you 2 different numbers for you totals...
function totalscore()
{
var total1 = 0;
var total2 = 0;
for (var n=0; n<10; n++)
{
total1 = total1 + randomnumber();
total2 = total2 + randomnumber();
}
document.write(total1);
document.write(total2);
}
Now I was a little confused at your code because the score is compounding on itself every time it recalculates in the for loop. (ie if the first 5 scores were 1, 2, 2, 1, 3, then the total score would be 24, and not the expected 9. If that was intentional, the put in this code instead.
function totalscore()
{
var total1 = 0;
var total2 = 0;
var score1 = 0;
var score2 = 0;
for (var n=0; n<10; n++)
{
score1 = score1 + randomnumber();
total1 = total1 + score1;
score2 = score2 + randomnumber();
total2 = total2 + score2;
}
document.write(total1);
document.write(total2);
}
Good luck with your project and learning coding!
Issues I am noticing
No semicolons behind most statements
You are returning Math.floor on randomnumber + 0.2, which makes + 0.2 do nothing.
You are calling onclick='game()' on button click, but there is no game(), but maybe that's in code we can't see.
You are setting both temp1 and temp2 = total, and using document.write on total twice
Now, the question is how to generate a random number. You are doing that correctly, although you could simplify it and use Math.floor(Math.random()*3) or return Math.floor(Math.random()*3). Of course, you can switch out 3 and replace it now with whatever cap you wish.
I am very new to javascript, and have limited knowledge. I have just grasped the concept of hello world. At the moment, my code adds FirstNumber and SecondNumber together to give the result. I would like it to do the following:
I am trying to make a program where FirstNumber is pre-defined as 1 and SecondNumber is done by user input. The javascript should count a sum from 1 to the number that should be entered. For example, if the user entered 5, the program should count the sum from 1 to 5 (1 + 2 + 3 + 4 + 5) which will be 15. I was told to maybe use an array, although I'm not sure.
Here is my HTML code:
<!DOCTYPE html>
<html>
<head>
<title>Sum of numbers</title>
<script type="text/javascript">
function sum()
{
var FirstNumber = 1;
var SecondNumber = document.getElementById('txtSecondNumber').value;
alert(parseInt(FirstNumber) + parseInt(SecondNumber));
}
</script>
</head>
<body>
Please enter a number:<input id="txtSecondNumber" type="text" />
<input id="btnAdd" type="button" value="Add" onclick="sum();"/>
</body>
</html>
Could someone help?
Thanks :)
What you're looking for is a for loop
for (var i = FirstNumber ;i < SecondNumber ; i++){
// do what ever you want in here
// like adding i to the total
}
For the simple case of adding sequential number you dont need to loop at all:
1+2+3+4+5+...+n = n(n+1)/2
Formula from: http://en.wikipedia.org/wiki/1_%2B_2_%2B_3_%2B_4_%2B_%E2%8B%AF
function sum() {
var num = document.getElementById('txtSecondNumber').value;
var sum = (num*(num+1))/2;
alert(sum);
}
While an array might help you with what you want, the important bit you're looking for is a for loop. You don't indicate your programming background or if JavaScript is your first language, but a for loop is a basic programming construct that has a starting condition, an ending condition, a way to change things (so something changes between start and end), and something to do while you're counting.
A simple for loop in JavaScript looks like this:
for( var i=0; i<10; i++ ){
alert( i );
}
This will pop-up an alert for each number from 0 to 9 inclusive.
In your case, you want to set your start condition to the first number, the end condition to check if you've done the last number (both of these can be variables - not just the constants as I've illustrated), and increment the number. Inside the loop, you'll want to be adding the number to a reference counter.
Try this, you want to loop through all the numbers in between the first and second number and add them to the result
var submit = document.getElementById('submit');
var input = document.getElementById('txtSecondNumber');
function sum() {
var FirstNumber = 1;
var SecondNumber = input.value;
var result = 0;
for (var i = FirstNumber; i <= SecondNumber; i++) {
result += i;
}
alert(result);
}
submit.addEventListener('click', sum);
jsFiddle - http://jsfiddle.net/et8t3bgd/
You can easily count a sum from 1 to any number with a for loop. If you will ever only sum up from 1, you do not need the FirstNumber variable. Otherwise, you can change i = 1 to i = FirstNumber.
var sum;
for (i = 1; i < SecondNumber+1; i++) {
sum += i;
}
function sum()
{
var SecondNumber = parseInt(document.getElementById('txtSecondNumber').value);
var result=(SecondNumber *(SecondNumber +1))/2;
alert(result);
}
Formula for sum to 1 to n number is n*(n+1)/2
As per Your Code
<!DOCTYPE html>
<html>
<head>
<title>Sum of numbers</title>
<script type="text/javascript">
function sum()
{
var SecondNumber = parseInt(document.getElementById('txtSecondNumber').value);
var result=(secondnumber*(secondnumber+1))/2;
alert(result);
}
</script>
</head>
<body>
Please enter a number:<input id="txtSecondNumber" type="text" />
<input id="btnAdd" type="button" value="Add" onclick="sum();"/>
</body>
</html>
DEMO
Here
function sum(n) {
var res = 0, total = 0;
while ((n--)>0) total += ++res;
return total;
}
Use a recursive program...
var num=Number(prompt("Enter a number"));
var sum=0;
for(var i=num;i!=0;i--){
sum+=i;
}
console.log(sum)
//print sum