-2

I have this javascript code and when you type in encrypt("this has no use yet", "B16-4131-8731") it should log the result of 8/7+3*1 But it says unexpected end of input

function encrypt(prompt, MathId) {
  // Prompt to base10
  var convertedPrompt = "";
  for (var i = 0; i < prompt.length; i++) {
    var asciiCode = prompt.charCodeAt(i);
    convertedPrompt += asciiCode;
  }

  // Cut Prompt every 4 letters
  var resultPrompt = "";
  var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  for (var i = 0; i < convertedPrompt.length; i += 4) {
    var part = convertedPrompt.substr(i, 4);
    var letter = letters.charAt(i / 4);
    resultPrompt += part + " (" + letter + ") ";
  }

  var parts = MathId.split("-");
  let mainBase = parts[0];
  let MathOrder = parts[1];
  let ExtraGate = parts[2];

  var conversionTable = {
    "4": "/",
    "3": "*",
    "2": "-",
    "1": "+"
  };

  var convertedMathOrder = "";
  for (var i = 0; i < MathOrder.length; i++) {
    var digit = MathOrder[i];
    var operator = conversionTable[digit];

    if (operator) {
      convertedMathOrder += operator;
    }
  }

  var finalResult = "";
  for (var j = 0; j < MathOrder.length; j++) {
    var digitB = ExtraGate[j];
    finalResult += digitB + convertedMathOrder[j];
  }

  var finalOutput = eval(finalResult);

  return finalOutput;
}

console.log(encrypt("this has no use yet", "B16-4131-8731"));

I really cant find the line causing the error

David
  • 208,112
  • 36
  • 198
  • 279
  • 5
    Step 1. Don't use `eval`. Step 2. if you're going to use `eval` *figure out what you're evaluating*. [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – VLAZ Aug 30 '23 at 11:44
  • Looks like `finalResult` has a stray trailing `+`... – Cerbrus Aug 30 '23 at 11:44
  • 5
    This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? – David Aug 30 '23 at 11:45
  • 3
    @VLAZ Step 3. Don't use ```eval``` – Ja Da Aug 30 '23 at 11:57
  • this error comes from this line `var finalOutput = eval(finalResult)`, wich is try todo this calculation: `eval('8/7+3*1+')` – Mister Jojo Aug 30 '23 at 12:41

1 Answers1

1

If you wish to mprove your code...

console.log('->', encrypt("this has no use yet", "B16-4131-8731"));

function encrypt(prompt, MathId)
  {
  const
    alphas          = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  , convertedPrompt = [...prompt].map(c=>c.charCodeAt(0).toString(10)).join('') // Prompt to base10
  , resultPrompt    = (convertedPrompt.match(/.{1,4}/g) ?? []).reduce((s,n,i)=> s + `${n} (${alphas[i]}) `,'')
  , [ mainBase, MathOrder, ExtraGate ] = MathId.split("-")
  , ops = 
     { '1': (a,b) => a + b
     , '2': (a,b) => a - b
     , '3': (a,b) => a * b
     , '4': (a,b) => a / b
     };
  let finalOutput = NaN
    , operator    = ''
    ;
  [...MathOrder].forEach( (op,i) =>
    {    
    finalOutput = isNaN(finalOutput) ? Number(ExtraGate[i]) : ops[operator]( finalOutput, Number(ExtraGate[i]));
    operator    = op;
    });
  return finalOutput;
  }
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40