How to put duplicate numbers between brackets in javascript? [duplicate] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Dear stackoverflowers,
Could someone help me further with this excercise:
Throw a dice 40 times. The throws has to be put in an array. If a throw is the same as the previous one, it has to be grouped between brackets. It'll cost you 1 point per throw, and if you throw two the same numbers in a row, you get 5 points. Print the info out for the user(like: "Congratz! You earned 5 points") , and how many points the user has left. I dont really know from how many points the user starts but lets just give it 40.
This is my code so far
<html>
<head>
<script>
function rollDice() {
var die1 = document.getElementById("die1");
var status = document.getElementById("status");
var d1 = Math.floor(Math.random()*6) +1;
console.log("You rolled "+d1+".");
if(d1)
}
</script>
</head>
<body>
<div id="die1" class="dice">0</div>
<button onclick="rollDice()"> Roll the dice </button>
<h2 id="status" style="clear:left":> </h2>
</body>
Id like to know how to put this into an array and if a throw is the same as the previous one, it logs to the console 5 points.
I'm a beginner so please bear with me.
Thanks in advance,
Youssef

YOu can declare an array as follows:
var diceRolls = [];
To add something to an array:
diceRolls.push(diceRoll);
To check previous result for equality. Note shoudl also check the previous element exists in the array.
if (diceRolls[diceRolls.length - 1] === diceRoll)
// do stuff
Hope that gets you started. W3cschools is a great way to get started

Related

jQuery parseInt() not working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have this code to add elements to my table and that part works perfectly fine. However, I want to add up the values of the last column in order to later print that on another part of the page. parseInt doesn't work for some reason (the values don't come back as integers and stay as strings, is there anything I have done wrong? Again, adding to the table works perfectly fine.
var hourC = 0;
var hourC =+ parseInt($(".hourCount").text(), 10);
alert(hourC);
Edit:
When I print the values of the variable hourC they don't add up to the previous value, they just stay next to each other. Example: 1 + 1 = 11 rather than 2. I don't see where my issue is and the answer for debugging didn't help since I still got the same result.
Final Edit:
I achieved what I wanted now through a different medium, I created an array and pushed the values into the array and then I used "join" to solve the issue.
If interested in what I was asking for here is a fiddle with the final result. (You can just change the console.log to alert)
Basic debugging skills.
parseInt works -- its a well tested function. If it broke in a browser, a lot of people would notice.
However, you haven't given any way to figure out what is going on with your code, since the real problem MUST be here:
$(".hourCount").text()
That must not contain whatever value you think it does.
Split your code up and use the debugger, or even console log, to see what the values are.
var hourC = 0;
var strValue = $(".hourCount").text();
alert(["strValue = ", strValue]);
hourC = parseInt(strValue, 10);
alert(hourC);

Javascript / Html Check box error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
So, I started this project for school; I'm a beginner in javascript as you can see from the code, and I'm kinda stuck. First of all, I want to make a quiz where you have to introduce the amount of questions you want to answer, then answer them ofc. For the beginning I chose 7 questions with random answers to test it. The problem is that when I introduce 3 or less questions to answer it works fine, but when i go for more (+3 <=7) I get a strange error: index.html?fname=3:103 Uncaught TypeError: Cannot read property 'checked' of undefined(…). Here is my code:
My code
Im sorry for distrubing you with my stupidity! Have a nice day!
PS: I forgot to mention that this code isnt finished (I still need to style it), so dont judge me.
On line 68 of your code, you set y to be the number of questions the user asked for.
y=document.getElementById("myForm").elements[0].value;
This loop (starting on line 102) is where the error is coming from:
for(var i=0; i<y; i++){
if(choices[i].checked){
choice = choices[i].value;
}
}
Here's what happens: If you ask for 4 questions, this loop will keep running as long as i is less than 4. When i is 3, the if statement will be trying to access choices[3].checked. Remember, choices[3] is actually the 4th item in the array since indexes start at 0. Each question only has 3 choices, so when you ask for the 4th one you get undefined.
But you want this loop to look at 3 and only 3 answers for each question, regardless of how many questions there are in total. What you probably meant to write was this:
for(var i=0; i<3; i++){
if(choices[i].checked){
choice = choices[i].value;
}
}

Math method gives me the same number all over the time [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Recently i've been trying to do a repetitive random number in JS, but the problem is that always i have to reload the page to get a new random number, even i try to show it multiple times but the problem is that the number is the same until i refresh the page.
<?php
<script type="text/javascript" charset="utf-8">
function getNumber(){
var number = Math.random()
return number;
}
document.write(getNumber()); // NUMBER 1
document.write(getNumber()); // SAME AS NUMBER 1
document.write(getNumber()); // SAME AS NUMBER 1
document.write(getNumber()); // SAME AS NUMBER 1
document.write(getNumber()); // SAME AS NUMBER 1
</script>
?>
I think it might be because you are using document.write. Use console.log instead and take a look at the console.
console.log(getNumber());
console.log(getNumber());
If you want your results in the browser get a reference to an element, or make and append one.
Math.random() returns a number between 0 and 1. Please read the docs for more information.
Rather than using document.write just update the content of your element like this:
function getNumber(){
var number = Math.random()
return number;
}
var div = document.getElementById('result');
div.innerHTML = getNumber();

JQuery append() adding quotes and newlines and I don't know why [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have an append statement that adds a single character at a time to a span. (The characters are pulled from a string by treating it like an array: text[i] and such.) I feel like it should be giving me this:
<span id="outputid0">text</span>
But instead it's giving me this:
<span id="outputid0">
"t"
"e"
"x"
"t"
</span>
I guess it makes sense, and this is not a real problem at the moment, but it is a bit awkward-looking and I'm not sure why it's happening. Can anyone explain this one for me?
Edit: Here's a link to JSFiddle, showing an example of what I was talking about. Thank you to ᾠῗᵲᄐᶌ, who seems to have diagnosed and solved the problem just fine without that. If anyone else would like to see it, there it is.
Append adds a child node after the last child node so each character is going to be a text node and that is why they are separated like that
If you want to add a character at a time you're better off taking whats there and concatenating the char to the string that's already there
You can do it like this using jQuery's .text(function(){}) function
// where i = index and v = current text value
$('#outputid0').text(function(i,v){
return v + "your new char";
});
FIDDLE EXAMPLE
If I was a betting man, I'd say you are looping thru an array to generate what you want. Do something like this: Remember, I am only assuming how the data is setup - and only showing for example purposes.
var arr = ['t','e','x','t'];
// just '.join' the array
<span id="outputid0"></span>
// following in script tag
jQuery('#outputid0').text(arr.join(''));
Just to reiterate what Ehsan mentions above. You really need to include your code so there is no 'assuming' or 'guessing' at what you want to accomplish :-)

How to put dice rolls in an array? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Dear stackoverflowers,
Could someone help me further with this excercise:
Throw a dice 40 times. The throws has to be put in an array. If a throw is the same as the previous one, it has to be grouped between brackets. It'll cost you 1 point per throw, and if you throw two the same numbers in a row, you get 5 points. Print the info out for the user(like: "Congratz! You earned 5 points") , and how many points the user has left. I dont really know from how many points the user starts but lets just give it 40.
This is my code so far
<html>
<head>
<script>
function rollDice() {
var die1 = document.getElementById("die1");
var status = document.getElementById("status");
var d1 = Math.floor(Math.random()*6) +1;
console.log("You rolled "+d1+".");
if(d1)
}
</script>
</head>
<body>
<div id="die1" class="dice">0</div>
<button onclick="rollDice()"> Roll the dice </button>
<h2 id="status" style="clear:left":> </h2>
</body>
Id like to know how to put this into an array and if a throw is the same as the previous one, it logs to the console 5 points.
I'm a beginner so please bear with me.
Thanks in advance,
Youssef
YOu can declare an array as follows:
var diceRolls = [];
To add something to an array:
diceRolls.push(diceRoll);
To check previous result for equality. Note shoudl also check the previous element exists in the array.
if (diceRolls[diceRolls.length - 1] === diceRoll)
// do stuff
Hope that gets you started. W3cschools is a great way to get started

Categories

Resources