jQuery parseInt() not working [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
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);

Related

Google script change a value in my array unexpectedly [duplicate]

This question already has answers here:
Why does changing an Array in JavaScript affect copies of the array?
(12 answers)
Closed 5 years ago.
I am stuck with a part of my google script code where one of array elements changed unexpectedly. It looks like a bug to me, but I'd like to hear from here if it's really is or just my coding error that I overlooked. Of course, I'm hoping for a solution as well in any case.
Here is that part of the code.
if (chkIn) {arr[1] = importData[i][1]+'2';
} else {
Logger.log((i)+' '+importData[i][1]);
Logger.log((i+1)+' '+importData[i+1][1]);
Logger.log((i+2)+' '+importData[i+2][1]);
Logger.log(arr[1]);
arr[1] = importData[i][1]+'1';
Logger.log('---------------------------------------------------');
Logger.log((i)+' '+importData[i][1]);
Logger.log((i+1)+' '+importData[i+1][1]);
Logger.log((i+2)+' '+importData[i+2][1]);
Logger.log(arr[1]);
};
(The if statement doesn't seem relevant here, but I included it just in case.)
Here is the output.
2573 2017122103
2574 20171221041
2575 20171221042
20171221042
---------------------------------------------------
2573 2017122103
2574 20171221041
2575 20171221031
20171221031
I really have no idea how importData[i+2][1] changed its value to arr[1] (the number after 2575).
Thank you in advance.
Probably this is because in your case:
arr === importData[i+2]
So when you change arr[1] you also have changed importData[i+2][1].

Using innerHTML for partial HTML [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
the following function
<script>
function new_par()
{
var beginning="<input id=";
document.getElementById("debug").innerHTML=beginning;
}
</script>
produces no output, although if I remove the "<" sign, it does. Presumably the javascript avoids an output that would destroy the html page, but would there be anyway to force the output?
You're assigning it to a node's innerHTML property. When you plug the string into the DOM it's trying to parse it as though it were HTML. If you were to simply add it as a text node it would appear.
var beginning="<input id=''";
document.body.textContent = beginning;
https://jsfiddle.net/2patzyo1/
Edit: upon looking again at your question, if you are trying to get the input element to appear on the page, the string you're using isn't proper HTML because you never closed the tag. When the browser runs into it, it tries to parse and render it, but because it isn't valid it gives up. This will work:
var beginning="<input id=''>";
document.body.innerHTML = beginning;
https://jsfiddle.net/2patzyo1/1/

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

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

Point Spending Tool - Difficulties [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I've made this small tool but I'm not sure how to achieve what I need.
I would like it to subtract from the "Points Left" when adding to the others.
(Not going below zero into the negative, only 30 points).
I would also like to prevent going BELOW the initial numbers in "Weapon Power" and "Magic Power".
(I would like to be able to only spend a maximum of 25 points into one "power")
I think it kind of explains itself so maybe I'm just confusing you more.
Any ideas?
DEMO
You need to give your number input elements distinct ids, because when you do document.getElementById, it will only return you the first element with the given ids.
Then, you need to give another distinct ID to the "points left" field for each character, and update that one. To do that, you'll need to pass to add and substract the correct value ( add(warrior, 'weapon'), add(wizard, 'magic')).
You need to be able to get your IDs from your warrior or wizard object, so you could try doing this:
var wizard = {
weapon:"idOfWeaponField", magic:"idOfMagicField", points:"idOfPointsField"
};
where the string values are the ids of your elements.
Then, within your add function, you can access your id like this
function add(character, statName){
var myID = character[statName];
}
and update the correct input value.
EDIT: code here.
I would also like to prevent going BELOW the initial numbers in "Weapon Power" and "Magic Power". >(I would like to be able to only spend a maximum of 25 points into one "power")
Basically, you've seen what I did with the points left, when I said if(pointsVal.value == 0) return; ? You should be able to implement any constraint you like using that.
I modified mine to give an example where the limits are at their initial values.
If you only want to spend a given number of points on one of the powers, you'll have to substract the limit from the current number and return from add() without changing the value if the number exceeds the threshold. That should be pretty easy with what you have now, since you don't have to change the character object to do it.

Categories

Resources