Today is the 7th day of my #100DaysOfCode journey with JavaScript.

I write about my learnings in an explained way through my blogs and socials. If you want to join me on the learning journey, make sure to follow my blogs and social and share yours too. Let’s learn together!🫱🏼‍🫲🏼

This Article is a part of the JavaScript Fundamentals series.

While Loop

As long as the test condition evaluates to true, the while statement generates a loop that performs the provided statement. Before the statement is carried out, the condition is assessed.

while(b > 7) {
    // do something
}

We are just stating that if a condition is true, this statement shall be carried out till it is not.👇🏼

Example: Complete the top double function to find the largest double for the value that is below the top.

function topDouble(value, top) {
   
    while (value < top) {
        value = value*2;
       
    }
    return value/2;
}

Break Statement

We will exit the loop once break is hit. Even when the condition is true, there is still a possibility to exit the loop thanks to the break statement.

while(true) {
    if(a > 5) {
        // exit the loop
        break;
    }
}

Questions for Practise

  • Given an integer value num, determine if it is even. If it is even, return true. Return false otherwise.

    function isEven(num) {
        if (num % 2 === 0){
        return true;
        } 
    }
    
    // or
    
    function isEven(num) {
        return num % 2 === 0;      
    }
    
  • The function smallerNumber will be given two unequal numbers: num1 and num2. Your goal is to find the smaller number and return it!

    function smallerNumber(num1, num2) {
    if (num1 < num2){
        return num1;
    }
    else {
        return num2;
    }
    }
    
  • A string is stored in the variable fakeName. Take this fake name and use it to replace every occurrence of "John" in the message. Do not change the message in other way.

    const fakeName = require('./fakeName');

    const message = ` Hello, John! You left a package at the office today. You can pick up tomorrow at 10am, John. If not I will drop it off this weekend. Goodbye John! `;

    const fakeName = require('./fakeName');
    
    const message = `
        Hello, ${fakeName}! You left a package at the office today.
        You can pick up tomorrow at 10am, ${fakeName}. 
        If not I will drop it off this weekend.
        Goodbye ${fakeName}!
    `;
    
  • The function checkNumber takes a single argument: a number num. The function should return the string positive if the number is positive, negative if the number is negative, and zero if the number is zero.

    function checkNumber(num) {
        if (num > 0){
            return 'positive';
        }
        else if(num < 0){
            return 'negative';
        }
        else {
            return 'zero';
        }
    }
    
  • The function maxSum takes a number argument num. Your goal is find the sum all of numbers, starting from 1, up to and including num.

    function maxSum(num) {
        let sum = 0;
        for(let i=1; i<=num; i++){
            sum = sum + i;
        }
        return sum;
    }
    

Conclusion

Ending with an extra bit of information about JavaScript functions…

We can exit loop by using both return and break statement.

Today I learned about While Loop and Break Statement and also practiced a few Questions in JavaScript.

If You ❤️ My Content! Connect Me on Twitter

Buy me a coffee