 |
|
 |
I have four check box list on my aspx page.
The scenario is that each check box list have check boxes with different-different value.
I want that when any user click on those check boxes then sum of value of all selected checkboxes if crosses 1 then it shows a message like that it exceeds 1
i am using the following javascript function
<script type="text/javascript" language="javascript">
var total = 0;
var histvalue = 0;
var valueOfbx = 0;
var currvalue = 0;
function MutExChkList(chk) {
if (chk.checked) {
valueOfbx = 0;
var chklst = chk.parentNode.parentNode.parentNode;
var chkbx = chklst.getElementsByTagName("input");
for (var i = 0; i < chkbx.length; i++) {
if ((chkbx[i].checked == true) && (chkbx[i].value != chk.value)) {
valueOfbx = Number(chkbx[i].value);
histvalue = -1;
chkbx[i].checked = false;
}
else if ((chkbx[i].checked == true) && (chkbx[i].value == chk.value)) {
currvalue = Number(chkbx[i].value);
}
}
total = total + currvalue;
if (histvalue == -1) {
total = total - valueOfbx;
histvalue = 0;
}
if (total > 1) {
total = total - Number(chk.value);
chk.checked = false;
alert("exceeds");
}
else
chkbx[i].checked = false;
}
else {
total = total - Number(chk.value);
}
}
</script>
it works perfectly on my system but as i tried to work this on other systems then it does not value of checkboxes
Please help me
Thanks in advance
|
|
|
|
 |
|
 |
Psst - this forum is NOT for questions, it's for showing off clever things we have done. Try asking this in Q&A and an answer will be forthcoming. Maybe.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
 |
|
 |
#include<iostream>
#include<iomanip>
using namespace std;
void line() { cout << setw(12) << setfill('-') << endl;}
int main() {
int binary[8];
int num;
int bits[8] = {128,64,32,16,8,4,2,1};
do {
line();
cout << "write in the number to be converted into binary." << endl;
cin >> num;
for(int i = 0;i < 8;i++) {
if(num >= bits[i]){
binary[i] = true;
num = num - bits[i];
}
else if(num < bits[i]) {
binary[i] = false;
}
}
for(int i = 0;i < 8;i++) {
cout << binary[i] ;
}
}while (1);
system("pause");
return 0;
}
this code is made about 1 month ago,and this is supposed to convert numbers into binary. Since I'm a beginner,the code might be messy and unintelligent. Please tell me how to improve this or to make it more efficient. If by any chance I'm in the wrong section, forgive me,this is the first time visiting this site.
|
|
|
|
 |
|
 |
It is the wrong forum - you want either Quick Answers[^] or tHE c++ Forum[^]
However, that is a bit of an odd way to do it. I would have a single loop and a single test:
for (int i = 0; i < 8; i++)
{
cout << (num & 0x80) != 0 ? "1" : "0";
num <<= 1;
}
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
 |
|
 |
Message Automatically Removed
|
|
|
|
 |
|
 |
Dear Sir, I have new project for based SEO program,But I am new beginner. so, help us ?
|
|
|
|
 |
|
 |
this forum isn't for questions, if it is ASP.NET, try posting it here: http://www.codeproject.com/Forums/12076/ASP-NET.aspx[^] sadly, i can't help you.
I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)
|
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
 |
You're in the wrong forum. This one is for showing clever code, not for asking questions.
BTW: IMO a fast solution would be based on a Dictionary (since 2.0) or a HashSet (since 3.5).
|
|
|
|
 |
|
 |
A hashset might not be the best choice to find doubled items since:
MSDN wrote: The HashSet(Of T) class provides high-performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.
|
|
|
|
 |
|
 |
Exactly. Iterate the input List, add new items to the HashSet and the result List.
|
|
|
|
 |
|
|
 |
|
 |
Agreed, the example given will run in N^2 time, and your solution will run in N time.
You can only be young once. But you can always be immature.
- Dave Barry
|
|
|
|
 |
|
 |
Do not forget that HashSet must somehow check the constraint that there must not be duplicates. Also the time needed for that operation must be taken into account. And it will increase with the size of the Hashset.
|
|
|
|
 |
|
 |
if (!hashSet.Contains(candidate)) hashSet.Add(candidate); takes full advantage of hashing and collecting, much faster than other collections' Contains method.
[ADDED] And the test is redundant, Add() already does it, so hashSet.Add(candidate); suffices [/ADDED]
modified 9 Mar '12.
|
|
|
|
 |
|
|
 |
|
 |
Luc Pattyn wrote: You're in the wrong forum.
Correct, this should be a Friday Programming Challenge.
|
|
|
|
 |
|
 |
I know... but the question forums are primarily used for showing code that doesn't work, so I just wanted to throw a stone in the pond.
BTW: I just realized that I know you personally. We both play in the same chess club.
Giraffes are not real.
|
|
|
|
 |
|
 |
You could have wrinkled the C# pond; the programming forums are for discussions, not just for "help, my code fails" kind of questions.
CU.
|
|
|
|
 |
|
 |
UniqueList = doubledList.distinct
|
|
|
|
 |
|
 |
private List<string> LoadUniqueList(List<string> doubledList)
{
return doubledList.Distinct().ToList();
}
LINQ is fun
And as for using this part of the forum for challenges, I think it's a great idea - hardly anyone ever posts here to begin with, since people don't usually look at their own code and say "Damn, that was clever - I need to share it with someone!"
|
|
|
|
 |
|
 |
Such an algorithm is 0(n²) thus for large collection, it will be very slow. Each time, you double the collection size, it is 4 times slower.
Using an HastSet or a SortedSet or a Dictionary would be much faster for large collections. The order would then be about O(n) with hashing or O(n log(n)) with Dictionary (binary search).
Another alternative would be to sort the list if the order does not matters. It is then easier to skip skip or remove duplicates.
Philippe Mori
|
|
|
|
 |
|
 |
I don't agree that hashing would make it O(n), as it requires to handle hash collisions. This if course is not a real problem for average use cases, but after all that's not what the Landau thing is about, right? Usually, this would end up taking O(n log(n)) time, even with hashing. The main difference would be the constant that is involved. Of course it is hard to tell, but I would think that the HashSet would provide highly optimized code to do just this and it would be recommended to just use that.
|
|
|
|
 |