Function that converts dollars to real, Mexican pesos and Chilean pesos

Asked

Viewed 980 times

1

Write a function with the name coinConverter that converts the value of dollars to real, Mexican pesos and Chilean pesos.

Use the following exchange rates:

reais = dólares * 3.25 // 
pesosMexicanos = dólares * 18 // pesosChilenos = dólares * 660

Code:

module.exports = function coinConvert(usDollars) {
  var arr = [];
  var reais = [usDollars * 3.25];
  var mexicanPesos = [usDollars * 18];
  var chileanPesos = [usDollars * 660];

  // Adicione o valor equivalente em reais
  arr.push(50*3.25);

  // Adicione o valor equivalente em pesos mexicanos
  arr.push(50*18);

  // Adicione o valor equivalente em pesos chilenos
  arr.push(50*660);

  return arr;
};

I put the data and only one gets Ok... And this error appears:

coinConvert()
  • should return [162.5, 900, 33000], for $50, okay
  • should return [325, 1800, 66000], for $100
  • expected [ 162.5, 900, 33000 ] to Deeply Equal [ 325, 1800, 66000 ]
  • should return [243.75, 1350, 49500], for $85
  • expected [ 162.5, 900, 33000 ] to Deeply Equal [ 276.25, 1530, 56100 ]
  • 1

    Hello @Jeh, note in your code that the value "50" is fixed when running the array.push(), i.e., Voce must change the value "50" according to the variables created above. You created 3 (real, mexicanPesos and chileanPesos), only you did nothing with them.

  • But how do I do it?

2 answers

0

Jeh, you are using a constant to multiply by the exchange rate, so your value is always equal! To solve your problem, you must use the variable that is received in the function, which in your case is usDollars.

  // Adicione o valor equivalente em reais
  arr.push(usDollars*3.25);

  // Adicione o valor equivalente em pesos mexicanos
  arr.push(usDollars*18);

  // Adicione o valor equivalente em pesos chilenos
  arr.push(usDollars*660);

Therefore, this part of the code has no function, and can be deleted, because you will not be using, and returns only the array arr:

  var reais = [usDollars * 3.25];
  var mexicanPesos = [usDollars * 18];
  var chileanPesos = [usDollars * 660];
  • Thank you Pedro!

0


You are already doing the calculations that the exercise asks for:

var reais = [usDollars * 3.25];
var mexicanPesos = [usDollars * 18];
var chileanPesos = [ usDollars * 660 ];
/// ;              ^                 ^
/// ; você não precisa usar array aqui

You just need to put these values within your variable arr instead of using the fixed value. Then your code should look +/- like in the example below:

module.exports = function coinConvert(usDollars) {
  var arr = [];
  var reais = usDollars * 3.25;
  var mexicanPesos = usDollars * 18;
  var chileanPesos = usDollars * 660;
  ///  ; NOTE que aqui eu removi o array `[]` 

  //arr.push(50*3.25);
  arr.push(reais);
  /// ;     ^ tire o valor fixo e coloque sua variável que esta fazendo o calculo 

  //arr.push(50*18);
  arr.push(mexicanPesos);
  /// ;     ^ mesma coisa aqui

  //arr.push(50*660); 
  arr.push(chileanPesos);
  /// ;     ^ mesma coisa aqui

  return arr;
};

See your function running:

function coinConvert(usDollars) {
  var arr = [];
  var reais = usDollars * 3.25;
  var mexicanPesos = usDollars * 18;
  var chileanPesos = usDollars * 660;
  ///  ; NOTE que aqui eu removi o array `[]` 

  arr.push(reais);
  /// ;     ^ mudei aki

  arr.push(mexicanPesos);
  /// ;     ^ mudei aki

  arr.push(chileanPesos);
  /// ;     ^ mudei aki

  return arr;
};

let v = prompt("Entre com o valor");
v = parseFloat(v);
if( isNaN(v) ) v = 0;

alert( coinConvert( v ) );

  • 1

    Thank you Icaro!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.