Categories
JAVASCRIPT PROGRAMMING TUTORIALS WEB DEVELOPMENT

How To Enable Or Disable Console.log For Debugging

So you are working on your Javascript project and you want to know how to enable or disable console.log messages? Then read on to learn how you can do it quite easily.

Whenever you are working with Javascript, be it on the front-end side with vanilla Javascript or frameworks like React, Vuejs etc. Or on the backend with Node JS, Express JS etc for your servers, you want a way to debug the code. Right?

One of the easiest way we try to debug our code is by using simple print statements. So on the Javascript end, this usually boils down to using console.log( ) messages.

But here is the thing. When you use a programming language that will be compiled – such as C, C++ or Java, we can use compiler flags to enable or disable debug messages.

However, for an interpreted language like Javascript, we dont have a built in method for how to Enable or Disable console.log( ) messages.

So what do we do in that case? Well, if we don’t have a built-in method to enable or disable Javascript console.log( ), then we need to create one!

And that is what we are going to do here. We will be writing a simple Javascript module that we can use to enable or disable Javascript’s console.log( ) messages.

So how do we design this piece of code?

Here is what we are going to do. Instead of using console.log( ) to print our debug messages, we will use our own custom function called debugLog( ). But within this debugLog( ) function, we will have a simple Debug Enable (or) Disable flag.

Now when this flag is set, we will call the console.log( ) in turn and if not set, we will skip printing the log messages. It is as simple as that!

Sounds good? Great! So here is the piece of code that does just that!

How to Enable or Disable console.log
How to Enable or Disable console.log using a switch?

Create a file called “utils.js” and add the following piece of code into it:

DEBUG = true;

function debugLog(msg) {
    if (DEBUG) {
        console.log(msg);
    }
}

//For express.js, we need to export this module
module.exports = debugLog;

So that is it! From now on, call debugLog( ) instead of console.log( ) to write your debug message. An example piece of such a debug message is shown below:

debugLog("[app] Connect to mongoDB...");

So whenever you want to switch on the debug logging, set the flag as below:

DEBUG = true;

But the moment you want to turn these debug messages OFF, just set it to false:

DEBUG = false;

And that is all there is to it! This is how you can enable or disable console.log( ) messages for debugging!

Categories
HTML PROGRAMMING TUTORIALS WEB BROWSER WEB DEVELOPMENT

HTML Meter Tag Explained

HTML Meter Tag is a new type of HTML tag introduced in the HTML 5 specification. So chances are, you are not so familiar with it. Right? But do not worry, as we will start exploring everything there is to the HTML meter tag in this article. So you will soon be able to start using it in your websites and web apps.

Sounds good? Great! Then let us get started!

What Is An HTML Meter Tag?

So consider this situation. You are creating a dashboard in your web app that, among many other things, also shows the storage space left in your hard disk. Now how will you go about designing it?

So the chances are, you might create a custom HTML element that is styled using its CSS properties to display the storage space graphically. But even if it seems a valid solution, problem is you need to keep creating such custom HTML elements frequently. And if you are designing a web app having a dashboard showing multiple data, these custom elements will only increase. Right?

HTML5 Meter tag explained with example
HTML5 Meter tag explained

So then what is an easier solution for this problem than that? Well for one, would it not be easier if we had a built in HTML element to display such measurements? With the growing number of web apps with dashboards, having such a prebuilt HTML element makes sense, right?

Well, that is precisely what the HTML5 specification guys decided to do! And that is how the HTML Meter tag came into picture!

So What Does The Meter Tag Do?

The idea was to create an HTML element that can be used to display any measured values within it. So that is what a Meter tag does!

A Meter tag can be used to display a measured value within a given range of values.

Sounds good? Great! Then let us move on to take a look at an example piece of code that uses the Meter tag. Shall we?

HTML Meter Tag Example

So for this example, let us assume that we want to display the size of a cloud storage space. For brevity, let us say we want to display the size of our mail box which is currently at 66% full. Alright?

So how do we go about using the Meter tag to display this value? And how will the displayed result look like?

To answer these questions, let us start writing our code as shown below:

<!DOCTYLE html>

<html>
  <head>
  </head>
  <body>
    <label for="disk">Disk Usage: </label>
    <meter id="disk" value="0.7">70%</meter>
  </body>
</html>

Now, if you copy the above piece of coded into a new file and save it as meter.html, what do you see?

Go ahead and try it out! I want you to do it yourself so that you become familiar with how to use the HTML meter tags.

Did you try it? If you did, you should see that the result looks something like this:

70%

Conclusion

So that is it! That is how the Meter tag works! You just feed the right value into the “value” property of the Meter tag and it will display the meter gauge accordingly. Go ahead and test it out with few more different values and see how the gauge changes yourself.

Finally, if you have any questions or suggestions, let me know in the comment below! So until next time, take care!

Categories
TUTORIALS

Error: Uncaught PermissionDenied: network access to “127.0.0.1:8000”, run again with the –allow-net flag

Did you try to run a Deno web server code but came across an error that read: “error: Uncaught PermissionDenied: network access to “127.0.0.1:8000”, run again with the –allow-net flag” ?

If yes, then do not worry. Because you are not alone? Even I got excited to run a Deno example web server code but encountered the same error message.

So what is the solution here? How do we go about fixing this?

Well luckily, a solution to this problem is quite easy. Because here is what is happening!

Error: Uncaught PermissionDenied: network access to "127.0.0.1:8000", run again with the --allow-net flag

Error: Uncaught PermissionDenied: network access to “127.0.0.1:8000” – How To Fix It?

So here is the thing. Deno has security features built into it. So when you try to run a piece of Deno code that uses network, it needs to be run with network permission. Otherwise, you get the above error message!

That is all that is happening here. So we tried to run an example Deno web server application that looked like this:

Deno Server Demo Code

So as you can see from the above Deno code, we are making use of a server module called serve. But this sever code is not on our local computer. So we have imported it from a github repository:

"https://deno.land/std@0.50.0/http/server.ts"

But after downloading it, when we tried to run the code using the command:

deno run server.ts

this is what is happening:

It triggered the serve module to run at port 8000.

However, there is a problem here. So as you see, we have just run the server.ts file as is. That is, we did not issue any explicit permission attached to it. So what happens in that case is that the Deno code tried to run without any extra permissions.

But by default, Deno code does not have permission to access network stuff. So, it just threw the above error!

Solution

So what is the solution then? Well, we just need to give network access to this program and it should start working! Right? So what is the command to do that? Here it is:

deno run --allow-net server.ts

So we just attached extra permission before running this Deno code. This will allow our program to use the port 8000 for accessing the server from a browser.

So when we run the above command, we get the output:

http://localhost:8000/

And when we access the above url, we can see that the server has rendered the text ‘Hello World’ to the browser.

So that is it! That is all we had to do to fix this Deno network program. Hope it was clear and easy for you!

If you still have any questions, do let me know in the comment section below!

Happy Coding!

Want To Learn Typescript? Take a look at this article!

Categories
TUTORIALS

pytesseract.pytesseract.tesseractnotfounderror: tesseract is not installed or it’s not in your path

In this article, we see how to fix the error : “pytesseract.pytesseract.tesseractnotfounderror: tesseract is not installed or it’s not in your path“.

So are you trying to use the Tesseract tool to do OCR using Python? But some how end up with this error? If yes, then this article will help you in fixing this error.

But before I explain you on how to fix it, here is what we will do. Let me explain you about what is happening here in the first place. Alright? Because if you know what is happening, you will be able to fix similar errors in future as well.

It is not just sufficient to know how to fix a problem. But to know why it happened in the first place. Right? So we will do just that.

What Does The Error Mean?

So let us take a look at the error once again, shall we? Here is what the error looks like:

pytesseract.pytesseract.tesseractnotfounderror How To Fix It?
pytesseract.pytesseract.tesseractnotfounderror How To Fix It?
pytesseract.pytesseract.tesseractnotfounderror: tesseract is not installed or it's not in your path

Does that error message makes any sense to you? No? Actually it does. But only if you know more about how Python’s PyTesseract library.

You see, PyTesseract library is a Python wrapper that makes use of the Tessseract application installed on your computer. So for the PyTesseract library to work, it needs to hook in to the Tesseract app first. But to do that, it needs to know where to find it. Right?

So how does the PyTesseract library know where to find it? Well that is where the OS PATH variables come into picture.

The Python library looks for an OS PATH variable called tesseract. So it is in this variable does it expect to find the path to the Tesseract app.

But when the library cannot find this PATH variable, or the Tesseract app, it will throw the error:

pytesseract.pytesseract.tesseractnotfounderror: tesseract is not installed or it's not in your path

So now that you know the root cause, what is the solution?

How To Fix pytesseract.pytesseract.tesseractnotfounderror?

To fix this problem, you have to follow these few simple steps:

  1. Make sure that Tesseract application is installed.
  2. Make sure that the OS PATH variable tesseract is set to point to the above application file.
    • So if you are on Windows, the tesseract PATH variable will have a value something like this: C:\Program Files (x86)\Tesseract-OCR\tesseract.exe. To find this out, type the below command:
      • echo $tesseract
    • But if you are on Mac or Linux (eg: Ubuntu), it will look like this: /usr/local/bin/tesseract. To find this out, type the below command:
      • echo $tesseract

So that is it! That is how you fix the tesseractnotfounderror error on your Python program.

Hope this helps. Do let me know if you have any questions and I will be happy to answer. See you in the next article. Cheers!

Do You Know The Difference Between Expression Vs Statement In Python?

Categories
TUTORIALS

A ________ value in a loop can be either a positive or a negative number.

So this is a programming quiz question where we need to find what value in a loop can be either a positive or a negative number.

The quiz question goes like this:

A __ value in a loop can be either a positive or a negative number?

To answer this question, we need to think a little bit about how a programming loop works.

A ________ value in a loop can be either a positive or a negative number.

Loop Example

So to do that, let us take an example of a for loop. Now, the syntax of a for loop looks something like this:

for (i=0; i < 10; i++) {
  //do something
}

So if we look at the above for loop, we can see that it has one variable i. But it is being used in 3 different places, right?

But why?

So for a for loop to execute, we need to have 3 things to it. First, we need an initial value from which the loop begins. In this case, it is done by the code: i = 0;

Next, we need to set a condition. So as long as this condition is true, we will loop. So in this case, we have it set as i < 10. Which means that this loop will run as long as the value of i is less than 10.

But the final part of the for loop is where we change the value of the variable i. So we have it set to i++. But what does it mean?

It means that we want the loop the increase the value of i after every loop is run.

So in all, we this is what we are asking the code to do. We want you to run for as long as the value of i is less than 10. And we start the loop with an initial value of i=0 and let it run until it becomes equal to 10. But as soon as i reaches 10, we want the loop to end!

So if we look at the for loop, it is something like this:

We want it to run as long as a condition is true. And that condition here is set as i to be less than 10. So as long as this condition is met, this code will loop.

Answer

So this begs us the question then. What are these type of values called? They are called “step” values.

So to answer our question:

A step value in a loop can be either a positive or a negative number?

Got it?!

Want to learn about Typescript? Check this out!

Categories
TUTORIALS

Add A Variable To URL Of Ajax – How To Guide

So you as a web developer want to have your user be able to add a variable to the URL of his post. But you do not know how to do that? Well do not worry. Because in this tutorial, we will teach you how to do just that!

Now this article is going to get a bit more technical. So I want you guys to be with me all the way through the article, alright? Great! Then it is about time for us to start. Let us go!

Add a variable to URL of Ajax – The Problem

So what is the problem we are looking at over here? Well, it is actually quite simple. We want to be able to add a variable value to the url parameter of the Ajax call.

Add a variable to URL of Ajax
Add a variable to URL of Ajax

So we are talking about a piece of code that looks something like this:

let var myQuery = "What Is My IP?";
$.ajax({url: "https://google.com/search?q=" + myQuery, method: 'POST', contentType: false,      cache: false, processData: false,
success: function(result){
      $("#div1").html(result);
    }});

So, as you can see from the above code, we are trying to pass in the search query “What Is My IP?” by trying to Add a variable to URL.

And in this case, we have our query stored in a Javascript variable myQuery. So by adding this variable to our Ajax url parameter, we should be able to issue a POST request to Google.

But this is all well and good, but what is the use of this? Why are we even using Ajax call for doing this in the first place? Since we could have just used simple Javascript for a URL query, right?

So to answer this question, we need to know what an Ajax call does.

So we can use simple Javascript code to make an url call. But the problem with that is, the call will be blocking. So our Javascript code will not continue to run until we get back a response from the server.

But on the other hand, using Ajax ensures us that we can make asynchronous requests for the web query. So thereby, our remaining Javascript code will continue to run while we are waiting for the ajax url code’s response.

And by using a Javascript variable like “myQuery” above, we can allow users to add a variable to url to our Jquery ajax request.

Conclusion

So there you have it. It is quite simple to add in a variable to Ajax calls. And I hope that this article helped you learning how to do it.

But if have any more queries about it, do let me know in the comments below. Because I will be more than happy to help you out!

Want to learn about Typescript? Take a look at this article

So with that, I will end this article. See you guys until next time. Cheers!

Categories
PROGRAMMING TUTORIALS

Typescript Type Conversion

In this article, we will learn how to do type conversion in Typescript. So are you working on Typescript and now want to convert a value from string to number? Or want to convert a number to string in Typescript? Well, then read through this article to learn how to do that!

So here is the thing. You have a Typescript value stored in a variable and you want to use that. But the thing is that, you can not use it as is. Why? Because this number is stored as a string type. But you want to use it as a number. So how can we do that?

Typescript Type Conversion
Typescript Programming Language Logo

That is when the Typescript type conversion comes to your rescue! Using the Typescript type conversion, you can turn that variable’s value from a string to a number!

Sounds great! Right? But then how do we do that in our code? To learn that, take a look at the example code given below:

Example Of Type Conversion from a String to Number

let myStringOfNum: string = "10";
let myNumber: number = Number(myStringOfNum);
console.log(typeof myStringOfNum);
console.log(typeof myNumber);

So if you look at the above code, we have a variable called myStringOfNum that holds a string that reads “10”. But we want that string to be used as a number. So what do we do for that? We use Type Conversions!

So in Line 2, we declared another variable called myNumber. But this time, this variable is of type number. What we do next is that we will take the value of the vairable myStringOfNum and convert its type to Number. We do that by using the typecast Number(). The resulting value is then assigned to our variable myNumber.

So in that case, our console output will look like this:

string
number

Example Of Type Conversion from a Number to String

In a similar way, we can convert a number to string in Typescript. So when we take a look at this code:

let myNumber: number = 10
let myString: string = String(number)
console.log(typeof myNumber);
console.log(typeof myString);

What do you think how to output will look like? I think your guess will be:

number
string

Summary

So in summary, we can convert a number to string or a string to number using Typescript’s Type conversion feature. I guess it could not have been any more simpler than this. What do you guys think? It is, Right?

Learn this difference between an expression and a statement here

Categories
HEALTH LIFESTYLE TOOLS TUTORIALS

BMI Calculator For Men

In this article, we will take a look at the BMI calculator for men. So, using this calculator, you will be able to check your Body Mass Index. But it wont stop just at that. Because this BMI tool will also tell you if you are over weight, under weight or at the normal weight.

BMI Calculator for Men

BMI Calculator For Men
BMI Calculator For Men Online

Calculate your BMI by entering your body weight and body height in the fields given below:

Enter Your Weight (in Kg):
Enter Your Height(in cm):

Your BMI Value Is:

Check what your resulting BMI value means over here.


So how is knowing your Body Mass Index value going to help you? Well, it is quite simple. You see, the BMI tool gives out BMI value for your body. So using this BMI value, you can check if you are at the right body weight for your age and height.

Now if you are in the normal BMI range, then all is well and good. But in case you are not, then it will tell you if you are under the normal body weight or over the normal weight.

So knowing if you are under weight or over weight will help you in figuring out what you should eat.

Wait what? How is that?

Yes, you read that right. Because if your BMI is not in the normal weight range, then you need to change your eating habits. Or in other words, you need to change your diet.

But why? Well, it is because if you are under weight, you will need to eat more food that is rich in calories. But on the other hand if you are under weight, you need to eat food with fewer calories.

That makes sense right? So that is why we will use the BMI Calculator for Men to find out what you should be doing about what you eat.

But before we go ahead using the BMI calculator for men, we should first know what is it, right? So we will start from there!

What Is Body Mass Index (BMI)?

Body Mass Index or BMI is a value that we get by dividing our body weight by the square of our body height. So in other words, we can calculate BMI using the formula:

BMI = (Body Weight in kg) / (Body Height in meters)²

So what doe this equation of BMI index mean? What is it doing logically?

What does a BMI calculate?

So as you can see above, we are using two values of our body to calculate our BMI. We take the body weight in Kilograms and divide it by the square of our body height given in meters.

So what does that mean? What do you think we are calculating here?

Logical Explanation Of BMI Calculator For Men

Well, in simple terms, we are just trying to see how much of a body mass we have per square meter, isn’t it. So in a way, we are trying to calculate the mass occupied by our body in a given square meter area!

It is as simple as that!

How Is BMI Calculator For Men Useful?

So now that we know how to calculate the BMI for men, we need to ask the next question. How is BMI going to be useful for us?

So here is thing. If you are a healthy man, you will be physically fit. Which in turn means that your physical appearance will also be good, right. So what tells us that? Well, for the most cases it is going to be your height and weight.

Makes sense, right?

Well think about it. If you are physically active, you will be burning most of the calories that you eat in any given day. Right? Which means that your body will have less body fat stored and well built muscles as you do more activities. Right?

So in that case, there will be a specific body weight for your body height which will be considered ideal. Correct? So that is exactly what BMI for men is giving us!

Bod Mass Index Range

BMI Range

Now that we what a BMI is,it is time to learn about the BMI range. So we calculated the BMI value. That is all well and good. But what does the value even mean? How will we know if we are over weight, healthy weigh tor under weight?

That is exactly when the BMI range comes into picture. The easiest way for us to understand it is to take a look at it below:

BMI Range Table

BMI RangeWeight Status Category
Below 18.5Underweight
18.5 to 24.9Healthy Weight
25 to 29.9Overweight
30 or aboveObese

So as you can see from the above BMI Range table, we are underweight if our BMI turns out to be below 18.5

But if our BMI calculator outputs a value between 18.5 to 24.9, then we are at our healthy weight!

On the other hand, if our BMI is between 25 to 29.9, we are considered overweight.

But if our BMI is more than 30, we are considered as obese.

Pretty handy way to find it out then. Yeah? 😉

So what ever value we get, at the end of the day, we will get to know if we are healthy or not. Now that is all that really matters, eh?

Conclusion

So there you have it. This is why you need to use the BMI calculator for men app given above to check your BMI. But if you still have any thing to ask, do let us know in the comment section below. We will be more than happy to answer you!

So with that said, we will end this article over here. Hope you were able to check your BMI value and learnt to take the right action from now on.

If you want to stay healthy, make sure you keep your body weight under control. If you need some more ideas about it, do read this blog for more. Hope you have a great healthy life ahead! Cheers guys!

Want to keep your Blood Pressure (BP) level in check? Then check this Best Pulse Oximeter buying guide.

Categories
ELECTRICAL SCIENCE ELECTRONICS TUTORIALS

A 600-W TV Receiver Is Turned On For 4 h With Nobody Watching It

A 600-W TV receiver is turned on for 4 h with
nobody watching it. If electricity costs 10 cents/kWh,
how much money is wasted?

So this is a problem that we have taken from the book Fundamentals Of Electrical Circuits By Alexander. But if you want to take a look at the problem yourself, then go to Chapter 1 question 1.39 of the book.

Solve Fundamentals Of Electrical Circuits Problem 1.39 - A 600-W TV receiver
Solve Fundamentals Of Electrical Circuits Problem 1.39 – “A 600-W TV receiver”

Since this is a problem we have taken from the Chapter 1 – Basic Concepts, it should be easy. But is that right? Well, let us see as we try to solve it, shall we?

So how do we go about solving this problem?

Well as we always do, we will first break the problem into small parts. By doing that, we will be in a better position to understand the problem in the first place. Right?

So, here is our breakdown of the problem.

The Problem Breakdown

So let us read out the problem once again, line by line. Alright?

Now the first line of the question reads – “A 600-W TV receiver is turned on for 4 h with nobody watching it.

So what do we learn from that? It means that we have a TV which consumes electricity at 600 Watts Per Hour or 600 Watt/Hr. But we can also write this as 0.6 Kilo Watts Per Hour or 0.6KW/Hr, right? So that means:

P = 0.6 KW/Hour

Ok! So with that being clear, we will move on to our next part of the question which reads – “If electricity costs 10 cents/kWh, how much money is wasted?”

From this sentence, we now know that the cost of 1 KW of Power consumption for every hour is 10 cents.

Electricity Price = 10c/KW

So the question after that then becomes quite simple, isn’t it? It is just asking us to find out what will be the electricity bill for running that A 600-W TV receiver for 4 hours.

A 600-W TV receiver Electricity Cost In 4 Hours?

So solving this now becomes a simple equation:

Total Bill = Power Consumed By TV in 4 hours * Elec. Price

So then what is the total Power consumed by the TV in 4 hours? It is simple right? If the TV consumes 0.6KW in 1 hour, then the amount of Power it consumes in 4 hours is:

Power consumed in 4 hours = 0.6KW * 4 = 2.4KW

So it is now clear right? The TV has consumed 2.4KW in 4 hours. So then what will be the electricity price for this much of power?

Total Bill = 2.4KW * 10 cents => $0.24

Categories
COMPUTER SCIENCE TUTORIALS

Which of the following is not a social networking website?

Which one of the following is not a social networking website? (a) Twitter (b)Facebook (c)My Space (d)Hotmail​

So in this article we will try to find out which one of those websites are not a social networking site.

Which of these are not social media sites?
Which of these are not social media sites?

But before we do that, we first need to understand what a social media website is. Right? So let us start from that point.

What Is A Social Media Website?

So there are many ways to answer this question. One is to explain it using take a text book definition of it which says:

Any website that allows people to create & share content to allow them for social networking.

But the problem is, it is not so clear in what it really means. Correct? So we can’t just go by it. Then what can we do about it? Well, let me explain you in a much simpler way. Alright? So here is my way of explaining it.

Any website using which we can create content & post it for the world to see while having a way to talk to others in real time is a social networking website.

What is not a social networking website?

It is not a social networking website if you cannot make your own post public and talk to them at the same time.

Does that make sense?

Answer With Explanation

Well then with that in our mind, let us look at each of the options given in the question. Alright? Here we go!

  1. Twitter – Is this a social networking website? So answer the questions asked above to find out. (i) Can you post on Twitter? Answer: Yes. (ii) Can you talk to others in real time? Answer: Yes. So as you can see, the answers for both the questions is Yes. So that means it is indeed a social networking website!
  2. Facebook – Is this a social networking website? So answer the questions asked above to find out. (i) Can you post publicly on Facebook? Answer: Yes. (ii) Can you talk to others in real time? Answer: Yes. So then this too should a social network!
  3. Myspace – Again, ask the same questions. (i) Can you post publicly on Myspace? Answer: Yes. (ii) Can you talk to others in real time? Answer: Yes. So that means it is indeed a social networking website!
  4. Hotmail – (i) Can you post publicly on Myspace? Answer: No. (ii) Can you talk to others in real time? Answer: Yes. So as you can see here, we cannot post publicly on Hotmail. Which means it is not one!

So from this analysis, we can see that the answer we should be picking up amount the given choices is (d)Hotmail.

What we can learn from here is that if we try to break down the problem into smaller parts, it is easier to answer. Correct? So as you can see, we went through each of the options in the question and tried to answer them. It is only then that we were able to answer it. Right?