#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    9
    Rep Power
    0

    Problem: Why is this If statement lying to me?


    I've almost finished an Javascript application I'm working on that displays data from Google maps. But I've just run into a strange problem when trying to change the condition which determines when the results are displayed.

    The part in question is the following:

    Code:
    <!-- Not sure what's going on here -->
    var TestThis=0;
    					 
    if (TestThis=1000){
    WriteToTable(iPlaceDisplayNum, place.name, place.rating, xScoreFinal, iProspect, place.url, place.formatted_phone_number);
    
    alert ('This alert should not be displaying.');
    }
    When trying to change when the WriteToTable function runs, I noticed a problem. So I put the if(TestThis=1000) in there just as a test. When the script runs with this test the function still runs and the alert is displayed.

    I'm not sure what's going on?

    I've included all the code below if context is needed. The part in question can be found if you search for "TestThis".

    Code:
    <!DOCTYPE html>
    <html>
    
    <meta charset="utf-8">
    <title>Review Prospecter V1.0
    </title>
    <link href="https://developers.google.com/maps/documentation/javascript/examples/default.css" rel="stylesheet">
    <script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places,visualization&v=3.exp"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    
    <center>
        <h2>REVIEW PROSPECTER V1.0</h2>
    </center>
    
    <div id="map-canvas" style="height:40%; width: 40%; float:center"></div>
    <div style="height:0%; width:0%; float:left"></div>
    <br />
    
    <div id="mydiv" ; style="float:center"></div>
    
    
    Keyword:
    <input type="text" value="taxi" id="text_keyword" />
    <br>
    
    Lattitude:
    <input type="text" value=51.412818 id="text_lattitude" />
    <br>Longitude:
    <input type="text" value=-0.301248 id="text_longitude" />
    <br>Radius (0-6):
    <select id="drop_radius">
        <option value=0>0</option>
        <option value=1>1</option>
        <option value=2>2</option>
        <option value=3 selected="selected">3</option>
        <option value=4>4</option>
        <option value=5>5</option>
        <option value=6>6</option>
    </select>
    <br>
    
    
    <button id="button" , type="button">Start</button>
    
    <table id="printTable" , border=1>
        <thead>
            <tr>
                <th>#</th>
                <th>Name</th>
                <th>Google Rating
                </th>
                <th>David's Score
                </th>
                <th>Prospect</th>
                <th>Google Place URL
                </th>
                <th>Phone Number
                </th>
            </tr>
        </thead>
        <tbody></tbody>
    </table>
    
    
    
    <script>
    var map;
    var infoWindow;
    var service;
    
    var iPlaceDisplayNum = 0;
    
    var iKeyword;
    var iRadius;
    var iLattitude;
    var iLongitude;
    
    
    
    
    function initialize() {
        map = new google.maps.Map(document.getElementById('map-canvas'), {
            mapTypeId: google.maps.MapTypeId.ROADMAP,
           
            center: new google.maps.LatLng(iLattitude, iLongitude),
            zoom: iRadius,
            styles: [{
                stylers: [{
                    visibility: 'simplified'
                }]
            }, {
                elementType: 'labels',
                stylers: [{
                    visibility: 'off'
                }]
            }]
        });
    
        infoWindow = new google.maps.InfoWindow();
        service = new google.maps.places.PlacesService(map);
    
        google.maps.event.addListenerOnce(map, 'bounds_changed', performSearch);
    }
    
    function performSearch() {
        var request = {
            bounds: map.getBounds(),
            keyword: iKeyword
        };
        service.radarSearch(request, callback);
    
    }
    
    function requestInfoWrapper(results, i) {
        i = i + 1;
        if (i >= results.length) {
    
           
            WriteToTable('-', '-', '-', '-', '-', '__', '-');
            alert('Listing Complete');
            return
        };
    
        ShowResults(results[i]);
        setTimeout(function () {
            requestInfoWrapper(results, i);
        }, 500);
    
    }
    
    function callback(results, status) {
        if (status != google.maps.places.PlacesServiceStatus.OK) {
            alert(status);
            return;
        }
        alert('Raw Data Results: ' + results.length + ' for ' + iKeyword + ' at Lat: ' + iLattitude + ' Long: ' + iLongitude);
        requestInfoWrapper(results, -1);
    
    }
    
    function ShowResults(place) {
    
        service.getDetails(place, printme);
    
    }
    
    
    function printme(place, status) {
        if (status == google.maps.places.PlacesServiceStatus.OK) {
    
    
            if (typeof place.reviews !== 'undefined') {
    
                var xScore = 0;
                var xGlobal = 0;
    
                for (var i = 0; i < place.reviews.length; i++) {
    
                    reviews = place.reviews[i];
    
    
    
                    for (var x = 0; x < reviews.aspects.length; x++) {
                        aspectr = reviews.aspects[x];
                        xScore += aspectr.rating;
                        xGlobal++;
                    }
                }
                var xScoreFinal = (xScore / xGlobal);
    
            }
    
            if (typeof xScoreFinal !== 'undefined') {
                iPlaceDisplayNum++;
    
                var iProspect;
                if (xScoreFinal < 2.3) {
                    iProspect = 'Yes';
                }
               
    					  
    		<!-- Not sure what's going on here -->
    		var TestThis=0;
    					 
                if (TestThis=1000){
    	      WriteToTable(iPlaceDisplayNum, place.name, place.rating, xScoreFinal, iProspect, place.url, place.formatted_phone_number);
    		alert ('This alert should not be displaying.');
    		}
    
            }
        }
    }
    
    
    $(document).ready(function () {
        $('#button').click(function (e) {
            e.preventDefault();
       
            iKeyword = $('#text_keyword').val();
            var tRadius = $('#drop_radius').val();
            iLattitude = $('#text_lattitude').val();
            iLongitude = $('#text_longitude').val();
    
            iRadius = (16 - tRadius);
    
            initialize();
        });
    });
    
    
    
    function WriteToTable(vNum, vName, vRating, vScore, vProspect, vUrl, vPhone) {
    
        // create a new tr element
        var tr = $("<tr/>");
        // add cells to it
    
        tr.append($("<td/>").html(vNum));
        tr.append($("<td/>").html(vName));
        tr.append($("<td/>").html(vRating));
        tr.append($("<td/>").html(vScore));
        tr.append($("<td/>").html(vProspect));
        tr.append($("<td/>").html('<a href="' + vUrl + '">' + vUrl + '</a>'));
        tr.append($("<td/>").html(vPhone));
        // append tr to table
        $("#printTable tbody").append(tr);
    }
    </script>
    
    
    </html>
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,056
    Rep Power
    9398
    What's going wrong is you're doing = assignment in the condition, not == comparison.
    Code:
    if (TestThis==1000){
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    9
    Rep Power
    0
    Oh, haha, what a newb. I only started learning JS a week ago.

    You can use 1 equal sign to compare in some situations, when do you have to use 2 equal signs?
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,056
    Rep Power
    9398
    Originally Posted by holdorfold1
    You can use 1 equal sign to compare in some situations
    Not in Javascript. One is always assignment, two is always comparison.

IMN logo majestic logo threadwatch logo seochat tools logo