Created
February 23, 2022 02:55
-
-
Save natafaye/e35fc2e9465c75437ed0a04d1432f8dc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.codewars.com/kata/59de469cfc3c492da80000c5/train/javascript | |
function compress(sentence) { | |
// Get all the words lower case in an array | |
const words = sentence.split(" ").map(word => word.toLowerCase()); | |
// Get rid of all the duplicates | |
const uniqueWords = words.filter( (word, index) => words.indexOf(word) === index ); | |
// Make a string of the position of each word in the non-duplicates array | |
return words.map(word => uniqueWords.indexOf(word)).join(""); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.codewars.com/kata/57a1fd2ce298a731b20006a4/train/javascript | |
function isPalindrome(x) { | |
return x.toLowerCase() === x.toLowerCase().split("").reverse().join(""); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.codewars.com/kata/586e6d4cb98de09e3800014f/train/javascript | |
class VendingMachine { | |
constructor(items, money) { | |
this.items = items; | |
this.money = money; | |
} | |
vend(selection, itemMoney) { | |
itemMoney = parseFloat(itemMoney); | |
const item = this.items.find(item => item.code.toLowerCase() === selection.toLowerCase()); | |
// invalid item | |
if(!item) { | |
return "Invalid selection! : Money in vending machine = " + this.money.toFixed(2); | |
} | |
// not enough money | |
if(item.price > itemMoney) { | |
return "Not enough money!"; | |
} | |
// out of stock | |
if(item.quantity === 0) { | |
return item.name + ": Out of stock!"; | |
} | |
// add the money to the machine | |
this.money += itemMoney; | |
// remove the item from the machine | |
item.quantity--; | |
let message = "Vending " + item.name; | |
// handle change | |
if(item.price < itemMoney) { | |
const change = itemMoney - item.price; | |
// remove the change from the machine | |
this.money -= change; | |
message += " with " + change.toFixed(2) + " change."; | |
} | |
return message; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment