Question about these type of exercices (Roman Numeral Converter & Caesars Cipher)

Links:
https://beta.freecodecamp.org/en/challenges/intermediate-algorithm-scripting/roman-numeral-converter
https://beta.freecodecamp.org/en/challenges/intermediate-algorithm-scripting/caesars-cipher

Hello, i just did these exercices today but after watching for official responses, i find a bit “ashamed”, like i cheated or some thing. I used what first came to my mind and what i find the easiest way to achieve it and understand it.
It’s like i transformed an intermediate exercice into a newbi tutorial…

The officials responses:
https://forum.freecodecamp.org/t/freecodecamp-algorithm-challenge-guide-roman-numeral-converter/16044
https://forum.freecodecamp.org/t/freecodecamp-algorithm-challenge-guide-caesars-cipher/16003

The first (roman converter):

/*
Link: https://beta.freecodecamp.org/en/challenges/intermediate-algorithm-scripting/roman-numeral-converter
https://forum.freecodecamp.org/t/freecodecamp-algorithm-challenge-guide-roman-numeral-converter/16044
*/

function convertToRoman( num ) {
	let n = num.toString().length;
	
	let array = [  [0, ""],[1,"I"],[2,"II"],[3,"III"],[4,"IV"],[5,"V"],[6,"VI"],[7,"VII"],[8,"VIII"],[9,"IX"],[10, "X"],[20,"XX"],[30,"XXX"],
				   [40,"XL"],[50,"L"],[60,"LX"],[70,"LXX"],[80,"LXXX"],[90,"XC"],[100,"C"],[200,"CC"],[300,"CCC"],[400,"CD"],[500,"D"],
				   [600,"DC"],[700,"DCC"],[800,"DCCC"],[900,"CM"],[1000,"M"],[2000,"MM"],[3000,"MMM"],[4000,"MMMM"],[5000,"MMMMM"],
				   [6000,"MMMMMM"],[7000,"MMMMMMM"],[8000,"MMMMMMMM"],[9000,"MMMMMMMMM"]
				];
	
	if( num > 9999 ) {
		return "Too big";
	
	}else if ( num < 0 ){
		return "Too little";
	}
	
	if( n == 4 ) {
		let milleString = num.toString()[0]+"000";
		let centaineString = num.toString()[1]+"00";
		let dizaineString = num.toString()[2]+"0";
		let uniteString = num.toString()[3];

		let milleInt = parseInt( milleString );
		let centaineInt = parseInt( centaineString );
		let dizaineInt = parseInt( dizaineString );
		let uniteInt = parseInt( uniteString );
		
		for( let k = 0; k < array.length; k++ ) {
			if( milleInt == array[k][0] ) {
				milleInt = array[k][1];
			
			}
			if( centaineInt == array[k][0] ) {
				centaineInt = array[k][1];
			
			}
			if( dizaineInt == array[k][0] ) {
				dizaineInt = array[k][1];
			
			}
			if( uniteInt == array[k][0] ) {
				uniteInt = array[k][1];
			}
		}
		
		return milleInt+centaineInt+dizaineInt+uniteInt;
	
	}else if( n == 3 ) {
		let centaineString = num.toString()[0]+"00";
		let dizaineString = num.toString()[1]+"0";
		let uniteString = num.toString()[2];

		let centaineInt = parseInt( centaineString );
		let dizaineInt = parseInt( dizaineString );
		let uniteInt = parseInt( uniteString );

		for( let k = 0; k < array.length; k++ ) {
			if( centaineInt == array[k][0] ) {
				centaineInt = array[k][1];
			
			}
			if( dizaineInt == array[k][0] ) {
				dizaineInt = array[k][1];

			}
			if( uniteInt == array[k][0] ) {
				uniteInt = array[k][1];
			}
		}
	
		return centaineInt+dizaineInt+uniteInt;
		
	}else if( n == 2 ) {
		let dizaineString = num.toString()[0]+"0";
		let uniteString = num.toString()[1];

		let dizaineInt = parseInt( dizaineString );
		let uniteInt = parseInt( uniteString );
		
		for( let k = 0; k < 19; k++ ) {
			if( dizaineInt == array[k][0] ) {
				dizaineInt = array[k][1];
			
			}
			if( uniteInt == array[k][0] ) {
				uniteInt = array[k][1];
			}
		}
	
		return dizaineInt+uniteInt;
	
	}else if( n == 1 ) {
		let uniteString = num.toString()[0];
		
		let uniteInt = parseInt( uniteString );
		
		for( let k = 0; k < 10; k++ ) {
			if( uniteInt == array[k][0] ) {
				uniteInt = array[k][1];
			}
		}
		
		return uniteInt;
		
	}
};

console.log( convertToRoman( 2 ) );
console.log( convertToRoman( 3 ) );
console.log( convertToRoman( 4 ) );
console.log( convertToRoman( 5 ) );
console.log( convertToRoman( 9 ) );
console.log( convertToRoman( 12 ) );
console.log( convertToRoman( 16 ) );
console.log( convertToRoman( 29 ) );
console.log( convertToRoman( 44 ) );
console.log( convertToRoman( 48 ) );
console.log( convertToRoman( 68 ) );
console.log( convertToRoman( 83 ) );
console.log( convertToRoman( 97 ) );
console.log( convertToRoman( 99 ) );
console.log( convertToRoman( 400 ) );
console.log( convertToRoman( 500 ) );
console.log( convertToRoman( 501 ) );
console.log( convertToRoman( 649 ) );
console.log( convertToRoman( 798 ) );
console.log( convertToRoman( 891 ) );
console.log( convertToRoman( 999 ) );
console.log( convertToRoman( 1000 ) );
console.log( convertToRoman( 1004 ) );
console.log( convertToRoman( 1006 ) );
console.log( convertToRoman( 1023 ) );
console.log( convertToRoman( 2014 ) );
console.log( convertToRoman( 3999 ) );
console.log( convertToRoman( 9999 ) );

console.log( convertToRoman( 10000 ) );
console.log( convertToRoman( -1 ) );
console.log( convertToRoman( 0 ) );

.
The second (Caesars Cipher):

/*
Link: https://beta.freecodecamp.org/en/challenges/intermediate-algorithm-scripting/caesars-cipher
https://forum.freecodecamp.org/t/freecodecamp-algorithm-challenge-guide-caesars-cipher/16003
*/

function rot13( str ) {
	let array = [ ['A', 'N'],['B','O'],['C','P'],['D','Q'],['E','R'],['F','S'],['G','T'],['H','U'],['I','V'],['J','W'], 
				  ['K','X'],['L','Y'],['M','Z'],['N','A'],['O','B'],['P','C'],['Q','D'],['R','E'],['S','F'],['T','G'],
				  ['U','H'],['V','I'],['W','J'],['X','K'],['Y','L'],['Z','M'],['!','!'],['?','?'],['.','.'],[' ',' ']
				];
	let arrL = array.length;
	let strL = str.length;
	let newStr = "";
	
	for( let k = 0; k < strL; k++ ) {
		for( let x = 0; x < arrL; x++ ) {
			if( str[k] == array[x][0] ) {
				newStr += array[x][1];
			}
		}
	}
	
	return newStr;
};

console.log( rot13( "SERR PBQR PNZC" ) );
console.log( rot13( "SERR CVMMN!" ) );
console.log( rot13( "SERR YBIR?" ) );
console.log( rot13( "GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT." ) );

.
Some of you will say “Well, you achieved it, it’s ok. But look at the more ‘officials’ responses and if you understand them and can reproduce them, it’s ok”.
The thing is, i don’t completly understand them :confused:

I have trouble using reduce/map/filter function so if i can avoid them, i do. I know it’s not good to avoid difficulties. It’s better to take time and try to learn from them so you can reuse this skill later and gain time.

I think i have to force me to use these methods even if i know a way to not do it. Would be better in the long run and will train my mind in wider/complex problem.

I feel a bit like talking to myself and doing a self therapy. You can enter the discussion:)

Have a nice day.

3 Likes

Hello, is it that bad to use array like i did to achieve conversion type like exercices ?
Is it bad to “rush” an exercice the simpliest way you can think at the moment and depending on your skill if you can take much (much much) more time to think and figure it out an other way that is more productive for your skill in a more long/professional run.
Like i said, i feel ashamed i have finished exercices like this.

1 Like

I very often will solve something the simplest and most obvious way first. I think that this gives me a better grasp of the problem that I’m working on. Once the solution is running, I can look at it and find ways to improve my solution. In a way, this means I’m getting more bang for my buck per challenge because I am solving it more than once.

6 Likes

Thanks you both ! I’m going to read more precisely the code you wrote to me tomorrow !

Hello back !
I really like what you used with the boolean and break then the ternary is sweet :slight_smile:
It really manage the special ponctuations and a gain of time with the break.
Is there performance difference between object and array ?

And thanks you for your feedback about “simple” code. I will try it out !

Thanks you very much ! I knew some one good in maths could answere this xD

// is this a good approach to hardcode the cipher like this,

function rot13(str) {
const alphabet = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’;
const cipher = ‘NOPQRSTUVWXYZABCDEFGHIJKLM’;
return str.split(’’).map((elem) => alphabet.indexOf(elem) !== -1 ? cipher[alphabet.indexOf(elem)] : elem).join(’’);
}

rot13(“SERR PBQR PNZC”);