简体   繁体   中英

For/while/do loop logic trouble

It's my first time posting here so go easy on me. I'm new to programming and have been trying to get my head around for/while/do loops for all of today. I have most of the code finished, however, I'm struggling with one section.

The program counts from 1-50 and creates 3 sets of arrays: multiples of two, multiples of 3, and one for non-multiples of 2. What I want to do is create a new array which has multiples of 2 & 3 pushed to it, comparing two of the original arrays.

I've searched about arrays and comparisons/intersections, though I was not able to find a solution that would've been appropriate to implement for my case here. The closest answer to this I could read about was only for PHP.

This is where I'm at right now, after trying to figure it out for so long:

var findCommon = function() {
    var g = 0;
    var w = 0;

    if (twoMultiples[g] !== threeMultiples[w]) {    
       do {
          g++;
       }

       while (g < w);    
       do {      
          w++;      
       }

       while (g > w);  
    } else {    
         bothMultiples.push(twoMultiples[g]);    
    }        
};

I'm also thinking I need a for-loop to begin the function, but I can't seem to figure out the conditions I'd apply if I did this.

Hope I can get some help with this!

Try this:

        var arrMultipleOfTwo =[];
        var arrMultipleOfThree =[];
        var arrCommonOfTwoThree =[];
        var arrNonMultipleOfTwo = [];

        for(var i=0; i< 50; i++)
        {
            if((i%2)!=0){
                arrNonMultipleOfTwo.push(i);
            }
            if((i%2)==0){
                arrMultipleOfTwo.push(i);
            }
            if((i%3)==0){
                arrMultipleOfThree.push(i);
            }
            if((i%2)==0 || (i%3)==0){
                arrCommonOfTwoThree.push(i);
            }
        }

        console.log("--array of multiple of 2--"+arrMultipleOfTwo.toString());
        console.log("--array of multiple of 3--"+arrMultipleOfThree.toString());
        console.log("--array of multiple of 2 and 3 both--"+arrCommonOfTwoThree.toString());
        console.log("--array of non-multiple of 2--"+arrNonMultipleOfTwo.toString());
var arrMultipleOfTwo =[];
            var arrMultipleOfThree =[];
            var arrCommonOfTwoThree =[];
            var arrNonMultipleOfTwo = [];
            {
                var i=0;//count
                do {
                    i++;
                    if((i%2)!=0){
                        arrNonMultipleOfTwo.push(i);
                    }
                    if((i%2)==0){
                        arrMultipleOfTwo.push(i);
                    }
                    if((i%3)==0){
                        arrMultipleOfThree.push(i);
                    }
                    if((i%2)==0 || (i%3)==0){
                        arrCommonOfTwoThree.push(i);
                    }
                }
                while (i < 50){
                    console.log("--array of multiple of 2--"+arrMultipleOfTwo.toString());
                    console.log("--array of multiple of 3--"+arrMultipleOfThree.toString());
                    console.log("--array of multiple of 2 and 3 both--"+arrCommonOfTwoThree.toString());
                    console.log("--array of non-multiple of 2--"+arrNonMultipleOfTwo.toString());

                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM