Open24

JSchallenger にチャレンジ - Fundamentals / Basics

cyamycyamy

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

https://www.jschallenger.com/javascript-fundamentals/javascript-basics/comparison-strict-equality

回答

function myFunction(a, b) {
  return a === b;
}

関連内容

なんとなく厳密等価を使っていたら、理由を振り返るいいチャンス。
ついでに JavaScript の歴史も学んでしまおう。

cyamycyamy

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

https://www.jschallenger.com/javascript-fundamentals/javascript-basics/basic-math-operators-javascript

回答

function myFunction(a, b, c, d, e, f) {
  return (((a + b) - c) * d / e) ** f;
}

関連項目

cyamycyamy

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

https://www.jschallenger.com/javascript-fundamentals/javascript-basics/check-if-string-contains-string

回答

function myFunction(a, b) {
  return a.includes(b) ? b + a : a + b;
}

関連項目

cyamycyamy

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));
}

https://www.jschallenger.com/javascript-fundamentals/javascript-basics/split-number-into-digits

関連項目

cyamycyamy

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

https://www.jschallenger.com/javascript-fundamentals/javascript-basics/clean-sort-join-strings

回答

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;
}

関連項目

cyamycyamy

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

https://www.jschallenger.com/javascript-fundamentals/javascript-basics/return-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;
}

解説

素数の判定にあたっては「エラトステネスの篩」というアルゴリズムを用います。聞いたことのない方は是非調べてみてください。

cyamycyamy

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

https://www.jschallenger.com/javascript-fundamentals/javascript-basics/insert-character-after-every-characters-backwards-javascript

function myFunction(a, b) {
  return Array.from(a)
    .reverse()
    .reduce((acc, cur, i) =>
      i % 3 === 0
        ? cur + b + acc
        : cur  + acc,
    );
}

関連項目

cyamycyamy

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

https://www.jschallenger.com/javascript-fundamentals/javascript-basics/incrementing-javascript-letters-alphabet

回答

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('');
}

関連項目