enable/disable button using case statement - javascript

Hello, need help pleaseI already tried adding a break on both end of condition ..I Have 5 Textboxes, 1 Button and a Javascript for their function. What i want to do is this..
CASE 1: When the value of Stat variable is "N", The program must require an input on Area, Capital and Code for the ADD button to enable else it will disable.
CASE 2 When the value of Stat variable is "R", The program must require an input on Area, Gross and Code textboxes for the ADD button to enable else it will disable. I have tried to code it this way.. But it didn't work.
function SetButtonStatus() {
var Stat = document.getElementById('<%=_oTextBoxNRC.ClientID%>').value;
var Area = document.getElementById('<%=_oTextBoxArea.ClientID%>').value;
var Capital = document.getElementById('<%=_oTextBoxCapital.ClientID%>').value;
var Code = document.getElementById('<%= _oTextboxBusLineCode.ClientID%>').value;
var Gross = document.getElementById('<%=_oTextBoxGrossRec.ClientID%>').value;
//Change these conditions as your requirement
switch (Stat) {
case 'N':
if (parseFloat(Area) >= 1 && parseFloat(Capital) >= 1 && Code.length >= 1)
document.getElementById('<%=_oButtonAdd.ClientID%>').disabled = false;
else
document.getElementById('<%=_oButtonAdd.ClientID%>').disabled = true;
break;
case 'R':
if (parseFloat(Area) >= 1 && parseFloat(Gross) >= 1 && Code.length >= 1)
document.getElementById('<%=_oButtonAdd.ClientID%>').disabled = false;
else
document.getElementById('<%=_oButtonAdd.ClientID%>').disabled = true;
break;
}
}

Use setAttribute and removeAttribute like this:
if (parseFloat(Area) >= 1 && parseFloat(Capital) >= 1 && Code.length >= 1)
{
document.getElementById('<%=_oButtonAdd.ClientID%>')
.setAttribute("disabled","disabled");
}
else
{
document.getElementById('<%=_oButtonAdd.ClientID%>')
.removeAttribute('disabled');
}

Related

Error in switch statement, Javascript

I got a function that should return a result depending on a dealed Black Jack hand. I used a switch statement for this, eventhough I'm not sure you could use multiple switch in a function. Anyhow I got an error saying 'missing ; before statement' after the first 'result' text in the first 'case'. This code is what I have been taught so I'm not sure where I did go wrong. Could you please give me a hint or anything, please? Refards, Thomas.
function printResult(playResult, dealResult) {
var text = "";
switch(playResult) {
case (playResult == 21) : result "black jack";
break;
case (playResult > 21) : result "busted";
break;
case (playResult < 21) : result "safe";
break;
}
switch(dealResult) {
case (dealResult < 17) : result "safe";
break;
case (dealResult == 17 && < 21) : result "stop";
break;
case (dealResult == 21) : result "black jack";
break;
}
return result;
}
var result = "Player: " + playResult + ", Dealer: " + dealResult;
ANSWER = (printResult(5+9+10, 6+3+7));
Maybe you want result to be a variable assigment?
result = "black jack"
or maybe a return?
return "black jack"
It looks like you're trying to do "if logic" in the case statement; that's not how a switch works. A switch is going to take the value of dealResult and do a straight compare.
switch(dealResult)
{
case 21: //dealResult MUST be 21
result = 'blackjack!'
break;
case 20: //dealResult MUST be 20
//etc..
break;
case >20: //not valid, will break
}
As far as I know, if you need to do > and < compares, you should be using an if -> else block.
You have to remove all of the playResult variables inside of the switch block.
In addition, switch does not support higher than or lower than so a if is better suited for this situation.
function printResult(playResult, dealResult) {
var result;
if(playResult == 21) {
result = "black jack";
} else if(playResult > 21) {
result = "busted";
} else {
result = "safe";
}
if(dealResult < 17) {
result = "stop";
} else if(dealResult == 17 && dealResult < 21) {
result = "stop";
} else {
result = "black jack";
}
return result;
}
var result = "Player: " + playResult + ", Dealer: " + dealResult;
ANSWER = (printResult(5+9+10, 6+3+7));
This is a lot closer, but there is still problems with your logic.
Looking at your function, even if you get the switch working i don't think the logic is what you're aiming it to be.
If the purpose of the function is tell you who won the game, ergo the result as your name implies then you need to first put the rules down.. lets just for readability write it in pseudo..
Where either player has blackjack check the following
Player Blackjack to Dealer Blackjack = Push (Draw)
Player Blackjack to Dealer Anything = Player Win
Player Anything to Dealer Blackjack = Dealer Win
Then check if one is bust, to win a player must of held
Player > 21 = Dealer Win
Player <=21 and Dealer > 21 = Player Win
After that, we can do the simple checks.
Player == Dealer = Push
Player > Dealer = Player Win
Player < Dealer = Dealer Win
You can't really use a switch statement effectively to do this. You 'can' use it but for something like this, if else will suffice.
function blackJackWinnerIs(player, dealer)
{
if((player == 21) || (dealer==21))
{
if(player > dealer)
return "Player";
else if (player < dealer)
return "Dealer";
else
return "Push";
}
else if(player > 21)
return "Dealer";
else if(dealer > 21) // already checked if player<=21
return "Player";
else if(player > dealer)
return "Player";
else if(dealer > player)
return "Dealer";
else return "Push";
}
var hands = [
{player:21,dealer:21},
{player:20,dealer:21},
{player:21,dealer:17},
{player:22,dealer:17},
{player:17,dealer:22},
{player:19,dealer:18},
{player:18,dealer:20},
{player:20,dealer:20}
];
for(var i=0; i<hands.length;i++)
{
var winner = blackJackWinnerIs(hands[i].player,hands[i].dealer);
console.log("Player:",hands[i].player," Dealer:",hands[i].dealer," => Winner:",winner);
}
This is functional.. but it won't protect you from bad inputs e.g. if you accidentally passed in two nulls you would get a "push" result when really you should either send out an error or void the game... but thats all on you.

Javascript switch

I am working on a zodiac calendar that requires a switch 0-11 for the signs. I have written HTML code that drops down for the month and a text input for the year. The sign should use id 'output' and should also show up in text. I am not sure if I am using my switch correctly, of if my math is causing the problem or why it is not sending to output.
HTML CODE:
<div><label for="sign">Sign</label><input type="text"
name ="sign" id="sign"></div>
Javascript Code
if (year && year.value && (year.length == 4)){
year = parseInt(years.value);
month = parseInt(month.value);
if (month < 2) {
year = (year - 1);
}
year = ((year - 1924) % 12);
} else { // Show Error:
document.getElementById('year').value =
'Please enter valid values.';
}
switch (year){
case 0 :
block code;
break;
etc..
} // End Switch
if (output.textContent != undefined) {
output.textContent = sign;
} else {
output.innerText = sign;
}
return false;
}
Your regular expression could be failing to match your lowercased url. When that happens, the result would be null.
You should be checking the match() result before using it. Something like this:
var matches = url.toLowerCase().match(/https?:\/\/(.+?)[?#\/$]/);
if (!matches || matches.length < 2) {
// Handle error
...
} else {
// Keep going
var domain = matches[1];
...
}
Also, verify that your regular expression is actually doing what you intend.
Because of my javascript code innerText
if (output.textContent != undefined) {
output.textContent = sign;
} else {
output.innerText = sign;
}
I had to delete
<div><label for="sign">Sign</label><input type="text"
name ="sign" id="sign"></div>
and replace it with
<p>Sign: <span id="output"></span></p>
I could have easily changed the javascript code and document.getElementID('output') = sign.value;
The problem should be caused by domain checking instead of calculate function.
Remove domain checking and try again (see if it works).
Errors:
1) if (year && year.value && (year.value.length == 4)){
year = parseInt(year.value);
2) main html didn't declare element "output"

Javascript switch and if statements with multiple conditions doing something really strange?

I'm trying to make a function that changes the value of the attribute x or y of the dot object four times, each time only if two conditions are met. The variable permissionSomething has to be 1, and x or y has to be more or less then a certain number, because in this case it is used for a moving dot on a canvas, which has to stay within the canvas. PermissionSomething is set to 1 if a certain key is pressed down, and changes back to the default value of -1 if that key goes up again.
var permissionLeft = -1;
var permissionRight = -1;
var permissionUp = -1;
var permissionDown = -1;
function update()
{
switch (true)
{
case permissionLeft = 1 && dot.x > 29:
dot.x--;
break;
case permissionRight = 1 && dot.x < 841:
dot.x++;
break;
case permissionUp = 1 && dot.y > 29:
dot.y--;
break;
case permissionDown = 1 && dot.y < 541:
dot.y++;
break;
}
}
If I run this, even without pressing any key, it starts to move in some strange way, stops and starts moving back and forth. If I don't press any keys permissionSomething should never be 1 so it should never move. The same thing happens if I write four if-statements like this:
if (permissionLeft = 1 && dot.x > 29) {
dot.x--};
if (.....

Condition for a number to be in range - Titanium/Javascript

I'm developing app in Titanium using Javascript and trying to realize the following check logic:
User has entered one value in range between 1 and 200, then he/she should enter second value in range between 1 and the first value(less or equal, but no more then the first value).
Here is my code:
var value_alert = ''; //First value
var value_remind = ''; //Second value (should be less or equal)
var default_value_alert = 10; //Default first value for TextField
var default_value_remind = 5; //Default second value for TextField
// handle and save the first value entered by user
function doOpenAlert(){
var input_text = Ti.UI.createTextField({
keyboardType: Ti.UI.KEYBOARD_PHONE_PAD
});
if(value_alert === ''){
input_text.value = default_value_alert;
} else {
input_text.value = value_alert;
}
var dialog = Ti.UI.createOptionDialog({
title : "Specified distance in the range 1-200 km",
androidView : input_text,
buttonNames : ['Ok', 'Cancel']
});
dialog.show();
dialog.addEventListener('click', function(e){
if(value_remind === ''){
value_remind = default_value_remind;
}
if(e.index == 0){ // Check is Ok pressed
// check_number = isInt(input_text.value);
if(input_text.value >= 1 && input_text.value <= 200){ // Check that the first value is in range
var toast = Titanium.UI.createNotification({
duration: 2000,
message: "Distance is " + input_text.value + " km."
});
toast.show();
value_alert = input_text.value; // Saving the first value entered by user
} else if(input_text.value == 0){
alert("The field is empty.");
} else if(!(input_text.value >= 1 && input_text.value <= 200)){
alert("Range is between 1 and 200 km.");
}
}
});
}
// handle and save the second value entered by user
function doOpenMinne(){
var input_text = Ti.UI.createTextField({
keyboardType: Ti.UI.KEYBOARD_PHONE_PAD
});
if(value_remind === ''){
input_text.value = default_value_remind;
} else {
input_text.value = value_remind;
}
var dialog = Ti.UI.createOptionDialog({
title : "Remind before number",
androidView : input_text,
buttonNames : ['Ok', 'Cancel']
});
dialog.show();
dialog.addEventListener('click', function(e){
if(value_alert === ''){
value_alert = default_value_alert;
}
if(e.index == 0){
// check_number = isInt(input_text.value);
if(input_text.value >= 1 && input_text.value <= value_alert){ // Check if the second value in is range between 1 and the first value
var toast = Titanium.UI.createNotification({
duration: 2000,
message: "Remind at " + input_text.value + " km."
});
toast.show();
value_remind = input_text.value; // Saving the second value entered by user
} else if(input_text.value == 0){
alert("The field is empty");
} else if(!(input_text.value >= 1 && input_text.value <= 200)){
alert("The range is between 1 and 200 km");
}
}
});
}
For example, it works well in the following combination:
1)
First value - 10;
Second value - 5;
2)
First value - 105;
Second value - 101;
And the main thing, if the first value is >= 100 , but the second is < 100 - it doesn't work.
It seems that conditions are correct, but works incorrect - can't find a mistake.
I think that the issue you're having is that you're comparing the values as strings rather than numbers. When you compare two strings, Javascript bases the comparison on the Unicode values of the characters in order. What does that mean for you? The short answer is, that while "90" < 200 is true because that comparison results in the "90" being coerced to 90, "90" < "200" is false because "9" is greater than "2".
In order to avoid this behavior, you need to convert one or both of your variables to numbers. This answer on converting strings into numbers shows a number of ways for you to do that, but in your case, I think that parseInt(input_text.value, 10) <= parseInt(value_alert, 10) would work fine for you.

Does switch statement start comparing cases from the top in this example?

I found this example to make range work with switch statement:
function GetText(value)
{
var result;
switch (true)
{
case ((value >= 26) && (value <= 50)):
result = ">= 26.";
break;
case ((value >= 1) && (value <= 25)):
result = "Between 1 and 25.";
break;
case (value == 0):
result = "Equals Zero.";
break;
}
return result;
}
But if I modify the code and remove the second check for the value the example will still work:
function GetText(value)
{
var result;
switch (true)
{
case ((value >= 26)):
result = ">= 26 .";
break;
case ((value >= 1)):
result = "Between 1 and 25.";
break;
case (value == 0):
result = "Equals Zero.";
break;
}
return result;
}
So if I passed 29 even that I have two true cases the first one will be selected. My question is that how switch statement works in most of programming languages it will start comparing from the top or its only in this case (and is it good or bad to write it like that?).
switch statement checks for matches from top to bottom.
From MDN docs on switch statement:
If a match is found, the program executes the associated statements. If multiple cases match the provided value, the first case that matches is selected, even if the cases are not equal to each other.
I would do something like this (with if and else if chains):
function GetText(value) {
var result;
if (value == 0) {
result = "Equals Zero.";
} else if (value <= 25) {
result = "Between 1 and 25.";
} else if (value <= 50) {
result = "Between 26 and 50.";
}
return result;
}

Categories