36. For loop exercise

Hits: 0

practice one

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <script type="text/javascript">
            /* 
                Calculate the sum of all odd numbers except 1-100
             */
            var sum = 0;
            for(var i=1; i<=100; i++){
                if(i%2 != 0){
                    sum += i;
                    console.log(i);
                }
            }
            console .log( "All odd sums are: " +sum);
         </ script > 
    </ head > 
    < body > 
    </ body > 
</ html >

Exercise 2

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <script type="text/javascript">
            /* 
                Print the number and sum of all multiples of 7 between 1 and 100
             */ 
            var sum = 0 ;
            var total = 0 ;
            for ( var i= 1 ; i<= 100 ; i++){
                 if (i% 7 == 0 ){
                    total++;
                    console.log(i);
                    sum += i;
                }
            }
            console .log( "The total number of multiples of 7 between 1-100 is: " +total+ "pieces" + " " + "The sum of them is: " +sum);
         </ script > 
    </ head > 
    < body > 
    </ body > 
</ html >

Exercise three

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <script type="text/javascript">
            /* 
                number of daffodils
                A daffodil number is a 3-digit number whose sum to the power of 3 of the digits in each digit is equal to itself.
                (Example: 1^3 + 5^3 + 3^3 = 153), please print all the daffodils.
             */

            //var num = 123; 
            //ones place 
            //var ones = num%10; 
            //tens place 
            //var twos = parseInt(num/10)%10;//pasrseInt() is used to round up 
            //hundreds bit 
            // var threes = parseInt(num/100)%10;

            var ones; // ones 
            var twos; //tens 
            var threes; //hundreds 
            var sum; //the cubic sum of their several 
            for ( var i= 100 ; i<= 999 ; i++){
                ones = i%10;
                twos = parseInt (i/ 10 )% 10 ;
                threes = parseInt(i/100)%10;
                sum = Math.pow(ones,3)+Math.pow(twos,3)+Math.pow(threes,3)
                if(sum == i){
                    console.log(i);
                }
            }
        </script>
    </head>
    <body>
    </body>
</html>

You may also like...

Leave a Reply

Your email address will not be published.