Why I think this way?

Why I think this way?
0

#1

Hello
the past two days i tried to solve the Roman Numeral Converter challenge ,
I read about it and understand the numbers and the combinations then on a paper I started to decide
how will I solve that it was nice and clean on Paper :grin: then I started to turn my ideas into code
here was the problem my code was so complicated that I my self got lost among its lines and it doesn’t work as I expected there is my code


function convertToRoman(num) {
   var numArr = (num+'').split``.map(x => x/1);
   console.log(numArr);
  var numericalValueArr = numArr.map((number,index,array)=>{
    if(array.length==1){
        return number;
    }

    var slicedArr=array.slice(index,array.length-1).length;

    var tenToThePowerOfX= Math.pow(10,slicedArr);

    return number*tenToThePowerOfX;
  });
  console.log(numericalValueArr);

  var romanNumerals =[
      {
          value:1,
          symbol:"I"
      },
      {
        value:5,
        symbol:"V"
    },
    {
        value:10,
        symbol:"X"
    },
    {
        value:50,
        symbol:"L"
    },
    {
        value:100,
        symbol:"C"
    },
    {
        value:500,
        symbol:"D"
    },
    {
        value:1000,
        symbol:"M"
    }
   ];


  function converter(){

      var numericalValue= numericalValueArr.map((num)=>{
          for( let[ index,number] of romanNumerals.entries()){

            for(let anotherNumber of romanNumerals){

                var subtract=num==number.value-anotherNumber.value;

                switch(true){
                    case num==0 :
                    return null;
                    break;

                    case num/number.value==1 :
                    remains=0;
                    return number.symbol;
                    break;

                    case subtract :
                    return anotherNumber.symbol+number.symbol;
                    break;

                    case remains==0&&num/number.value==2&&num!==number.value :
                    return number.symbol+number.symbol;
                    break;

                    case remains/anotherNumber.value==1&&num!==number.value :
                    return target.symbol+anotherNumber.symbol;
                    break;
                    
                    case remains/anotherNumber.value==2&&num!==number.value :
                    return target.symbol+anotherNumber.symbol+anotherNumber.symbol;
                    break;

                    case remains/anotherNumber.value==3&&num!==4*Math.pow(10,Number) :
                    return target.symbol+anotherNumber.symbol+anotherNumber.symbol+anotherNumber.symbol;
                    break;
                    
                    
                }
                
                console.log("NUM "+num);
               
                var correctNum= num>10&&num<100 ? 10 :100;
                console.log(correctNum);
                var remains;


                if(num>number.value){
                 remains = num%number.value;
                 var target=number;
                 console.log(remains,number.value);
                }
                
                
            }   
          }
    });
      console.log('Roman '+numericalValue.join(''));
  }
  converter();
   
}
   
   convertToRoman(200);

compare my code to this code you will understand my problem

var convertToRoman = function(num) {

  var decimalValue = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ];
  var romanNumeral = [ 'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I' ];

  var romanized = '';

  for (var index = 0; index < decimalValue.length; index++) {
    while (decimalValue[index] <= num) {
      romanized += romanNumeral[index];
      num -= decimalValue[index];
    }
  }

  return romanized;
}

// test here
convertToRoman(36);


please try to help me on this situation with any advice , thank you


#2

One thing you can learn is that sometimes it is much easier storing two arrays separately when their indexes are meant to perfectly mirror eachother. An object is meant for more complex data, and while it is very easy to organize and explicitely say what is what, an array is better as it’s just a collection of data.


#3

thank you ,
if you have any ideas or instructions for me to follow when i solve algorithms I will be thankful .


#4

I like to write down my solutions on paper too. However I’ve noticed that how my brain solves the problem intuitively is not how I can go about it in the code. Try to break down the logic as much as possible, draw out that actions are taking place, and keep track of each change in data.

Don’t make this stuff up though, you’re just translating what should be happening in your code, so make sure to double check what you write out.

I got stuck with sorting the highest number algorithm and gave this a shot. I found that when I look at the array I drew out, I intuitively knew what was highest and lowest at a glance. So I focused only on one index at a time, and only from left to right. I had to really handicap myself. I’d ask if this number is greater than the next one. Eventually I learned that if I asked myself which was greater or not that was the wrong way to think about it. I calculated the difference between them.


#5

I solved this kata with different approaches and languages. and found that collection of tuples or arrays are working just fine. But two separate arrays always bugged me for some reason.
In js collection of objects working ok, it’s just bit verbose.

/* jshint esversion: 6 */
const literals = [ { literal: 'M', value: 1000 },
  { literal: 'CM', value: 900 }, { literal: 'D', value: 500 }, { literal: 'CD', value: 400 }, { literal: 'C', value: 100 },
  { literal: 'XC', value: 90 }, { literal: 'L', value: 50 },  { literal: 'XL', value: 40 }, { literal: 'X', value: 10 },
  { literal: 'IX', value: 9 },  { literal: 'V', value: 5 },  { literal: 'IV', value: 4 }, { literal: 'I', value: 1 } ];

const convertToRoman = function convertToRoman(acc, num) {
  const { literal, value } = literals.find(({ value }) => num / value >= 1) || {};
  return literal ? convertToRoman(acc.concat(literal), num - value) : acc;
}.bind(null, '');

convertToRoman(44);

#6

Sorry. I just meant it is a lot easier to understand, and it really only works if your value equivalents share the same index. The solution OP provided was much easier to read than his solution.