Javascript / Html Check box error [closed] - javascript

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;
}
}

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);

Function not returning the value [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am working on a javascript quiz programme , and i have return a function to check what difficulty level the user wants . below is the code and the jsfiddle :
function getdifficulty(){
var j = 0;
var level = prompt('what level would you like 1. easy 2. intermediate 3.hard' , '')
if(level == easy){
j = questionseasy[0];
}
else if(level == intermediate){
j = questionseasyenuf[0];
}
else{
j = questionshard[0];
}
alert("you did it");
}
getdifficulty();
Jsfiddle here
now the problem is the the alert is not showing up ? whats the problem with this short piece of code ? (In the real programme though i will not use an alert but return statement , i even tried using document.write or console.log but none worked) .
prompt() returns a string. You need to make your comparisons strings by encapsulating them in double quotes (").
function getdifficulty(){
var level = prompt('what level would you like 1. easy 2. intermediate 3.hard' , '')
if(level == "easy"){
alert("easy");
}
else if(level == "intermediate"){
alert("intermediate");
}
else{
alert("hard");
}
}
getdifficulty();
JSFiddle
Also, in the implementation you have provided in your post, questionseasy, questionseasyenuf, and questionshard will not be defined. You must bring them into the scope of the function before you can start using them.
questionseasy is undefined. You can trace javascript as it runs in your browser console and see this.

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 :-)

Defining String Arrarys [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
Hi I'm a beginner in Javascript and somethinh strange occured.
I wanted to create an global String array in an external Javascript file with
var natArray = new Array(20); But my IDE(Webstorm 7) Says me newArray is an Unresolved function and I cant use the array in another function it was created for(Source: Javascript Console)
Thank you for your time and feedback.
EDIT: Rest of the Code
function country(name){
for(i=0;i<20;i++)
natArray[i]=name;
document.write(natArray[i]);
}
And here i call it(a html file):
Country:
<select>
<script type="text/javascript">
country("RandomCountry1");
country("RandomCountry2");
</script>
</select>
Im just getting an empty drop-down-list
I'm not sure what you are trying to accomplish, but the reason that document.write(natArray[i]); is returning undefined is that you are leaking i to the global scope in for(i=0;i<20;i++).
Once this loop has completed, i is equal to 20, even outside the scope of the loop (to avoid this, try for(var i=0;i<20;i++). Although natArray has a .length of 20, arrays elements are zero-indexed, meaning that they start counting at 0, not 1.
Because of this, if what you are trying to do is access the last element of natArray, you can/should do so with natArray[natArray.length - 1]. The -1 accounts for the zero-indexed nature of Javascript arrays. If natArray has a length of 20, you will fail to access natArray[20] because this statement is actually trying to access the 21st element, not the 20th.

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

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