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';
}
const chai = require("chai");
const assert = chai.assert;
chai.config.truncateThreshold = 0;
describe('ChooseCake', () => {
it("the best carrot cake test", () => {
const cake1 = [4.90, 485, 'carrot cake 1'];
const cake2 = [4.50, 425, 'carrot cake 2'];
assert.strictEqual(chooseCake(cake1, cake2), 'choose cake 1');
});
it("the best apple cake test", () => {
const cake1 = [45.69, 1945, 'apple cake 1'];
const cake2 = [43.10, 1847, 'apple cake 2'];
assert.strictEqual(chooseCake(cake1, cake2), 'choose cake 2');
});
it("the best air cake test", () => {
const cake1 = [0, 0, 'air cake 1'];
const cake2 = [0, 0, 'air cake 2'];
assert.strictEqual(chooseCake(cake1, cake2), 'either of the two');
});
});
describe('More tests', () => {
it("random cake test", () => {
const cake1 = [Math.floor(Math.random() * 50), Math.floor(Math.random() * 200), 'cake 1'];
const cake2 = [Math.floor(Math.random() * 50), Math.floor(Math.random() * 200), 'cake 2'];
const result = chooseCake(cake1, cake2);
if (cake1[0] / cake1[1] < cake2[0] / cake2[1]) {
assert.strictEqual(chooseCake(cake1, cake2), 'choose cake 1');
} else if (cake1[0] / cake1[1] > cake2[0] / cake2[1]) {
assert.strictEqual(chooseCake(cake1, cake2), 'choose cake 2');
} else {
assert.strictEqual(chooseCake(cake1, cake2), 'either of the two');
}
});
});
(Cambiar de posicion las palabras de una frase)
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
const Test = require('@codewars/test-compat'); describe("TextoAlreves", () => { it("Basic tests", () => { Test.assertEquals(textoAlreves('Soy Yoda'), 'Yoda Soy'); Test.assertEquals(textoAlreves('Luke traeme una Cocacola'), 'Cocacola una traeme Luke'); Test.assertEquals(textoAlreves('No pepsi'), 'pepsi No'); Test.assertEquals(textoAlreves('Payaso'), 'Payaso'); Test.assertEquals(textoAlreves(''), ''); Test.assertEquals(textoAlreves('h'), 'h'); }); it("More tests", () => { Test.assertEquals(textoAlreves('cachorro come tofu'), 'tofu come cachorro') Test.assertEquals(textoAlreves('¿Por qué la gallina cruzó la carretera?'), 'carretera? la cruzó gallina la qué ¿Por') Test.assertEquals(textoAlreves('Vader pidió café'), 'café pidió Vader') Test.assertEquals(textoAlreves('programadores prefieren la oscuridad'), 'oscuridad la prefieren programadores') Test.assertEquals(textoAlreves('NO PAGAR FACTURA DE LUZ??'), 'LUZ?? DE FACTURA PAGAR NO') }); });
// Since Node 10, we're using Mocha.// You can use `chai` for assertions.const chai = require("chai");const assert = chai.assert;// Uncomment the following line to disable truncating failure messages for deep equals, do:// chai.config.truncateThreshold = 0;// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.// Uncomment the following to use the old assertions:// const Test = require("@codewars/test-compat");- const Test = require('@codewars/test-compat');
describe("Solution", function() {it("should test for something", function() {// Test.assertEquals(1 + 1, 2);// assert.strictEqual(1 + 1, 2);- describe("TextoAlreves", () => {
- it("Basic tests", () => {
- Test.assertEquals(textoAlreves('Soy Yoda'), 'Yoda Soy');
- Test.assertEquals(textoAlreves('Luke traeme una Cocacola'), 'Cocacola una traeme Luke');
- Test.assertEquals(textoAlreves('No pepsi'), 'pepsi No');
- Test.assertEquals(textoAlreves('Payaso'), 'Payaso');
- Test.assertEquals(textoAlreves(''), '');
- Test.assertEquals(textoAlreves('h'), 'h');
- });
- it("More tests", () => {
- Test.assertEquals(textoAlreves('cachorro come tofu'), 'tofu come cachorro')
- Test.assertEquals(textoAlreves('¿Por qué la gallina cruzó la carretera?'), 'carretera? la cruzó gallina la qué ¿Por')
- Test.assertEquals(textoAlreves('Vader pidió café'), 'café pidió Vader')
- Test.assertEquals(textoAlreves('programadores prefieren la oscuridad'), 'oscuridad la prefieren programadores')
- Test.assertEquals(textoAlreves('NO PAGAR FACTURA DE LUZ??'), 'LUZ?? DE FACTURA PAGAR NO')
- });