top of page

Luvcodings QUESTION AND ANSWERS -

 

JAVASCRIPT

QUESTION: 1

Given 5 numbers, write a code to return each of the number  multiplied by the total of number

Solution: 1

<!DOCTYPE html>
<Html>
    <head>
        <Title>"JAVASCRPTS LESSONS "</Title>
    </head>
    
    <Body>
        <h2>******JavaScript Solutions**********</h2>


         <p>Question: Given 5 numbers, write a code to return each of the number  multiplied by the total of number:</p><br>

        

        <p>"Answers"</p>

        <p id="demo"></p>
        <p id="demo1"></p>
        
        <script>
            //using Array/List
            //The numbers enclosed in an array
            var numbers=[5, 6, 8, 9, 12];
            
            // Length of the array, that is the total numbers available
            var n=numbers.length;
            
            // Now lets iterate over all the numbers to multiply them by the total.
            
            //using map function
            var new_Numbers= numbers.map(function(i){
                return i*n;
            });
            console.log(new_Numbers);


            document.getElementById("demo").innerHTML="Using Map function: Answer = " + "" +new_Numbers
                   
            //using forEach function
            var new_Numbers1=[];
            numbers.forEach(function(i){
                let result= i*n;
                new_Numbers1.push(result);
            });
            console.log(new_Numbers1);

            document.getElementById("demo1").innerHTML="Using forEach function: Answer = " + "" + new_Numbers1
        
        </script>
            
    </Body>
</Html>

Sample Output

Capture3.JPG
bottom of page