Ad
Arrays
Fundamentals

Today is your partner's birthday and you go to the supermarket to buy their favorite cake. Once you arrive, you realize there are two different options to choose from. You don't care which one tastes better, just which one is the most cost-effective, because as an extreme cheapskate, you want to save as much money as possible.

Example arrays:

cake1 = [4.90, 485, 'carrot cake 1'];  

cake2 = [4.50, 425, 'carrot cake 2'];  

Note: (The first value is the cost $ and the second the grams g)

Is it worth buying cake 1 with a higher cost for a few more grams? Or is it better to buy cake 2 with a lower cost but a few grams less? In the example of carrot cake, the best option would be cake1.

To make the best decision, we must compare the cost per gram of each cake.

Create a function that takes any pair of cake1 and cake2 arrays. Return the best option: 'choose cake 1' or 'choose cake 2'
If the two cakes have the same value return 'either of the two'

function chooseCake(cake1, cake2) {
  //Mr. Krabs is proud of you
  return cake1[0] / cake1[1] < cake2[0] / cake2[1] ? 'choose cake 1' :
    cake1[0] / cake1[1] > cake2[0] / cake2[1] ? 'choose cake 2' :
    'either of the two';
}

(Cambiar de posicion las palabras de una frase)

Code
Diff
  • function textoAlreves(text) {
      // write code here
      //prueba:
      return text.split(" ").reverse().join(' ');
    };
    
    //split separa cada palabra del string
    //reverse cambia las palabras del sitio
    //join vuelve a unir las palabras en una frase
    • function TextoAlreves(text) {
    • function textoAlreves(text) {
    • // write code here
    • }
    • //prueba:
    • return text.split(" ").reverse().join(' ');
    • };
    • TextoAlreves("Hola soy un Texto")
    • //split separa cada palabra del string
    • //reverse cambia las palabras del sitio
    • //join vuelve a unir las palabras en una frase