Exercise 002: Step by Step

Let’s review this function step by step

First of all we capture the value included in the InputBox by using its id tag

 let x = document.getElementById("passid");
    pass = x.value;
    numberOfnumbers=0;
    numberOfSpecialChar=0;
    mychar=0;

numberOfSpecialChar and numberOfNumbers are two variables that we will use respectively to measure how many special chars are in the string and how many numbers have been included.

Let’s also set another variable which is type array and includes all the list of special characters .

 myarr = ['!','@','#','$','%','&','*'];

At this point we can start a loop : the objective is to loop through each character of the password the user has provided

 for (i=0;i<pass.length;i++) {
        myCharCode=pass.charCodeAt(i);
        mychar=pass.charAt(i);

        if ( (myCharCode>=48) &amp;&amp; (myCharCode<=59) ) {
                numberOfnumbers++;
        }

        for (y=0; y<7;y++) {

            if (mychar==myarr[y]) {
                numberOfSpecialChar++;
            }
        }

    }

In the line 2 of the above block we use the function charCodeAt to get the ASCII code of each characters included in the password.

In the lines 5-7 we check if the ASCII code is included in the range of numbers code of the ASCII table. The the ASCII code is between 48 and 59 we increase the variable numberOfnumbers.

In the bloc lines 11 to 14 we loop through the myarr variable and check if the char code of the password is equal to one of the char included in the array. If that is the case we increase the variable numberOfSpecialChar

At this point we can verify if all three conditions are met or not. We do that in the following block:

if ( (pass.length>=7) &amp;&amp; (numberOfSpecialChar>=2) &amp;&amp; (numberOfnumbers>=2)) {
        let msg="Password OK"
        document.getElementById("ok").innerHTML=msg;
        document.getElementById("ko").innerHTML="";

    }

If the condition are met we output that the password is ok.

If the condition are not met we output why the password is not valid

else {
        msg="Password is rejected because:";
        msg_specialchar="";
        msg_numberOfnumber="";
        msg_len="";

        if (numberOfSpecialChar<2) {
                msg_specialchar=" the number of special chars is less than 2 - ";
        }

        if (numberOfnumbers<2) {
            msg_numberOfnumber="there are less than 2 numbers - ";
        }

        if (pass.length<7) {
            msg_len=" passowrd is not long at least 7 chars - ";
        }

        msg=msg+ msg_specialchar + msg_numberOfnumber + msg_len;

        msg=msg.slice(0,msg.length-2);
        document.getElementById("ko").innerHTML=msg;
        document.getElementById("ok").innerHTML="";

    }

Something interesting in this exercise is the use of the function charCodeAt(). Interestingly in C you can assign a char to a int variable and the variable will store the code of the char.

Something in JAVA where you can cast,int value = (int) c;, but not in JavaScript which has its own function : charCodeAt().

JS by Exercise 001 – Explanation

First we create  a function called my input

function myinput() {
}

Now we need to capture the input of the user with the prompt command

  let myInp = prompt("Please enter your phrase");

Now that that we have an input from the user consisting in several words we can split this string using the split function and a space (” “) as a delivemeter. We do that by defining a variable x which is an array and we fill it by splitting the string by ” ” and measure how many elements are in the array using length

let x= myInp.split(" ");
let lenarr = x.length;

The following section of the JS code is the one that does the job. First we define 2 variables mystring and total. Both of those variable are type String.

Then we start looping through the array variable x and pass each single word to a variable called myword

So if we have given 3 words as input separated by a space (” “) now we will generate a loop where myword will receive each single word as the loop continues.

Here is any example if we pass the input: javascript is fun.

Now variable x will be composed by an Array of 3 elements.

Now that we have any array we can loop through it. Each single element of the array is passed to the myword variable.

For each word we measure the length again and we start a second loop.

In this second loop we concatenate from char #2 to to the last char (myword.length) and then we add the the first letter (which for java is char[0] by the way)

Then we add the string “ou” and a space char.

let mystring="";
let total="";
    for (i=0;i<lenarr;i++)  {
        var myword=x[i];
        for (y=0;y<x[i].length;y++) {
          mystring= myword.slice(1,myword.length) + myword.slice(0,1) + "ou" + " ";
        }

        total = total + mystring  

So if the input is :

The output by moving the first letter and the end of each word and adding “ou” is:

Here is the full code available to everyone 

function myinput() {
    let myInp = prompt("Please enter your phrase");
    document.getElementById("yourinput").innerHTML="Input->:" + myInp;

    let x= myInp.split(" ");
    let lenarr = x.length;

    let mystring="";
    let total="";
    for (i=0;i<lenarr;i++)  {
        var myword=x[i];
        for (y=0;y<x[i].length;y++) {
          mystring= myword.slice(1,myword.length) + myword.slice(0,1) + "ou" + " ";
        }

        total = total + mystring   
        
    }
    total ="Output->:" + total;
    document.getElementById("yourresult").innerHTML=total;