Need Help smallest-common-multiple

Need Help smallest-common-multiple
0

#1

Hello, i need to scan an multiple array and if i find a value false, it return me false.
On the contrary, if no false is found, it return me true.

[ [true, 1], [true, 2], [true, 3] ]; // give me true
[ [false, 1], [true, 2], [false, 3] ]; // give me false

https://www.w3schools.com/jsref/jsref_every.asp


#2

Firstly, welcome to the forums.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://freecodecamp.org.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:


#3

Will the false values (when they are present) always be in the inner array? If so, then you could use every and find like:

const noFalse = arr => arr
  .every(subArr => subArr
    .find(elem => elem === false) === undefined);

or use some and find like:

const noFalse = arr => !arr
  .some(subArr => subArr
    .find(elem => elem === false) === false);

#4

Could you write that in ES5? I think the OP will understand it better that way.


#5

Sure.

Using every and find:

function noFalse(arr) {
  return arr.every(function(subArr) {
    return undefined === subArr.find(function (elem) {
      return elem === false;
    });
  });
}

Using some and find:

function noFalse(arr) {
  return !arr.some(function(subArr) {
    return false === subArr.find(function(elem) {
      return elem === false;
    });
  });
}

Though, I think it flows better using arrow functions.


#6

Hello, thx for your response.
Actualy, i’m trying to do one of the fcc exercice there:
Link:https://beta.freecodecamp.org/en/challenges/intermediate-algorithm-scripting/smallest-common-multiple

But i’m having trouble, lots of trouble. Could you help me to debug and make my code work the way i tried ti do it:

/*
Link: https://beta.freecodecamp.org/en/challenges/intermediate-algorithm-scripting/smallest-common-multiple
*/

function noFalse( arr ) {
	return arr.every( function( subArr ) {
		return undefined === subArr.find( function( elem) {
			return elem === false;
		});
	});
};

function smallestCommons( arr ) {
	let min = Math.min.apply(Math, arr); //1
	let max = Math.max.apply(Math, arr); //3
	let bool = true;
	let smallArr = [min]; //[1]
	let bigArr = [max]; //[3]
	let boolArr = [];
	let i = 0;
	let valueSmall = 0;
	let valueMax = 0;
	
	let smallArrL = smallArr.length;
	let bigArrL = bigArr.length;
	
	do {
		for( let x = 0; x < smallArrL; x++ ) {
			for( let z = 0; z < bigArrL; z++ ) {
				if( smallArr[x] == bigArr[z] ) {
					valueMax = bigArr[z];
					valueSmall = smallArr[x];
					for( let k = 2; k < valueMax; k++ ) {
						if( Number.isInteger( valueSmall/k ) == true ) {
							boolArr.push( true );
						
						}else {
							boolArr.push( false );
						}
					}
				
				}else {

					i++;
					smallArr.push( smallArr[i-1] + min );
					bigArr.push( bigArr[i-1] + max );
				}
			}
		}	
		
		smallArrL = smallArr.length;
		bigArrL = bigArr.length;	
		
		console.log(boolArr);
		
		if( noFalse( boolArr ) == true ) {
			bool = false;
			let val = boolArr[0][1];
			
			return val;
		}
	
	}while( bool == true );
};

console.log( smallestCommons( [1,3] ) );

It’s pretty chaotic but i think it should work. If i could have some tips on what to fix.


#7

Can you explain in plain language what your code is trying to do? It would be nice to understand the purpose of some of the variables. Once you can explain what you think the code is supposed to be doing, then it gives us a better chance of steering you in the right direction.

FYI - It is not necessary to blur out a solution which does not pass the challenge tests.


#8

Okay ! Gonna do this !
And bluring a code that need help is not good because you can’t copy/past it :confused:


#9

You are correct. That is why I only blur full solutions and not something on which I need help.


FreeCodeCamp - Arguments Optional - Need Help
#11
/*
Link: https://beta.freecodecamp.org/en/challenges/intermediate-algorithm-scripting/smallest-common-multiple
*/

function noFalse( arr ) {
	return arr.every( function( subArr ) {
		return undefined === subArr.find( function( elem) {
			return elem === false;
		});
	});
};

function smallestCommons( arr ) {
	let min = Math.min.apply(Math, arr); //1
	let max = Math.max.apply(Math, arr); //3
	let bool = true;
	let smallArr = [min]; //[1]
	let bigArr = [max]; //[3]
	let boolArr = [];
	let i = 0;
	let valueSmall = 0;
	let valueMax = 0;
	
	let smallArrL = smallArr.length;
	let bigArrL = bigArr.length;
	
	
	//Do this while bool is 'true'
	do {
		/*
		smallArr[0] == 1; (It's my min value from my original array 'arr')
		bigArr[0] == 3; (It's my max value from my original array 'arr')
		
		I check one value at a time from 'smallArr' if it's equal to all my values from 'bigArr'
		*/
		for( let x = 0; x < smallArrL; x++ ) {
			for( let z = 0; z < bigArrL; z++ ) {
				/*
				if one value is equal to the other one like '3' or '6' for the '[1, 3]' exemple:
				I collect this value for more simple coding.
				Then in a for loop, i check in the interval k=2 (because i don't need '1') to valueMax-1.
				So for the first try, it's only 2 which is tested. The next time it will be '2 to 6'
				"valueSmall/2" -> 3/2 == 1.5 so it's false. I creat a value 'false in my table 'boolArr'. 
				*/
				if( smallArr[x] == bigArr[z] ) {
					valueMax = bigArr[z];
					valueSmall = smallArr[x];
					for( let k = 2; k < valueMax; k++ ) {
						if( Number.isInteger( valueSmall/k ) == true ) {
							//the 'k' help me to know at which indexe
							boolArr.push( true, k );
						
						}else {
							boolArr.push( false );
						}
					}
				
				}else {
					/*
					If the first comparaison do not match, i add new values to my arrays:
					smallArr = 1+1 so 2 : [1, 2]
					bigArr = 3+3 so 6 : [3, 6]
					With it, i can reuse my two first for loop in a dynamic way and check next values
					*/
					i++;
					smallArr.push( smallArr[i-1] + min );
					bigArr.push( bigArr[i-1] + max );
				}
			}
		}	
		
		/*
		Don't think it's necessary...
		*/
		smallArrL = smallArr.length;
		bigArrL = bigArr.length;	
		
		/*
		If i have only true values, it mean that the interval between, for exemple, '2 and 6' is divisible without float value.
		*/
		if( noFalse( boolArr ) == true ) {
			//go out from the 'while loop' because i found my smallest common multiple
			bool = false;
			//i recover my index
			let val = boolArr[0][1];
			//then i use this index to find my value
			return bigArr[val];
		}
	
	}while( bool == true );
};

console.log( smallestCommons( [1,3] ) );

//1 et 3
//1 -> 	1	;	2	;	3	;	4	;	5	;	6
//3 -> 	3	;	6

I hope my comment can help you. Even me, i was not sure for every thing i did xD


#12

Question #1) Do you understand why your code is only making it through one iteration of the inner for loop? The same goes for the outer loop.

Question #2) How are you going to get the for loops to iterate more than one iteration?


#13

Well, for now, i think i will try to figure out a new solution. Will update today.


#14

Hello back, i think my new code is “better”. In my solution, i create an array with the first 10 values (0-9).
If i find my solution in these 10 possibilities, it return my result.
For exemple, here for [1, 3], it return me 6 because it is in the first 10 possibilities.
But if it’s not the case, i save my last value from my multidimentional array. Then i delete all the value from my original array. And i start to re-fill my array with 10 next new values. I do it and if it succefully find my result, i get out.
I tried to find a way to have some thing dynamic and not to much cpu heavy…
But my problem is i have issue to…get out :confused:

/*
Link: https://beta.freecodecamp.org/en/challenges/intermediate-algorithm-scripting/smallest-common-multiple
*/

function smallestCommons( arr ) {
	let min = Math.min.apply( Math, arr ); //1
	let max = Math.max.apply( Math, arr ); //3
	let minCopy = min;
	let maxCopy = max;
	let myArray = [ [min, max] ];
	let myNewArray = [];
	//global variable
	bool = false;
	//global variable
	boolOutWhile = false;
	
	//I create my original array with the first 10 values (0-9)
	for( let k = 0; k < 9; k++ ) {
		myArray.push( [minCopy = minCopy+min, maxCopy = maxCopy+max]);	
	}

	do {
		//I make a copy of the last value of my array for later
		myNewArray.push( myArray[myArray.length-1] ); //[10, 30]
		let lastValueMin = myNewArray[myNewArray.length-1][0]; //10
		let lastValueMax = myNewArray[myNewArray.length-1][1]; //30
		
		let myArrayL = myArray.length; //10
		
		//Two 'foor-loop' to check every value from myArray[k] with myArray[][x]
		for( k = 0; k < myArrayL; k++ ) {
			for( let x = 0; x < myArrayL; x++ ) {
				//If i have a match then
				if( myArray[k][0] == myArray[x][1] ) {
					//I make a copy of the number matching for my next 'for loop' interval (here: 3, 6 and 9 matches)
					valueCopy = myArray[x][1];

					/*		
					3 is EQUAL to 3
					6 is EQUAL to 6
					9 is EQUAL to 9
					*/
					// console.log( myArray[k][0]+" is EQUAL to "+myArray[x][1]);
					
					/*
					It's 1 and 3 because the interval is between min(1) and max(3)
					3/1=(3) 3/2(1.5) 3/3=(1)// 6/1=(6) 6/2=(3) 6/3=(2) // 9/1=(9) 9/2=(4.5) 9/3=(3) 
					*/
					for( let i = min; i <= max; i++ ) {
						//If my valueCopy / i (3/1, 3/2, 3/3...) is not an Integer then i stop
						if( Number.isInteger( valueCopy / i ) == false ) {
							bool = false;
							i = max;
						
						//else, my bool have a value of 'true'
						}else {
							bool = true;
						}
					}
					//If my bool still have a value of true at the end of the last 'for-loop' then it's done
					if( bool == true ) {
						//i can get out of the 'while-loop'
						boolOutWhile = true;
						
						//return my result
						return valueCopy;
					}	
				}
			}
		}
		//delete my useless value to do it again
		myArray.splice(0);
		myNewArray.splice(0);
		
		//re-fill my array with new values from the end of my last ten first array values
		/*
		For exemple, if 6 was not the value to return for 1-3, then i would have:
		[
			[11, 33], [12, 36], [13, 39], [14, 42], [...], [20, 60]
		]
		*/
		for( k = 0; k <= 9; k++ ) {
			myArray.push( [lastValueMin = lastValueMin+min, lastValueMax = lastValueMax+max]);	
		}	
		
	}while( boolOutWhile == false );
};

// console.log( smallestCommons( [1,3] ) ); //find 6
//console.log( smallestCommons( [1,5] ) ); //failed

#15

Hi back, i spend my time finishing the next 2 exercices :slight_smile:
(not bad for uping a topic xD)


#16

Hello, upping my topic.


#17

Well, i finished all of the others exercices. I will take time for this one.
I think i have a comprehension issue regarding the while() and how to get out of it with embricated stuff in it.
My while doesn’t seem to know when my bool is true of false.