Skip to content

Instantly share code, notes, and snippets.

@vitaretnama
Last active April 22, 2022 04:43
Show Gist options
  • Save vitaretnama/70271613104be6e24fb7bc8fbb1c95fa to your computer and use it in GitHub Desktop.
Save vitaretnama/70271613104be6e24fb7bc8fbb1c95fa to your computer and use it in GitHub Desktop.
Codewars solutions 8 kyu Kata using JavaScript
// Codewars Solution
// 1. Exclamation marks series #1: Remove an exclamation mark from the end of string. Ex : doTest("Hi!", "Hi");
function remove(string) {
//coding and coding....
if (string[string.length - 1] == "!") {
return string.slice(0, string.length - 1);
} else {
return string;
}
}
// 2. Name Shuffler => Write a function that returns a string in which firstname is swapped with last name. Ex : nameShuffler('john McClane'); => "McClane john"
function nameShuffler(str) {
//Shuffle It
const fullName = str.split(" ");
return [fullName[1], fullName[0]].join(" ");
}
// 3. Capitalization and Mutability => JavaScript Program to Convert the First Letter of a String into UpperCase
function capitalizeWord(word) {
return word.charAt(0).toUpperCase() + word.slice(1);
}
// 4. String cleaning => Ex : 'This looks5 grea8t!' -> 'This looks great!'
function stringClean(s) {
// Function will return the cleaned string
return s.replace(/[0-9]/g, "");
}
// 5. Convert a string to an array
function stringToArray(string) {
// code code code
return string.split(" ");
}
// 6. Bin to Decimal => Complete the function which converts a binary number (given as a string) to a decimal number.
function binToDec(bin) {
// ...
return parseInt(bin, 2);
}
// 7. How good are you really? => Calculate the avarge of array and compare with your score
function betterThanAverage(classPoints, yourPoints) {
// Your code here
let total = 0;
for (let i = 0; i < classPoints.length; i++) {
total += classPoints[i];
}
let classAvg = total / classPoints.length;
return yourPoints > classAvg;
}
// 8. Stringy String => write a function stringy that takes a size and returns a string of alternating '1s' and '0s'. Ex: a string with size 6 should return :'101010'.
function stringy(size) {
const str = "";
return str.padStart(size, "10");
}
// 9. Removing elements => Take an array and remove every second element from the array. Always keep the first element and start removing with the next element. Ex : assert.deepEqual(removeEveryOther(['Hello', 'Goodbye', 'Hello Again']),['Hello', 'Hello Again']);
function removeEveryOther(arr) {
return arr.filter((_, index) => index % 2 == 0);
}
// 10. Rock Paper Scissors!
const rps = (p1, p2) => {
// Bikin p1 itu menang terus
if (p1 == p2) {
return "Draw!";
} else if (p1 == "rock" && p2 == "scissors") {
return "Player 1 won!";
} else if (p1 == "scissors" && p2 == "paper") {
return "Player 1 won!";
} else if (p1 == "paper" && p2 == "rock") {
return "Player 1 won!";
} else {
return "Player 2 won!";
}
};
// 1. Return => Make multiple functions that will return the sum, difference, modulus, product, quotient, and the exponent respectively.
function add(a, b) {
return a + b;
}
function divide(a, b) {
return a / b;
}
function multiply(a, b) {
return a * b;
}
function mod(a, b) {
return a % b;
}
function exponent(a, b) {
return a ** b;
}
function subt(a, b) {
return a - b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment