Posts

Showing posts from November, 2023

for and if condition using JS

Image
  Unveiling the Mystery of Numbers: An Adventure in JavaScript Introduction Greetings, fellow coders! Today, we embark on a journey into the heart of JavaScript, where we'll unravel the mysteries of numbers. Our guide for this exploration is a small but powerful script that uses a for loop and if conditions to classify numbers as either even or odd. Let's dive right in! The Code Breaking Down the Script In this code snippet, we have an array called numbers containing integers from 1 to 10. The script then employs a for loop to iterate through each number in the array. The for Loop} The loop initializes a variable i to 0, and as long as i is less than the length of the numbers array, it iterates, incrementing i with each cycle. The if Condition Inside the loop, there's an if condition that checks whether the current number ( numbers[i] ) is even or odd. If it's even, a message is logged indicating that the number is even; otherwise, it's identified as odd....

Switch case using JS

Image
  Exploring the Days: A JavaScript Journey Introduction Welcome to another blog post where we delve into the fascinating world of JavaScript! Today, we are going to explore a small piece of code that involves a switch statement to determine feelings towards certain days of the week. Let's dive in! The Code ; } The Breakdown In this code snippet, we have two variables, favoriteday and worstday , set to 'sunday' and 'monday', respectively. The focus is on favoriteday , and a switch statement is used to evaluate its value. The Switch Statement The switch statement checks the value of favoriteday against different cases. If favoriteday matches a case, a corresponding message is logged to the console. Case 'tuesday': If favoriteday is 'tuesday', the code logs 'Tuesday is depression day'. Case 'friday': If favoriteday is 'friday', the code logs 'Friday is the sweetest day'. Default: If the value of favoriteday doe...

JS using if else method

Image
  JavaScript if else statement makes it very easy to execute your code if specific conditions is truthy. Its syntax is just as easy. It is composed of three parts. The first part is  if  keyword. You use this keyword to tell JavaScript that you are about to create an if else statement. The second part is a condition you want to test for. Condition is wrapped with parentheses and it follows the  if  keyword. This can condition can range from very simple to very complex expressions. The only thing that matters is if the result of that expression is  boolean , either  true  or  false . The third part is a block of code you want to execute. This block of code is surrounded by curly brackets. It follows right after the condition. Remember that this block of code will be executed only if the condition evaluates to  true , is truthy. If it evaluates to  false , it is falsy, the block of code will not be executed.