samedi 27 février 2016

jquery Increasing score after matching random numbers

I've been trying to figure out on the how to do an increment each time the button is clicked after the random numbers matches, or doesn't match.

Basically, if the numbers matched, a counter for matched will go up. Same goes for mismatched numbers.

But currently, the counter will only go up once and only once.

jQuery:

 $(document).ready(function(){
    var a;
    var b;
    display();
    displayInt();

    function displayInt(){
        var disInt;
        disInt = setInterval(display, 2000);
    }    

    function display() {

        a = Math.floor((Math.random()*3)+1);
        $("#number1").html(a);
        b = Math.floor((Math.random()*3)+1);
        $("#number2").html(b);    
    }

    function addScore(){
        var correct;
        var wrong; 
        correct=0;
        wrong=0;

        if (a == b){
            correct++;
            $("#correctScore").html(correct);

        }else{

            wrong++;
            $("#wrongScore").html(wrong);    
        }
    }

    $('input[type="button"]').click(function() {

        a = $("#number2").html();
        b = $("#number1").html();
        addScore();

    });    
});

if statement not resolving despite condition being met C++

I'm working on a university assignment wherein I have to re-create the classic Frogger game. Having trouble with tyre movement.

for (int tZ = 0; tZ < kTyreColumnLength; tZ++)
{
    for (int tX = 0; tX < kTyreRowLength; tX++)
    {
        eachTyre = tX + tZ;
        tyre[eachTyre] = tyreMesh->CreateModel(0, -2.50, 0);
        tyre[eachTyre]->Scale(10);
        tyre[eachTyre]->SetX(tyreXs[tX]);
        tyre[eachTyre]->SetZ(tyreZs[tZ]);

        if (tZ % 2 == 0)
        {
            tyreMovingLeft[eachTyre];
        }
    }

}

Basically, the way I'm drawing my tyres is through nested for loops. The inner loop draws3 tyres, and is repeated for each row (outer loop)

I'm then trying to assign each "even" row tyre, a Boolean to track its direction.

tZ % 2 ==0 IS correctly resolving to true/false each time the outer loop iterates, the debugger says the condition IS being met, but it's just never executing.

vendredi 26 février 2016

How to change option by selecting different values in javascript

I have two condition, if i am selecting no.1 from the drop down list then i want to see option 1 and if i am selecting no.2 from the drop down list then i want to see option 2.

     var mylist=document.getElementById("mylist");

         if(mylist.value == "1")
           {
            document.getElementById('myalllist').getElementsByTagName('option')[1].selected = 'selected';
           }
         if (mylist.value == "2")
          {

            document.getElementById('myalllist').getElementsByTagName('option')[2].selected = 'selected';
          }


  HTML

    <select id="mylist" onchange="fndropoption()">
     <option>Select</option>
     <option>1</option>
     <option>2</option>
   </select>       

    <select id="myalllist" onchange="fndropoption()">
     <option value="1">xxxxxxxx</option> 
     <option value="2">yyyyyyyy</option>
    </select>

SO, If i select 1 from my dropdown list, then option[1] should appear on my next drop down list and if i select 2 from dropdown list, then option[2] should appear on my next drop down list

Shipping Charges Calculator miss calculation

The assignment has the following rates: Weight of Package Rate per 500 miles shipped 2 pounds or less $1.10 over 2 pounds but not more than 6 pounds $2.20 over 6 pounds but not more than 10 pounds $3.70 over 10 pounds $3.80

The shipping charge per 500 miles are not prorated. For example, if a 2 pound package is shipped 502 miles, the charge would be $2.20. Write a program that ask the user to enter the weight of a package and then display the shipping charges.

My issue is that im getting wrong answers. This is what i got so far:

import java.util.Scanner;
public class ShippingCharges
{
public static void main (String [] args)
{
    double mDrive, rMiles, wPound;

    Scanner keyboard = new Scanner (System.in);

    System.out.print ("Enter Weight of Package: ");
    wPound = keyboard.nextDouble();
    System.out.println("");

    System.out.print ("Enter Miles Driven: ");
    mDrive = keyboard.nextDouble();
    System.out.println("");

    rMiles = mDrive / 500;

    if (wPound <2)
    {
        System.out.println ("You will be charged "+ "$" + Math.ceil(rMiles)*1.10);
    }

    if (wPound >=2 && wPound <6)
    {
        System.out.println ("You will be charged "+ "$" + Math.ceil(rMiles)*2.20);
    }

    if (wPound >=6 && wPound <10)
    {
        System.out.println ("You will be charged "+ "$" + Math.ceil(rMiles)*3.70);
    }

    if (wPound >= 10)
    {
        System.out.println ("You will be charged "+ "$" + Math.ceil(rMiles)*3.80);
    }

}
}

Following the example, the program should do 502/500 *2.2 which is 2.2 and the program is showing 4.4. Any suggestion?

What is an efficient way to move in a specific direction relative to its data?

So I'm working on a mini-game where you are a player in an arena with monsters. The goal is to kill all the monsters by shooting them. You can move up/right/down/left, and so can the monsters. If the monsters touch you, you die.

I have to create a player AI for the player to try to stay alive.

So I have defined an integer for each "Danger Zone" for each direction (DangerUp/DangerDown/DangerRight/DangerLeft) and initialized them all to zero. So it looks like this:

int DangerUp = 0, DangerDown = 0, DangerRight = 0, DangerLeft = 0;

If there is a monster above of me, dangerUp will increment by one. If there is one below me, dangerDown will increment by one. Same with the other two.

So what I'm trying to write (in psuedeocode) is essentially:

if I'm surrounded by three (which means 3 of the 4 Danger variables have a value of 1), move to the direction where the danger is 0.

I was planning on doing this:

if (DangerUP == 1 && DangerDOWN == 1 && DangerLEFT == 1 && DangerRIGHT == 0) // if surrounded by 3 in the up, down, left direction

{
// move right;
player_x_direction++; // (this moves my character one direction to the right)

}

and then I plan on doing three else if's where I test for all combinations of having 3 of the 4 variables equal to 1.

Is there a simpler way to do this where I can just move in the direction of whatever danger direction is = 0?

auto delete new characters javascript

I'm working on a lab for school, and the goal is to create a web control to will only accept a monetary value from the user.

Rules:
1st digit must be “-“ or “$”
2nd digit must be “$” or a number
3rd digit on must be a number or a “.”
Only two digits after decimal
Must have a “$”

The problem I'm having, is that while I can set up labels to display when something is or is not allowed, I can't figure out how to get it to auto delete new characters when the conditions haven't been met. (E.G. can't enter "-1", only "-$" or "$1".) Trying to set the substring's length doesn't seem to be working. The condition in the current example also only seems to work when the string consists only of "-". Any extra characters added do not trigger it.

Code:

    function text2money(e) {
        // get value of txt
        var str = document.getElementById("<%=txt.ClientID %>").value;

        if (str.substring(0, str.length) === "-" && str.substring(1, str.length) !== "$") {
                str.length = 2;
        }

        // goes through string one character at a time, converts them to char, then checks if char is a decimal digit
        if (str.substring(0, 1) === ('-') || str.substring(0, 1) === ('$')) {
            document.getElementById("<%= lbl1.ClientID %>").innerHTML = "True"; // must use .innerHTML with labels

            // check if second digit is '$' or numeric
            if (str.substring(1, 2) === ("$") || isFinite(str.substring(1, 2))) {
                document.getElementById("<%= lbl1.ClientID %>").innerHTML = "It's numeric";

                // check if third and any future characters are '.' or numeric

            } // end second char if

            else {
                document.getElementById("<%= lbl1.ClientID %>").innerHTML = "Second char must be '$' or numeric";
            } // end second char

        } // end first char if
        else {
            document.getElementById("<%= lbl1.ClientID %>").innerHTML = "First character must be '-' or '$'";
        } // end first char

        // check if $ is included anywhere in the string
        var result = str.indexOf("$") <= -1; // -1 == false
        if (result) {
            document.getElementById("<%= lbl2.ClientID %>").innerHTML = "$ is a required character";
            }
            else {
                document.getElementById("<%= lbl2.ClientID %>").innerHTML = "";
            }

        } // end text2money

sampleVariable As Boolean = My.settings.sampleSettings (boolean dataType) doesn't work

I have a 2 scenarios that involves using My.Settings.sampleSettings with a dataType boolean and a sampleVariable as data type boolean.

Code: Since both sampleVariable and sampleSettings are boolean I declare them like that

Dim sampleVariable As Boolean = My.Settings.sampleSettings
Console.WriteLine("Result: " & sampleVariable)
If sampleVariable = False Then
    Console.WriteLine("1")
    sampleVariable = True
Else
    Console.WriteLine("2")
    sampleVariable = False
End If
My.Settings.Save()

Output: The output seems to not satisfy the condition 1, it always satisfies the condition 2 which is false

Result: False
1
Result: False
1
Result: False
1

Code: In this code I didn't put the sampleSettings to a boolean variable and it is working fine.

Console.WriteLine("Result: " & My.Settings.sampleSettings)
If My.Settings.sampleSettings = False Then
    Console.WriteLine("1")
    My.Settings.sampleSettings = True
Else
    Console.WriteLine("2")
    My.Settings.sampleSettings = False
End If
My.Settings.Save()

Output: Whenever I click the button it triggers a different condition, this is my goal.

Result: False
1
Result: True
2
Result: False
1

Question: How do I properly contain the My.Settings.sampleSettings to a boolean variable?