How to check conditions in succession? - java

if the first ad block is loaded -> stop checking and execute it -> if the first one is not loaded -> check the second one -> the second one is loaded -> stop checking -> the second one is not loaded -> check the third one .
Tried to execute through Switch case. Only the first block is executed. Please help me figure out what the problem is.
mDatabase = FirebaseDatabase.getInstance().getReference("Reklama");
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
// Switch between video ads and interstitial ads. if Reklama == 1 -> Interstitial / if Reklama == 2 -> Video.
int Reklama = Math.toIntExact((Long) snapshot.getValue());
//ad matching cycle
if (Reklama == 1) {
final int adUnit1 = 1;
final int adUnit2 = 2;
final int adUnit3 = 3;
int ReklamaInt = 1;
switch (ReklamaInt) {
case adUnit1:
mInterstitialAd.loadAd(adRequest);
break;
case adUnit2:
mInterstitialAdMid.loadAd(adRequest);
break;
case adUnit3:
mInterstitialAdLow.loadAd(adRequest);
break;
default:
throw new IllegalStateException("Unexpected value: " + ReklamaInt);
}
}
//ad matching cycle
else if (Reklama == 2) {
final int adUnit1 = 1;
final int adUnit2 = 2;
final int adUnit3 = 3;
int ReklamaReward = 1;
switch (ReklamaReward){
case adUnit1:
mRewardedAd.loadAd(adRequest);
break;
case adUnit2:
mRewardedAdMid.loadAd(adRequest);
break;
case adUnit3:
mRewardedAdLow.loadAd(adRequest);
break;
default:
throw new IllegalStateException("Unexpected value: " + ReklamaReward);
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});

Related

The textbox of the current Activity is not updated when another item is selected in the RadioButton of the previous Activity. Android/Java

The application should count the moments of inertia and moments of resistance of the profiles, and output the value of the selected profile in the TextView.
Application work:
The user enters the value of loads, deflection, length
Selects the required profile via radoiButton
Clicks on the "Execute calculation" button
The application reads and displays the value of the selected profile in the TextView
At the first launch, the application works correctly and everything is displayed. But when you exit to the previous activity to change the profile selection via radoiButton, the value of the text field does not change and remains the same as in the previous selection. Only restarting the entire application helps.
It turns out that to change the cross-section, you need to completely terminate the application each time and re-enter the data.
data input activity1
public void shvellerOnClick(View view) {
radioGroupShveller.removeAllViews();
RadioButton shvellerYRadioButton = new RadioButton(this);
RadioButton shvellerPRadioButton = new RadioButton(this);
shvellerYRadioButton.setText(R.string.shveller_Y);
shvellerPRadioButton.setText(R.string.shveller_P);
radioGroupShveller.addView(shvellerYRadioButton);
radioGroupShveller.addView(shvellerPRadioButton);
shvellerPRadioButton.setOnClickListener(radioButtonClickListener);
shvellerYRadioButton.setOnClickListener(radioButtonClickListener);
shvellerYRadioButton.setId(R.id.idShvellerYRadioButton);
shvellerPRadioButton.setId(R.id.idShvellerPRadioButton);
radioGroupShvellerX2.removeAllViews();
radioGroupDvutavr.removeAllViews();
radioGroupShvellerX2.clearCheck();
radioGroupDvutavr.clearCheck();
}
public void dvaShvelleraOnClick(View view) {
radioGroupShvellerX2.removeAllViews();
RadioButton shvellerYX2RadioButton = new RadioButton(this);
RadioButton shvellerPX2RadioButton = new RadioButton(this);
shvellerYX2RadioButton.setText(R.string.shveller_Y_x2);
shvellerPX2RadioButton.setText(R.string.shveller_P_x2);
radioGroupShvellerX2.addView(shvellerYX2RadioButton);
radioGroupShvellerX2.addView(shvellerPX2RadioButton);
shvellerYX2RadioButton.setOnClickListener(radioButtonClickListener);
shvellerPX2RadioButton.setOnClickListener(radioButtonClickListener);
shvellerYX2RadioButton.setId(R.id.idShvellerYX2RadioButton);
shvellerPX2RadioButton.setId(R.id.idShvellerPX2RadioButton);
radioGroupShveller.removeAllViews();
radioGroupDvutavr.removeAllViews();
radioGroupShvellerX2.clearCheck();
radioGroupDvutavr.clearCheck();
}
View.OnClickListener radioButtonClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
RadioButton rb = (RadioButton)v;
switch (rb.getId()) {
case R.id.idShvellerYRadioButton:
valueSelectedGost = 0;
break;
case R.id.idShvellerPRadioButton:
valueSelectedGost = 1;
break;
case R.id.idShvellerYX2RadioButton:
valueSelectedGost = 10;
break;
case R.id.idShvellerPX2RadioButton:
valueSelectedGost = 11;
break;
case R.id.idDvutavBRadioButton:
valueSelectedGost = 20;
break;
case R.id.idDvutavKRadioButton:
valueSelectedGost = 21;
break;
}
}
};
public void vypolnitRaschetOnClick(View view) {
int putGost = valueSelectedGost;
Intent performCalculationIntent = new Intent(StBalkaSchema1CopyActivity.this, StBalkaShema1RaschetCopyActivity.class);
performCalculationIntent.putExtra("gostInt", putGost);
startActivity(performCalculationIntent);
}
Activity2 calculation
Intent intent = getIntent();
int getGostInt = intent.getIntExtra("gostInt",0);
selectedProfileImageView = (ImageView) findViewById(R.id.selectedProfileImegeView);
selectedProfileTextView = (TextView) findViewById(R.id.selectedProfileTextView);
infoSelectedProfileTextView = (TextView) findViewById(R.id.infoSelectionProfileTextView);
double loadToFormula = Double.parseDouble(loadTextView.getText().toString());
double lengthToFormula = Double.parseDouble(lengthTextView.getText().toString());
double deflectionToFormula = Double.parseDouble(deflectionTextView.getText().toString());
double resultWtr = resultMmax * 100 / (1.12*2.1);
double resultItr = resultMmax * Math.pow(10, 5) * lengthMToFormula * Math.pow(10, 2) * deflectionToFormula / (10 * 2.1 * Math.pow(10, 6));
if (getGostInt >= 0 & getGostInt <= 9){
selectedProfileImageView.setImageResource(R.drawable.shveller);
if (getGostInt == 0){
ShvellerU_GOST_8240_89.addShvellerU();
infoSelectedProfileTextView.setText(ShvellerU_GOST_8240_89.getClosestInertiaResistance(resultItr, resultWtr));
selectedProfileTextView.setText(R.string.shveller_Y);
} else if (getGostInt == 1){
ShvellerP_GOST_8240_89.addShvellerP();
infoSelectedProfileTextView.setText(ShvellerP_GOST_8240_89.getClosestInertiaResistance(resultItr, resultWtr));
selectedProfileTextView.setText(R.string.shveller_P);
}else {
infoSelectedProfileTextView.setText("Профиль не выбран");
selectedProfileTextView.setText("Профиль не выбран");
}
} else if (getGostInt >= 10 & getGostInt <= 19){
selectedProfileImageView.setImageResource(R.drawable.dva_shvellera);
if(getGostInt == 10){
ShvellerUx2_GOST_8240_89.addShvellerUx2();
infoSelectedProfileTextView.setText(ShvellerUx2_GOST_8240_89.getClosestInertiaResistance(resultItr, resultWtr));
selectedProfileTextView.setText(R.string.shveller_Y_x2);
} else if (getGostInt == 11){
ShvellerPx2_GOST_8240_89.addShvellerPx2();
infoSelectedProfileTextView.setText(ShvellerPx2_GOST_8240_89.getClosestInertiaResistance(resultItr, resultWtr));
selectedProfileTextView.setText(R.string.shveller_P_x2);
} else {
infoSelectedProfileTextView.setText("Профиль не выбран");
selectedProfileTextView.setText("Профиль не выбран");
}
}
}
Profile adding method
public class ShvellerU_GOST_8240_89 extends Shveller {
public ShvellerU_GOST_8240_89(String name, double momentSoprotivleniya, double momentInertsii, double massa) {
super(name, momentSoprotivleniya, momentInertsii, massa);
}
public static void addShvellerU() {
ShvellerU_GOST_8240_89 shveller5U = new ShvellerU_GOST_8240_89("5У", 9.1,22.8,4.84);
shveller5U.putIn(shveller5U);
ShvellerU_GOST_8240_89 shveller6_5U = new ShvellerU_GOST_8240_89("6,5У", 15,48.6,5.9);
shveller6_5U.putIn(shveller6_5U);
ShvellerU_GOST_8240_89 shveller8U = new ShvellerU_GOST_8240_89("8У", 22.4,89.4,7.05);
shveller8U.putIn(shveller8U);
ShvellerU_GOST_8240_89 shveller10U = new ShvellerU_GOST_8240_89("10У", 34.8,174.0,8.59);
shveller10U.putIn(shveller10U);
}
}
The error will most likely occur in this block activity2 calculation.
if (getGostInt == 0){
ShvellerU_GOST_8240_89.addShvellerU();
infoSelectedProfileTextView.setText(ShvellerU_GOST_8240_89.getClosestInertiaResistance(resultItr, resultWtr));
selectedProfileTextView.setText(R.string.shveller_Y);
I think that this is due to the fact that when objects are added through the addShvellerU () method, they remain in memory and when a different section is selected, the addDvutavrK () method cannot overwrite another. How to solve this problem?

How to change ImageView of dice roller based on random result

I am building a Fate Dice roller app for my finishing course in App Development and I am having a problem with changing the ImageView of the dice based on the result. Here is the code(the roller is in a fragment tab):
public class RollDiceFragment extends Fragment {
private Button btnRoll;
private EditText et_rollDice;
private ImageView iv_dice1, iv_dice2, iv_dice3, iv_dice4;
Random random;
public RollDiceFragment() {
}
public static RollDiceFragment newInstance() {
RollDiceFragment fragment = new RollDiceFragment();
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_rolldice, container, false);
random = new Random();
btnRoll = rootView.findViewById(R.id.btnRoll);
et_rollDice = rootView.findViewById(R.id.et_rollDice);
iv_dice1 = rootView.findViewById(R.id.iv_dice1);
iv_dice2 = rootView.findViewById(R.id.iv_dice2);
iv_dice3 = rootView.findViewById(R.id.iv_dice3);
iv_dice4 = rootView.findViewById(R.id.iv_dice4);
btnRoll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RollDiceFragment.this.rollDice();
}
});
return rootView;
}
private void rollDice(){
int d1 = this.random.nextInt(3) -1;
int d2 = this.random.nextInt(3) -1;
int d3 = this.random.nextInt(3) -1;
int d4 = this.random.nextInt(3) -1;
String print = (((BuildConfig.FLAVOR + addResult(d1)) + addResult(d2)) + addResult(d3)) + addResult(d4);
int diceSum = ((d1 + d2) + d3) + d4;
if (diceSum > 0){
this.et_rollDice.setText("+" + Integer.toString(diceSum));
}else {
this.et_rollDice.setText(Integer.toString(diceSum));
}
}
public String addResult(int i){
if (i == 1){
return "+";
}
if (i == 0){
return "0";
}
return "-";
}
}
The random roller works fine on the emulator and the result is shown in the editText line. I would just like the images to change based on the result of each dice. If a dice is showing a "-" the image shown should be diceminus.jpg.... Any help would be appreciated =)
The problem is that you are passing an int to setText method.
On the else case change it to using the setText overload with String as parameter:
this.et_rollDice.setText("" + Integer.toString(diceSum));
I found an answers so I will post it here in case somebody else needs the code one day =)
switch (d1){
case -1:
iv_dice1.setImageResource(R.drawable.diceminus);
break;
case 0:
iv_dice1.setImageResource(R.drawable.dicezero);
break;
case +1:
iv_dice1.setImageResource(R.drawable.diceplus);
break;
}
switch (d2){
case -1:
iv_dice2.setImageResource(R.drawable.diceminus);
break;
case 0:
iv_dice2.setImageResource(R.drawable.dicezero);
break;
case +1:
iv_dice2.setImageResource(R.drawable.diceplus);
break;
}
switch (d3){
case -1:
iv_dice3.setImageResource(R.drawable.diceminus);
break;
case 0:
iv_dice3.setImageResource(R.drawable.dicezero);
break;
case +1:
iv_dice3.setImageResource(R.drawable.diceplus);
break;
}
switch (d4){
case -1:
iv_dice4.setImageResource(R.drawable.diceminus);
break;
case 0:
iv_dice4.setImageResource(R.drawable.dicezero);
break;
case +1:
iv_dice4.setImageResource(R.drawable.diceplus);
break;
}

How to set textview as method output in andrioid?

I have just started to use android studio recently and currently I am working on a Roman Numeral Translator app. The app's interface looks like this: Application interface
The user will use the keypad to input a integer which will show up on the TextView shown above. When they hit the convert button it will take the integer they have entered and convert it (the program will be able to catch the input if contains a string or character). Then the app will reset the TextView to the result after the user hits the "convert" button.
Currently, my main activity contains the onClickListeners for the buttons and a separate translator method for the translations. My problem is with the "convert" button I'm not sure how to get the input from the translator method and set it as TextView when it finishes converting. Here is the sample of my code:
"Convert" button listener- `
convert.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
TextView numeralInput = (TextView) findViewById(R.id.textView);
String intValue = numeralInput.getText().toString();
try{
int integer = Integer.parseInt(intValue);
if (integer > 0 && integer <= 4999){
translator(integer);
}else{
numeralInput.setText("Please enter an integer between 0 and 4,999.");
}
}catch(NumberFormatException e){
numeralInput.setText("Invalid input try again.");
}
}
}
);
`
Translator Method- `
public static void translator(int integer) {
LinkedList<String> stack = new LinkedList<String>();
// if (integer > 0 && integer <= 4999) {
//ArrayList<Integer> placement = new ArrayList<Integer>();
int place = (int) Math.log10(integer);
for (int i = 0; i <= place; i++) {
//while(integer > 0){
//System.out.println(integer);
int placeOfValue = integer % 10;
//stack.push(placeOfValue);
//System.out.print(stack);
//System.out.print(placeOfValue +":" + i);
String placement = "";
switch (i) {
case 0:
placement = ones(placeOfValue);
break;
case 1:
placement = tens(placeOfValue);
break;
case 2:
placement = hundreds(placeOfValue);
break;
case 3:
placement = thousands(placeOfValue);
break;
default:
break;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
stack.push(placement);
}
integer = integer / 10;
//System.out.print(placement);
// System.out.println(placement.size());
//}
// for(int j = 0; j < placement.size(); j++){
// double tenthPower = Math.floor(Math.log10(placement.get(j)));
// double place = Math.pow(10, tenthPower);
// System.out.println(place);
//
// }
// }
while (!stack.isEmpty()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
System.out.print(stack.pop());
}
}
// } else {
// System.out.println("Please enter an integer between 0 and 4,999.");
// }
}
}
`
The other methods inside translator is like a library for the roman numerals each containing a numeral for each of the place values as shown below.
Thousands method- `
public static String thousands(int integer) {
String thouValue = "";
switch (integer) {
case 1:
thouValue = "M";
//System.out.print("M");
break;
case 2:
thouValue = "MM";
//System.out.print("MM");
break;
case 3:
thouValue = "MMM";
//System.out.print("MMM");
break;
case 4:
thouValue = "MMMM";
//System.out.print("MMMM");
break;
default:
thouValue = "";
break;
}
return thouValue;
}
`
Make your translator() method return a string which contains the final output.
So before the while statement in that method, declare a string such as
String result = null; and in the loop append the popped values to this variable like, result += stack.pop().
Now, the place where you call the translator(integer) method, do numeralInput.setText(translator(integer)) instead of translator(integer)
You need to make your TextView a class member and initialise it in your oncreate bundle, so that you can access the textview elsewhere in the activity.
TextView numeralInput;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_main);
numeralInput = (TextView) findViewById(R.id.textView);
convert.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
String intValue = numeralInput.getText().toString();
try{
int integer = Integer.parseInt(intValue);
if (integer > 0 && integer <= 4999){
translator(integer);
}else{
numeralInput.setText("Please enter an integer between 0 and 4,999.");
}
}catch(NumberFormatException e){
numeralInput.setText("Invalid input try again.");
}
}
}
);

can't catch Exception

When I start my tests I want to see an Exception, but programm just stay on the one line "mobileTelephony.driver" and don't throught exception. I don't understand why?
#Test(groups = {"non-basic"})
#Parameters({"idCategory"})
public void checkSearchForm(int idCategory) throws InterruptedException {
String categoryName;
int location = 1;
StackOfCategories sub1Stack = TestSuiteMobileTelephony.sub1Stack;
boolean isItSubCategory;
basePage.getBasePage();
basePage.clickCategoryName(idCategory);
MobileTelephonyPage mobileTelephony = PageFactory.initElements(basePage.driver, MobileTelephonyPage.class);
while (location <= 3) {
mobileTelephony.clickChangeLocation(location);
for(int i = 1; i <= sub1Stack.size(); i++) {
if (location == 1) {
categoryName = sub1Stack.getCategory(i).getNameEn();
} else if (location == 2) {
categoryName = sub1Stack.getCategory(i).getNameRu();
} else {
categoryName = sub1Stack.getCategory(i).getNameUk();
}
mobileTelephony.writeInSearchFormAndClick(categoryName);
try {
mobileTelephony.driver.findElement(By.xpath(".//div[#id='breadcrumbs']/span1"));
} catch(Exception e){
e.printStackTrace();
mobileTelephony.back();
}
isItSubCategory = true;
AssertMessage.assertTrueNavigateSubCategory(categoryName, isItSubCategory);
mobileTelephony.back();
}
location++;
}
}
Mobail Telefony code
public class MobileTelephonyPage extends BasePage {
public void clickAndWriteNumber(String number) throws AWTException {
String[] numsArray = number.split("");
number1.clear();
number1.click();
Robot robot = new Robot();
// Constryction
for(int i = 0; i < numsArray.length; i++) {
switch(Integer.parseInt(numsArray[i])) {
case 0 :
robot.keyPress(KeyEvent.VK_0);
break;
case 1 :
robot.keyPress(KeyEvent.VK_1);
break;
case 2 :
robot.keyPress(KeyEvent.VK_2);
break;
case 3 :
robot.keyPress(KeyEvent.VK_3);
break;
case 4 :
robot.keyPress(KeyEvent.VK_4);
break;
case 5 :
robot.keyPress(KeyEvent.VK_5);
break;
case 6 :
robot.keyPress(KeyEvent.VK_6);
break;
case 7 :
robot.keyPress(KeyEvent.VK_7);
break;
case 8 :
robot.keyPress(KeyEvent.VK_8);
break;
case 9 :
robot.keyPress(KeyEvent.VK_9);
break;
}
}
}
public MobileTelephonyPage(WebDriver driver) {
super(driver);
}
public int getHeightImg(int number) {
int height = driver.findElement(By.xpath("(.//div[#class='icon']/img)[" + number + "]")).getSize().getHeight();
return height;
}
public int getWidthImg(int number) {
int width = driver.findElement(By.xpath("(.//div[#class='icon']/img)[" + number + "]")).getSize().getWidth();
return width;
}
public MobileTelephonyPage back() {
driver.navigate().back();
return this;
}
public String getCurrentURL() {
return driver.getCurrentUrl();
}
public void clickOperator(String linkText) {
driver.findElement(By.linkText(linkText)).click();
}
}
in debug program stop in the next snipet of code(class HttpCommandExecutor)
this.log("profiler", new HttpProfilerLogEntry(command.getName(), true));
HttpResponse e = this.client.execute(httpRequest, true);
this.log("profiler", new HttpProfilerLogEntry(command.getName(), false));
No Exception is thrown.
Probably the code needs a long time to be executed or it is in a livelock.
A livelock is a situation when a function is executed but never ends. For example because in a for loop you loose to update a variable so the test is always true
Edited after new informations
From the javadoc of WebDriver:
This method is affected by the 'implicit wait' times in force at the
time of execution. The findElement(..) invocation will return a
matching row, or try again repeatedly until the configured timeout is
reached. findElement should not be used to look for non-present
elements, use findElements(By) and assert zero length response
instead.
As you can see the the function could not returns exactly as mentioned on the first two lines of my post.

Java: Using created string as loop parameter?

In short, the user will input a number (say 1 through 3). This will decide which range of numbers the loop should search through.
switch(input){
case 1:
searchTerm = "i<10 && i>5";
case 2:
searchTerm = "i>=10 && i<19";
case 3:
searchTerm = "i>19 && i<24";
}
while(searchTerm){
//some function
}
Is this possible? I I've not been able to find a way to use a string as search parameters.
EDIT: I don't think I did a very good job of explaining why I needed this. What is one to do if there are different numbers of parameters? For example:
case 1:
searchTerm = "i<5"
case 2:
searchTerm = "i>25 && i<29"
case 3:
searchTerm = "(i<50 && i>25) && (i>55 && i<75)"
case 4:
searchTerm = "(i<20 && i>15) && (i>300 && i<325) && (i>360 && i<380)
Then how does one do it? Multiple loops that call the same function?
The correct way to do this is to not use a string at all:
int min, max;
switch(input){
case 1: // i<10 && i>5
min = 6;
max = 10;
break; // to avoid follow-through to the next case
case 2: // i>=10 && i<19
min = 10;
max = 20;
break;
case 3: // i>19 && i<24
min = 20;
max = 25;
break;
default:
// You need something here in case the value entered wasn't 1-3
}
for (int i = min; i < max; ++i) {
// ...
}
Re your edit:
I don't think I did a very good job of explaining why I needed this. What is one to do if there are different numbers of parameters?
In that case, you'll have to use an expression evaluator (or write one, which is a non-trivial task). There's one in Spring, for instance (not recommending, just happened to hear about it). A search for "Java expression evaluator" should turn up some options.
Another alternative, which is somewhat amusing given that some folks mistook your question for a JavaScript question, is to use the JavaScript evaluator built into Java (either Rhino or Nashorn). E.g.: Live Example
import javax.script.*;
class Ideone {
public static void main(String[] args) throws java.lang.Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
String searchTerm = "i >= 19 && i <= 24";
int i;
try {
i = 19;
engine.put("i", i);
while ((boolean)engine.eval(searchTerm)) {
System.out.println("i = " + i);
++i;
engine.put("i", i);
}
System.out.println("Done");
} catch (ScriptException scriptException) {
System.out.println("Failed with script error");
}
}
}
...but you'll still have the problem of determining what initial value to use for i, which I've hardcoded above.
In Java 8 you can select a lambda instead of String:
Predicate<Integer> searchTerm = (Integer v) -> false;
switch (input) {
case 1:
searchTerm = (Integer v) -> v < 10 && v > 5;
break;
case 2:
searchTerm = (Integer v) -> v >= 10 && v < 19;
break;
case 3:
searchTerm = (Integer v) -> v > 19 && v < 24;
break;
}
while (searchTerm.test(i)) {
...
}
You can create an enumeration as below.
public enum SearchTerms {
None(""),
Between6And9("i<10 && i>5"),
Between10And18("i>=10 && i<19"),
Between20And23("i>19 && i<24");
private final String stringValue;
SearchTerms(String stringValue) {
this.stringValue = stringValue;
}
public String getStringValue() {
return stringValue;
}
public static SearchTerms fromStringValue(String stringValue) {
for (SearchTerms searchTerm : values()) {
if (searchTerm.getStringValue().equalsIgnoreCase(stringValue)) {
return searchTerm;
}
}
return SearchTerms.None;
}
}
Usage:
SearchTerms searchTerm = SearchTerms.fromStringValue("i<10 && i>5");
switch(searchTerm) {
case Between6And9:
//dosomething
break;
}
You can use .eval() of JavaScript.
Also don't forget break; at the end of each case:
Check out this fiddle.
Here is the snippet.
function test(input, i) {
switch (input) { //input=1
case 1:
searchTerm = "i<10 && i>5"; //this will be 'searchTerm'
break;
case 2:
searchTerm = "i>=10 && i<19";
break;
case 3:
searchTerm = "i>19 && i<24";
break;
}
while (eval(searchTerm)) { //'searchTerm' converted to boolean expression
alert(i); // alert for i=7,8,9
i++;
}
}
test(1, 7); //pass input=1 and i=7

Categories