Jump to content

davej

Moderator
  • Posts

    3,988
  • Joined

  • Last visited

  • Days Won

    2

Posts posted by davej

  1. checkstr('');

    checkstr(' ');

    checkstr('procedure ');

    checkstr(' procedure ');

    checkstr(' xxxxxxxxxx procedure / /');

    checkstr('xxxxxxxxxx procedure / /');

    checkstr('Echo procedure 12/20/2016 12:05:32 PM');

     

    function checkstr(str){

    'use strict';

    if (str){ // eliminates null, undefined, and empty string

    var offset = str.indexOf('procedure');

    var firstblank = str.indexOf(' ');

    if(offset != -1 && firstblank + 1 == offset && str[offset + 12] == '/' && str[offset + 15] == '/'){

    alert('['+str+']\nmatch');

    }else{

    alert('['+str+']\nfail');

    }

    }else{

    alert('invalid');

    }

    }

  2. What is the effect of producing a false positive?

    var str = 'Echo procedure 12/20/2016 12:05:32 PM';
    var offset = str.indexOf('procedure');
    var firstblank = str.indexOf(' ');
    if(offset != -1 && firstblank + 1 == offset && str[offset + 12] == '/' && str[offset + 15] == '/'){
      alert('match');
    }else{
      alert('no match');
    }
    
    • Like 1
  3. I meant one example of when would I use $a = NULL; instead of $a = '';

     

    I don't think your question entirely makes sense. Variables are initialized if the code needs them to be initialized, to a value that makes sense for that code, but not all variables need to be initialized. It all depends on how the code is written and how the variable is used.

  4. My code does not really satisfy the problem requirements stated in the original post, but it does demonstrate the use of a function.

     

    If you want to have a function that returns a boolean you might do something like this...

    function vowel(ch){
      'use strict';
      if (typeof ch != 'string' || ch.length != 1){
        console.log('Internal error in vowel');
      }
      var chu = ch.toUpperCase();
      if(chu == "A" || chu == "E" || chu == "I" || chu == "O" || chu == "U") {
        return true;
      }else{
        return false;
      }
    }
    
  5. <!DOCTYPE html>

    <html>

    <head>

    <meta charset="utf-8">

    <title>Function</title>

    <script>

    window.onerror = function(a,b,c){

    alert('Javascript Error: '+a+'\nURL: '+b+'\nLine Number: '+c);

    return true;

    }

    </script>

    </head>

     

    <body>

     

    <h1>Function</h1>

    <input type="text" id="intxt"/>

    <input type="button" id="btn" value="Run"/>

    <div id="outtxt">

    </div>

     

    <script>

    'use strict';

    document.getElementById('btn').onclick = getinputtxt;

     

    function getinputtxt(){

    var outtxt = document.getElementById('outtxt');

    outtxt.innerHTML = '';

    var t = document.getElementById('intxt').value;

    var o = vowels(t);

    outtxt.innerHTML = o;

    }

     

    function vowels(raw){

    var vowels = 0;

    var nonvowels = 0;

    var sent = raw.trim().toUpperCase();

     

    for (var i=0 ; i<sent.length ; i++) {

    if(sent[i] == ' '){

    // do nothing

    }else if(sent[i] == "A" || sent[i] == "E" || sent[i] == "I" || sent[i] == "O" || sent[i] == "U") {

    vowels++;

    }else{

    nonvowels++;

    }

    }

    var str = "Number vowels = " + vowels + "<br/>Number non-vowels = " + nonvowels;

    return str;

    }

    </script>

    </body>

    </html>

  6. An index is generally an integer column in a table where each integer value is unique and can be used to identify that row. Since a primary key must uniquely identify each row a surrogate index is often created and used as a primary key. A primary key can be anything that uniquely identifies each row but an index is a convenient solution. For the gory details of surrogate keys, primary keys, foreign keys, etc... you should read more about relational database theory.

     

    https://en.wikipedia.org/wiki/Unique_key

     

    Perhaps I am not being completely accurate in my above semantics. You can also apply an "Index" to important columns in a table to speed up searches related to those columns. This is a different sort of "index" than what I was thinking of above. It will accelerate searches but will slow writes to the table. See...

     

    http://www.w3schools.com/sql/sql_create_index.asp

  7. I'm not familiar with Node JS. What I am trying to get at is that if there is a block of code written in PHP7, and the same block is written in JS. If both are made to run on the same server, which technology executes such code and outputs result faster?

     

    Do you understand the concept of a client-server architecture? NodeJS is a special implementation of Javascript that runs on the server -- but NORMALLY Javascript only runs on the client.

  8. Server-side languages generally require a page reload to do something. Javascript is client-side code and runs in the client browser and can do things quickly without reloading the page or bothering the server. Javascript code can also minimize the server workload by only requesting the portion of the page that needs to be updated rather than reloading an entire new page.

  9. <!DOCTYPE html>

    <html>

    <head>

    <meta charset="utf-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Test</title>

    <style>

    </style>

    <script>

    window.onerror = function(a,b,c){

    alert('Javascript Error: '+a+'\nURL: '+b+'\nLine Number: '+c);

    return true;

    }

    </script>

    </head>

    <body>

    <h1>Test</h1>

    <script>

    counter();

    counter({mode:"stopped"});

    counter({

    min:0,

    max:200,

    mode:"increment"

    });

     

    function counter(o){

    if( o instanceof Object ){

    if(o.min != undefined && o.min != null){

    alert('min= ' + o.min);

    }

    if(o.max != undefined && o.max != null){

    alert('max= ' + o.max);

    }

    if(o.mode != undefined && o.mode != null){

    alert('mode= ' + o.mode);

    }

    }else{

    alert('Error: counter parameter ['+ o +'] is not an object');

    }

    }

    </script>

    </body>

    </html>

×
×
  • Create New...