if condition not working properly in html js - javascript

in if condition i get value max 10 min 9 when compare its true and exe how its possible
for(var r=1;r<n;r++)
{ c=0;
max=table.rows[r].cells[c].innerHTML;
if (max<minimum)
{
alert("if max"+max+" minimum"+minimum );
minimum = max;
alert(minimum);
location = r+1;
}
else
{
minimum=minimum;
alert("else minimum"+minimum);``
}
}
}
Thank you
give me solutions to find correct answer or else i need code for highlight in table cell which cells are all have minimum value in a table

max=table.rows[r].cells[c].innerHTML;
InnerHTML is a string. In order to do comparison you need to convert it to a number with parseInt or parseFloat
max = parseInt(table.rows[r].cells[c].innerHTML);

You have to use parseInt() method because the verification will be done on a string:
max=parseInt(table.rows[r].cells[c].innerHTML);

Related

How do I make the font size in the table adaptive?

I have a form now, and the number of words in the form is different.Now assume that the table size does not change and I want the font size in the table to change with the number of words.That is to say, the more words, the smaller the font and the last words, the bigger the front. Could you please tell me how to achieve this?
There may be a better way to do this, but you could use javascript. Example below:
var allTDs = document.querySelectorAll('td');
for (i=0; i<allTDs.length; i++){
var tdLength = allTDs[i].innerText.length;
if (tdLength<=100){
allTDs[i].style.fontSize="large";
} else if (tdLength>100 && tdLength<=200){
allTDs[i].style.fontSize="medium";
} else if (tdLength>200 && tdLength<=400){
allTDs[i].style.fontSize="small";
} else if (tdLength>400){
allTDs[i].style.fontSize="x-small";
};
};
Note: This example counts the number of characters in the innerText instead of the number of words. You could change it to count words instead - one way to get a rough count of words is to actually count the spaces " " in the innerText:
var tdLength = allTDs[i].innerText.split(" ").length;

Adding a number to a variable in a parent window

I am trying to load an existing answer and then add a number to it. The answerTotal variable has been set to the value of the recorded answer.
This then should be increasing by 12.5 each time the if statement actions. The problem is that this is not what is happening.
The number is being added on to the end of the loaded answer, for example if the load answer is 25, the output would be 2512.5 when it should be 37.5.
I have seen answers on here mentioning parseInt but it either doesnt work or im not using it correctly. parse answer
Here is what I have at the moment:
var answerTotal = 0;
window.parent.LoadAnswerReturned = function (Reference, Value) {
answerTotal = Value;
}
setTimeout(function(){
window.parent.LoadAnswer('TEST');
}, 100);
function checkAnswer(clicked) {
if(...){
...
answerTotal += 12.5
}
}
Please let me know if any more information is needed. Cheers.
It seems that the variable answerTotal is a string. You need to convert it to number using Unary Plus and then add it other number.
function checkAnswer(clicked) {
if(...){
...
answerTotal = +answerTotal + 12.5
}
}
The unexpected result is because by the type of the variable data that is string rather than number. This in turn means that string addition is performed:
"25" + "12.5" = "2512.5"
Instead, you should update you code to ensure arithemtic addition is performed instead:
function checkAnswer(clicked) {
if(...){
...
/* Use parseFloat to ensure two numbers are added, rather than a string
and a number */
answerTotal = Number.parseFloat(answerTotal) + 12.5;
}
}
You should parse your float variable, so you ensure you are using float variables:
answerTotal = parseFloat(answerTotal) + 12.5;

Javascript Floating Number Comparison

I'm trying to do what I thought was pretty straight forward but having odd results. I have two fields on a page: transactionAmount and transactionLimit. When the button is clicked, it calls a javascript function that makes sure the amount isn't greater than the limit:
var transactionAmount = parseFloat(document.getElementById("amount").value).toFixed(2);
var transactionLimit = parseFloat(document.getElementById("limit").value).toFixed(2);
if (transactionAmount > transactionLimit) {
alert("Over limit");
}
If I set the transactionAmount to be $2.00 and the transaction Limit to be $100.00, I get the over limit alert. If I set the transactionAmount to be $1.00 then it works fine. Basically any other value less than $1.00 works if the limit is $100.00 but anything over $1.00 gives me the error.
Would be grateful for some insight! Thank you!
The main problem happening because of toFixed(2) it converts your result to string that's why your condition is not working as you expected. just wrap it with preceding + character to make it Number from String
var transactionAmount = +(parseFloat('2.00').toFixed(2));
var transactionLimit = +(parseFloat('100.00').toFixed(2));
console.log(transactionAmount,transactionLimit, typeof transactionAmount,typeof transactionLimit )
if (transactionAmount > transactionAmount ) {
console.log("Over limit");
}
Try to add the function "Number()" to you values, toFixed() actually transform them to string. You can also add a "+" before to do the same action.
Example:
var transactionAmount = Number(parseFloat("150.00").toFixed(2)); //or: +parseFloat("150.00").toFixed(2)
var transactionLimit = Number(parseFloat("100.0").toFixed(2)); //or: +parseFloat("100.0").toFixed(2)
if (transactionAmount > transactionLimit)
{
alert("Over limit");
} elseĀ {
alert("you ok dude");
}
Output:
"Over limit"
I hope it helps you!

How check input maxlength < 1GB?

I have 1 TextBox input data in PHP.
I want check limit input.
How check input maxlength < 1GB?
Why do you need to know the byte size of the input?
Regardless, to calculate the byte size of a string in Javascript you can use:
function stringSize(s) {
return encodeURI(s).split(/%..|./).length - 1;
}
This assumes that you're using UTF-8. Not sure if this would work with other encodings.
This can be counting the lines of characters ..The code will be
var value = document.getElementById('titleeee').value;
if (value.length < 3) { //replace with your character length
//the rest of ur code
}
Please see here for more info
1GB = 1024mb = 1048576kb = 1073741824 characters

Javascript - Format number to always show the original decimal places

I need a js function that show the original count of decimals in a number. For example:
value display
2.31 2
1.0 1
2.3500 4
The problem is that i dont know how get the count of decimals.
I have that code:
value=2.3500;
return CountofDecimals(value); // must be display 4:
Anything help??? Thanks :P
That's not possible. There's no difference between the number 3.5 and 3.50 in JavaScript, or indeed in any other common programming language.
If you actually mean they're strings (value = '2.3500' rather than value = 2.3500) then you can use indexOf:
var decimalPlaces = value.length - value.indexOf('.') - 1;
Caveat: I hate this answer, I don't really advocate it
Don't store it as a number, store it as a string. This can result in "stringly typed" code quickly so it is inadvisable. It is a workaround since JavaScript uses a float as the number type.
Alternatively store it as an Object and parse out the format via a function call:
{ value = "1.2345", decimal = 4}
and use that to create the correct number format. If I had the requirement this is probably the hack I'd use. Or, I would have my server return the formatted string as you can pull that off easily server side.
If it would be possible take these numbers as strings, it definitely is possible..And quite simple actually.
function countDecimals(string){
var delimiters = [",","."];
for(var i = 0; i<delimiters.length; i++){
if(string.indexOf(delimiters[i])==-1) continue;
else{
return string.substring(string.indexOf(delimiters[i])+1).length;
}
}
}
You could use this function:
function decimalplaces(number)
{
numberastring = number.toString(10);
decimalpoint = numberastring.indexOf(".");
if(decimalpoint == -1)
{
return 0;
}
else
{
return numberastring.length - decimalpoint - 1;
}
}

Categories

Resources