[04.15.22] Codewars
5493 ワード
Break camelCase
Description
Complete the solution so that the function will break up camel casing, using a space between words.
My answer
function solution(string) {
return [...string].map(function(letter) {
return letter === letter.toUpperCase() ? letter = ` ${letter}` : letter
}).join('')
}
Other solutions
function solution(string) {
return(string.replace(/([A-Z])/g, ' $1'));
}
function solution(string) {
return string.replace(/([a-z])([A-Z])/g, "$1 $2");
}
Wrap up
RegExp.$1-$9
$1, ..., $9 properties are static, they are not a property of an individual regular expression object. Instead, you always use them as RegExp.$1, ..., RegExp.$9.
The values of these properties are read-only and modified whenever successful matches are made.
The number of possible parenthesized substrings is unlimited, but the RegExp object can only hold the first nine. You can access all parenthesized substrings through the returned array's indexes.
These properties can be used in the replacement text for the String.replace method. When used this way, do not prepend them with RegExp. The example below illustrates this. When parentheses are not included in the regular expression, the script interprets $n's literally (where n is a positive integer).
Examples
var re = /(\w+)\s(\w+)/;
var str = 'John Smith';
str.replace(re, '$2, $1'); // "Smith, John"
RegExp.$1; // "John"
RegExp.$2; // "Smith"
source: mdn web docs
Multiples of 3 or 5
Description
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in. Additionally, if the number is negative, return 0 (for languages that do have them).
Note: If the number is a multiple of both 3 and 5, only count it once.
My answer
function solution(number){
let array = [];
if (number < 0) {
return 0;
} else {
for(let i = 1; i < number; i++) {
array.push(i);
}
const three = array.filter(ele => ele % 3 === 0);
const five = array.filter(ele => ele % 5 === 0);
const result = [...new Set([...three, ...five])];
return result.reduce((a,b) => a+b, 0);
}
}
Other solutions
function solution(number){
var sum = 0;
for(var i = 1;i< number; i++){
if(i % 3 == 0 || i % 5 == 0){
sum += i
}
}
return sum;
}
function solution(number){
return number < 1 ? 0 : [...new Array(number).keys()].filter(n => n % 3 == 0 || n % 5 == 0).reduce((a, b) => a + b);
}
Wrap up
keys method
The keys() method returns a new Array Iterator object that contains the keys for each index in the array. It returns a new Array iterator object.array.keys()
Examples
const array1 = ['a', 'b', 'c'];
const iterator = array1.keys();
for (const key of iterator) {
console.log(key);
}
// expected output: 0
// expected output: 1
// expected output: 2
const arr = ['a', , 'c'];
const sparseKeys = Object.keys(arr);
const denseKeys = [...arr.keys()];
console.log(sparseKeys); // ['0', '2']
console.log(denseKeys); // [0, 1, 2]
source: mdn web docs
Disemvowel Trolls
Description
Trolls are attacking your comment section!
A common way to deal with this situation is to remove all of the vowels from the trolls' comments, neutralizing the threat.
Your task is to write a function that takes a string and return a new string with all vowels removed.
For example, the string "This website is for losers LOL!"would become "Ths wbst s fr lsrs LL!".
Note: for this kata y isn't considered a vowel.
My answer
function disemvowel(str) {
return str.replace(/[aeiou]/gi, '');
}
Other solutions
const vowels = 'aeiou';
function disemvowel(str) {
return str
.split('')
.filter(letter => !vowels.includes(letter.toLowerCase()))
.join('');
}
Wrap up
Regex g, Regex i
g = global, match all instances of the pattern in a string, not just one
i = case-insensitive (so, for example,/a/i will match the string "a"or "A".
source: stackoverflow
Reference
この問題について([04.15.22] Codewars), 我々は、より多くの情報をここで見つけました
https://velog.io/@jay_jykim91/04.15.22-Codewars
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
function solution(string) {
return [...string].map(function(letter) {
return letter === letter.toUpperCase() ? letter = ` ${letter}` : letter
}).join('')
}
function solution(string) {
return(string.replace(/([A-Z])/g, ' $1'));
}
function solution(string) {
return string.replace(/([a-z])([A-Z])/g, "$1 $2");
}
var re = /(\w+)\s(\w+)/;
var str = 'John Smith';
str.replace(re, '$2, $1'); // "Smith, John"
RegExp.$1; // "John"
RegExp.$2; // "Smith"
Description
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in. Additionally, if the number is negative, return 0 (for languages that do have them).
Note: If the number is a multiple of both 3 and 5, only count it once.
My answer
function solution(number){
let array = [];
if (number < 0) {
return 0;
} else {
for(let i = 1; i < number; i++) {
array.push(i);
}
const three = array.filter(ele => ele % 3 === 0);
const five = array.filter(ele => ele % 5 === 0);
const result = [...new Set([...three, ...five])];
return result.reduce((a,b) => a+b, 0);
}
}
Other solutions
function solution(number){
var sum = 0;
for(var i = 1;i< number; i++){
if(i % 3 == 0 || i % 5 == 0){
sum += i
}
}
return sum;
}
function solution(number){
return number < 1 ? 0 : [...new Array(number).keys()].filter(n => n % 3 == 0 || n % 5 == 0).reduce((a, b) => a + b);
}
Wrap up
keys method
The keys() method returns a new Array Iterator object that contains the keys for each index in the array. It returns a new Array iterator object.
array.keys()
Examples
const array1 = ['a', 'b', 'c'];
const iterator = array1.keys();
for (const key of iterator) {
console.log(key);
}
// expected output: 0
// expected output: 1
// expected output: 2
const arr = ['a', , 'c'];
const sparseKeys = Object.keys(arr);
const denseKeys = [...arr.keys()];
console.log(sparseKeys); // ['0', '2']
console.log(denseKeys); // [0, 1, 2]
source: mdn web docs Disemvowel Trolls
Description
Trolls are attacking your comment section!
A common way to deal with this situation is to remove all of the vowels from the trolls' comments, neutralizing the threat.
Your task is to write a function that takes a string and return a new string with all vowels removed.
For example, the string "This website is for losers LOL!"would become "Ths wbst s fr lsrs LL!".
Note: for this kata y isn't considered a vowel.
My answer
function disemvowel(str) {
return str.replace(/[aeiou]/gi, '');
}
Other solutions
const vowels = 'aeiou';
function disemvowel(str) {
return str
.split('')
.filter(letter => !vowels.includes(letter.toLowerCase()))
.join('');
}
Wrap up
Regex g, Regex i
g = global, match all instances of the pattern in a string, not just one
i = case-insensitive (so, for example,/a/i will match the string "a"or "A".
source: stackoverflow
Reference
この問題について([04.15.22] Codewars), 我々は、より多くの情報をここで見つけました
https://velog.io/@jay_jykim91/04.15.22-Codewars
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
function disemvowel(str) {
return str.replace(/[aeiou]/gi, '');
}
const vowels = 'aeiou';
function disemvowel(str) {
return str
.split('')
.filter(letter => !vowels.includes(letter.toLowerCase()))
.join('');
}
Reference
この問題について([04.15.22] Codewars), 我々は、より多くの情報をここで見つけました https://velog.io/@jay_jykim91/04.15.22-Codewarsテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol