はじめに
JSchallenger の Javascript Fundamentals – Javascript Basics のまとめです。
まとめページのトップは、JSchallenger まとめ です。
問題一覧と回答
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;
}
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
substring を使ってしまったが、slice を使うようにした方が応用が利いてよさそう
function myFunction(a) {
return a.substring(3);
}
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
a の b% は?という問題なので、a に b をかけるというのが普通の考え方だと思うのだが、これって日本的な考え方なのか?
まあ、細かい話だが。
function myFunction(a, b) {
return a * b / 100;
}
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
括弧がないだけで同じだけど、それに括弧つけるなら substract も括弧を付けるべきでは?
function myFunction(a, b, c, d, e, f) {
return ((a + b - c) * d / e) ** f;
}
function myFunction(a, b, c, d, e, f) {
return (((a + b - c) * d) / e) ** f;
}
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.indexOf(b) === -1 ? a + b : b + a
}
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
}
How many times does a character occur?
// Write a function that takes two strings (a and b) as arguments
// Return the number of times a occurs in b
これは圧倒的に author’s solution の方がきれいな回答だった(/_\*)
function myFunction(a, b) {
return (b.match(newRegExp(a.replace('?', '\\?'), 'g')) || [] ).length;
}
function myFunction(a, b) {
return b.split(a).length - 1
}
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) {
returnNumber.isInteger(a);
}
function myFunction(a) {
return a - Math.floor(a) === 0
}
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
}
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
toFixed 知らんかった。
function myFunction(a) {
returnMath.round(a * 100) / 100;
}
function myFunction(a) {
returnNumber(a.toFixed(2));
}
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) {
returnString(a).split('').map(s => Number(s));
}
function myFunction( a ) {
const string = a + '';
const strings = string.split('');
return strings.map(digit => Number(digit))
}
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) {
return a[0].toUpperCase() +
a.slice(1).replace('%', '') +
b.replace('%', '').split('').reverse().join('');
}
function myFunction(a, b) {
constfunc = x => x.replace('%','');
const first = func(a);
const second = func(b).split('').reverse().join('');
return first.charAt(0).toUpperCase() + first.slice(1) + second;
}
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 の中に function を作る発想はなかった。
function myFunction(a) {
while (1) {
flag = true;
for (i = 2; i <= Math.sqrt(a); i ++) {
if (a % i === 0) {
flag = false;
break;
}
}
if (flag) return a;
a ++;
}
}
function myFunction( a ) {
function isPrime(num) {
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) returnfalse;
}
return num > 1;
}
let n = a;
while (!isPrime(n)) n++;
return n
}
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) {
return x % y === 0 ? x : (Math.floor(x / y) + 1) * 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 a.replace( /(?=(...)+(?!.))/g, b);
}
function myFunction(a, b) {
let result = [];
let rest = a;
while (rest.length) {
result.push(rest.slice(-3));
rest = rest.slice(0, -3);
}
return result.reverse().join(b);
}
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) {
return str.split('').map(s => String.fromCharCode(s.charCodeAt() + 1)).join('');
}
function myFunction(str) {
const arr = [...str];
const correctedArray = arr.map(e => String.fromCharCode(e.charCodeAt()+1));
return correctedArray.join('');
}
コメント