JSchallenger にチャレンジ - Fundamentals / Basics
Notion にまとめていたものを少しずつ移行中。後ほど解説も追加します。
Fundamentals 編
JSchallenger にチャレンジ - Fundamentals / Basics
JSchallenger にチャレンジ - Fundamentals / Arrays
JSchallenger にチャレンジ - Fundamentals / Objects
JSchallenger にチャレンジ - Fundamentals / Dates
JSchallenger にチャレンジ - Fundamentals / Sets
DOM 編
JSchallenger にチャレンジ - DOM / DOM selector methods
JSchallenger にチャレンジ - DOM / Events and user interactions
JSchallenger にチャレンジ - DOM / DOM manipulation
JSchallenger にチャレンジ - DOM / DOM fundamentals
JSchallenger にチャレンジ - DOM / Recursive functions
Sum two numbers
Write a function that takes two numbers (a and b) as argument
Sum a and b
Return the result
回答
function myFunction(a, b) {
return a + b;
}
Comparison operators, strict equality
Write a function that takes two values, say a and b, as arguments
Return true if the two values are equal and of the same type
回答
function myFunction(a, b) {
return a === b;
}
関連内容
なんとなく厳密等価を使っていたら、理由を振り返るいいチャンス。
ついでに JavaScript の歴史も学んでしまおう。
Get type of value
Write a function that takes a value as argument
Return the type of the value
回答
function myFunction(a) {
return typeof(a);
}
関連内容
Get nth character of string
Write a function that takes a string (a) and a number (n) as argument
Return the nth character of 'a'
回答
function myFunction(a, n) {
return a[n-1]
}
Remove first n characters of string
Write a function that takes a string (a) as argument
Remove the first 3 characters of a
Return the result
回答
function myFunction(a) {
return a.slice(3);
}
関連項目
Get last n characters of string
Write a function that takes a string as argument
Extract the last 3 characters from the string
Return the result
回答
function myFunction(str) {
return str.slice(-3);
}
関連項目
Get first n characters of string
Write a function that takes a string (a) as argument
Get the first 3 characters of a
Return the result
回答
function myFunction(a) {
return a.slice(0,3);
}
関連項目
Extract first half of string
Write a function that takes a string (a) as argument
Extract the first half a
Return the result
回答
function myFunction(a) {
return a.slice(0, a.length / 2);
}
関連項目
Remove last n characters of string
Write a function that takes a string (a) as argument
Remove the last 3 characters of a
Return the result
回答
function myFunction(a) {
return a.slice(0,-3);
}
関連項目
Return the percentage of a number
Write a function that takes two numbers (a and b) as argument
Return b percent of a
回答
function myFunction(a, b) {
return b / 100 * a;
}
Basic JavaScript math operators
Write a function that takes 6 values (a,b,c,d,e,f) as arguments
Sum a and b
Then substract by c
Then multiply by d and divide by e
Finally raise to the power of f and return the result
Tipp: mind the order
回答
function myFunction(a, b, c, d, e, f) {
return (((a + b) - c) * d / e) ** f;
}
関連項目
Check if a number is even
Write a function that takes a number as argument
If the number is even, return true
Otherwise, return false
回答
function myFunction(a) {
return a % 2 === 0;
}
Check if a number is a whole number
Write a function that takes a number (a) as argument
If a is a whole number (has no decimal place), return true
Otherwise, return false
回答
function myFunction(a) {
return Number.isInteger(a);
}
関連項目
Multiplication, division, and comparison operators
Write a function that takes two numbers (a and b) as arguments
If a is smaller than b, divide a by b
Otherwise, multiply both numbers
Return the resulting value
回答
function myFunction(a, b) {
return a < b ? a / b : a * b;
}
関連項目
Check whether a string contains another string and concatenate
Write a function that takes two strings (a and b) as arguments
If a contains b, append b to the beginning of a
If not, append it to the end
Return the concatenation
回答
function myFunction(a, b) {
return a.includes(b) ? b + a : a + b;
}
関連項目
Round a number to 2 decimal places
Write a function that takes a number (a) as argument
Round a to the 2nd digit after the comma
Return the rounded number
回答
function myFunction(a) {
return parseFloat(a.toFixed(2),10);
}
関連項目
Split a number into its digits
Write a function that takes a number (a) as argument
Split a into its individual digits and return them in an array
Tipp: you might want to change the type of the number for the splitting
回答
function myFunction(a) {
return a
.toString()
.split('')
.map((str) => parseInt(str, 10));
}
関連項目
Clear up the chaos behind these strings
It seems like something happened to these strings
Can you figure out how to clear up the chaos?
Write a function that joins these strings together such that they form the following words:
'Javascript', 'Countryside', and 'Downtown'
You might want to apply basic JS string methods such as replace(), split(), slice() etc
回答
function myFunction(a, b) {
const upperFirstA =
a.replace('%', '')
.split('')
.at(0)
.toUpperCase();
const validateA =
upperFirstA +
a.replace('%', '')
.slice(1);
const validateB =
b.replace('%', '')
.split('')
.reverse()
.join('');
return validateA + validateB;
}
関連項目
- String.prototype.replace() - JavaScript - MDN Web Docs
- String.prototype.split() - JavaScript - MDN Web Docs
- Array.prototype.at() - JavaScript - MDN Web Docs
- String.prototype.toUpperCase() - JavaScript - MDN Web Docs
- String.prototype.slice() - JavaScript - MDN Web Docs
- Array.prototype.join() - JavaScript - MDN Web Docs
Return the next higher prime number
This challenge is a little bit more complex
Write a function that takes a number (a) as argument
If a is prime, return a
If not, return the next higher prime number
回答
function myFunction(a) {
const isPrime = (n) => {
if (n === 2) return true;
for (let i=2; i<n; i++) {
if (n % i === 0) return false;
}
return true;
}
while(!isPrime(a)) a++;
return a;
}
解説
素数の判定にあたっては「エラトステネスの篩」というアルゴリズムを用います。聞いたことのない方は是非調べてみてください。
Find next higher natural number that is divisble by y
Write a function that takes two numbers, say x and y, as arguments
Check if x is divisible by y
If yes, return x
If not, return the next higher natural number that is divisible by y
回答
function myFunction(x, y) {
while (x % y !== 0) x++
return x
}
Insert character after every n characters (backwards)
Write a function that takes two strings (a and b) as arguments
Beginning at the end of 'a', insert 'b' after every 3rd character of 'a'
Return the resulting string
function myFunction(a, b) {
return Array.from(a)
.reverse()
.reduce((acc, cur, i) =>
i % 3 === 0
? cur + b + acc
: cur + acc,
);
}
関連項目
Find the correct word by incrementing letters in alphabet
Write a function that takes a string as argument
As it is, the string has no meaning
Increment each letter to the next letter in the alphabet
Return the correct word
回答
function myFunction(str) {
const [...alphabet] = 'abcdefghijklmnopqrstuvwxyz';
const [...splitStr] = str;
const getNextStr = (current) =>
current === 'z'
? 'a'
: alphabet[alphabet.indexOf(current) + 1];
return splitStr
.map((current) => getNextStr(current))
.join('');
}
関連項目
Find the position of one string in another
Write a function that takes a string as argument
The string contains the substring 'is'
Return the index of 'is'
回答
function myFunction(a) {
return a.indexOf('is');
}