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
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.