samedi 1 octobre 2016

For loop with two counters, and confusing if-else statements.(JavaScript)

<!doctype html>
<html>
  <head>
    <script>
      function do_something() {
        var theBody = document.getElementsByTagName("body")[0];
        var theImg = document.createElement("img");
        theImg.src = "cat.png";
        theBody.appendChild(theImg.cloneNode(true));
        var count = 0;
        for (var i = 0; i < 10; i++, count++) {
            if (i == 5) continue;
            if (count == 3) {
                count = 0;
                theBody.removeChild(theBody.lastChild);
            } else {
                theBody.appendChild(theImg.cloneNode(true));
            }
            if (i > 7) break;
        }
      }
    </script>
  </head>
  <body onload="do_something()"></body>
</html>

I am supposed to tell how many images would a modern browser display.

I have two major doubts here:

  • When i=4, what is the value of count? I think it would be 0, but don't why I am confused about it.
  • As the code tells, when count = 0, an image will be removed from the body. Does the code append an image, and then remove an image? Or, does it simply remove one image? This is the part that is confusing because nothing is said about what happens when i=3.

According to the given answer, 6 images are added in the loop, and 2 are removed. So, a total number of 5 images are displayed.

Detecting if a all points have been checked in a JPanel

I created an array of about 11 buttons in a JPanel using springlayout, then set actionListener on them. However, I want to make the program able to detect when all buttons have been points have been checked, after which some action takes place. Here is my piece of code!

public class Beginner extends JPanel {

    static JButton quest;
    Random rand = new Random();

    int n = 10;

    static List <Point> points = new ArrayList<Point> ();

    Quest1 q1;
    Quest2 q2;
    Quest3 q3;
    Quest4 q4;
    Quest5 q5;
    Quest6 q6;
    Quest7 q7;
    Quest8 q8;
    Quest9 q9;
    Quest10 q10;
    Quest11 q11;

    public Beginner() {

              int radius = 200;
              Point center = new Point (250, 250);

              double angle = Math.toRadians(360 / n);

              points.add(center);

              for (int i = 0; i < n; i++) {
                  double theta = i * angle;

                  int dx = (int) (radius * Math.sin(theta));

                  int dy = (int) (radius * Math.cos(theta));

                  Point p = new Point (center.x + dx , center.y + dy);

                  points.add(p);

              }

              draw (points);
              }

               public void draw (List<Point> points) {

                   JPanel panels = new JPanel();

                   SpringLayout spring = new SpringLayout();

                   int count = 1;
                   for (Point point: points) {

                       quest = new JButton("Question " + count);
                       quest.setForeground(Color.BLACK);
                        Font fonte = new Font("Script MT Bold", Font.PLAIN, 20);
                        quest.setFont(fonte);

                       add (quest);
                       count++;

                       spring.putConstraint(SpringLayout.WEST, quest, point.x, SpringLayout.WEST, panels );

                       spring.putConstraint(SpringLayout.NORTH, quest, point.y, SpringLayout.NORTH, panels );

                       setLayout(spring);

                       panels.setOpaque(false);
                       panels.setVisible(true);
                       panels.setLocation(5,5);

                       add(panels);



                       quest.addActionListener (new ActionListener() {
                           public void actionPerformed (ActionEvent p) {


                                if (point.equals(points.get(0))) {


                                    q1 = new Quest1();


                                }   
                               else if (point.equals(points.get(1))) {

                                    q2 = new Quest2();

                                                        }
                               else if (point.equals(points.get(2))) {

                                       q3 = new Quest3();

                                                       }
                             else if (point.equals(points.get(3))) {

                                q4 =  new Quest4();
                        }

            else if (point.equals(points.get(4))) {

                q5 = new Quest5();
                                                    }

            else if (point.equals(points.get(5))) {

                q6 = new Quest6();

                                    }  
            else if (point.equals(points.get(6))) {

                q7 = new Quest7();

                                }  

 else if (point.equals(points.get(7))) {

    q8 = new Quest8();


                    }  

 else if (point.equals(points.get(8))) {

    q9 = new Quest9 () ;


        }  
 else if (point.equals(points.get(9))) {

    q10 = new Quest10() ;


                }  
 else if (point.equals(points.get(10))) {

    q11 =  new Quest11 ();

   // I would love to detect if all points have been checked, then some action takes place. This is where my problem is!



        }           
                } 
                       });
                   }

                        }
}

Current If statement involving String

//Essentially my assignment is supposed to take 2 words, calculate the smallest of the two which I have done, but now I need to reference the smallest word itself instead of the number. Which I believe requires an If statement, but I cannot get my String to initialize and the console gets picky when I end the program with the system.out.println command. 

Scanner scan = new Scanner(system.In); 
System.out.println(" Input first password ");
String pass1 = scan.nextLine();
Int pass1l = pass1.length();
System.out.println(" input second password "); 
String pass2 = scan.nextLine();
Int pass2l = pass2.length();
Int spassl = Math.min(pass1l,pass2l);
// problematic part here. 
String spass;
If (pass1l > pass2l){ spass = pass2}
else if (pass1l < pass2l) {spass = pass1}
else if (pass1l == pass2l) {spass = null;};
// Also the Else if statements come up as not statements        and the first is an illegal start of an expression.

// Then if I fix those I call spass with system.out and it says it's not initialized. I just started learning basic Java, I've used processing before but not for String related if statements, only integers.

Error when I use if inside if

This is my show.html.erb.

I'm just trying to use an if statement within another if, when the provider is the facebook use a specific url , if the provider is google use another method.

<% if current_user %>

  <%= if current_user.provider == 'google' %>
    <%= image_tag "#{current_user.image}?sz=100", style: "border-radius:50px" %>
  <% else %>
    <%= image_tag "http://ift.tt/2dy0rC4", style: "border-radius:50px" %>
  <% end %>

<%= current_user.name %>

<% else %>
  <%= link_to "Logar com o Facebook", "/auth/facebook", id: "sign_in", class: "btn btn-info" %>
  <%= link_to "Logar com o Google", "/auth/google_oauth2", id: "sign_in" , class: "btn btn-success" %>
<% end %>

but I'm getting this error:

Facegoogle/app/views/home/show.html.erb:5: syntax error, unexpected ')', expecting keyword_then or ';' or '\n' ...ent_user.provider == 'google' );@output_buffer.safe_append=' ... ^
/home/filipe/Sites/Facegoogle/app/views/home/show.html.erb:9: syntax error, unexpected keyword_else, expecting ')' '.freeze; else ^ 
/home/filipe/Sites/Facegoogle/app/views/home/show.html.erb:13: syntax error, unexpected keyword_end, expecting ')' '.freeze; end ^ 
/home/filipe/Sites/Facegoogle/app/views/home/show.html.erb:18: syntax error, unexpected keyword_else, expecting ')' '.freeze; else ^
/home/filipe/Sites/Facegoogle/app/views/home/show.html.erb:22: syntax error, unexpected keyword_end, expecting ')' '.freeze; end ^ 
/home/filipe/Sites/Facegoogle/app/views/home/show.html.erb:24: syntax error, unexpected keyword_ensure, expecting ')' 
/home/filipe/Sites/Facegoogle/app/views/home/show.html.erb:26: syntax error, unexpected keyword_end, expecting ')'

Java float comparison is not working even with Float.compare

I have _allowedPercentageDiscount = 0.0 and perentageDiscount = 1.0, First I tried with

if(_allowedPercentageDiscount < percentageDiscount)
//do something

but it never result true. Then, after some search, I tried with Float.compare but it still did not result true. Magically it gives correct result in evaluation window. (Android Studio).

enter image description here

What the hell is wrong and what to do?

Program with querying python list with multiple values using for loop and if in statements

I know the title is a little messy and if someone wants to fix it, they are more than welcome to.

Anyways, I'm having trouble querying a python list with multiple values, I have looked on other Stackoverflow questions and none of seem to match what I'm looking for.

So, this is the code I have so far, its supposed to use a for loop statement, so that it goes through each character and then uses and if in statements to check whether a character in the user input matches anything in the list.

In my example, it only uses symbols, but hopefully that shouldn't be much of a problem

Anyways here is the code

string = input("What symbol character would you like to check")
symbols=[' ', '!', '#', '$', '%', '&', '"', '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~',"'"]
def symbol():
    for x in symbols:
        if x =='!':
            return True
        elif x !='!':
            return False

if symbol():
    print('ok')
if not symbol():
    print('What Happened?')

How to show value in if statement in laravel

I am doing my project but got stuck in this. I want to print both value of degrees and experiences. How can i achieve this? My controller file is like below:

public function industryPost (Request $request) {
        $industry = $request->input('industry');

        if (empty($industry)) {
            return 'Industry can not be null';
        } else {

            $experiences = AreaOfExperience::where('industry_id', $industry)->where('is_active', 1)->where('is_deleted', 0)->get();
            $degrees = Degree::where('industry_id', $industry)->where('is_active', 1)->where('is_deleted', 0)->get();


            if (!empty($degrees) ) {
                $string2 = '';
                foreach ($degrees as $degree) {
                    $string2 .= '
                        <div class="col-md-4">
                            <div class="be-checkbox">
                                <label class="check-box">
                                    <input id="degree" type="checkbox" name="degrees[]" value="'.$degree->id.'" class="checkbox-input">
                                    <span class="check-box-sign"></span>
                                </label>
                                <span class="large-popup-text"> '.$degree->name.' </span>
                            </div>
                        </div>
                    ';
                }
                return response($string2);
            }
            if (count($degrees) == 0) {
                return 101;
            }

            if (!empty($experiences) ) {
                $string = '';
                foreach ($experiences as $experience) {
                    $string .= '
                        <div class="col-md-4">
                            <div class="be-checkbox">
                                <label class="check-box">
                                    <input id="experience" type="checkbox" name="area_of_experiences[]" value="'.$experience->id.'" class="checkbox-input">
                                    <span class="check-box-sign"></span>
                                </label>
                                <span class="large-popup-text"> '.$experience->name.' </span>
                            </div>
                        </div>
                    ';
                }
                return response($string);
            }
            if (count($experiences) == 0) {
                return 100;
            }


        }
    }

I want to print both the value of degree and experience. But in my query it prints the value of degree. how can i achieve this? Please help!