3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【教材】JavaScriptの教材を作ってみました2

Last updated at Posted at 2020-12-16

※当方駆け出しエンジニアのため、間違っていることも多々あると思いますので、ご了承ください。また、間違いに気付いた方はご一報いただけると幸いです。

半人前のくせに、偉そうに教材を作ってみました。

###学べる技術

  • 繰り返し構文
  • 多次元配列

##問題1

console.table(array);

 //[ [1], [2, 3], [4, 5, 6], [7, 8, 9, 10] ]

この様な結果が返る配列arrayを繰り返し構文を用いて作ってください。(見やすいように半角スペースを入れてますが不要です)

##問題2

let x = ["a", "b", "c"];
let y = ["d", "e", "f"];
let z = ["g", "h", "i"];

上記の様な配列があるとします。

下記の様な結果が返る配列X,Y,Zを繰り返し構文を用いて作ってください。

console.log(X);
console.log(Y);
console.log(Z);

//["a", "d", "g"];
//["b", "e", "h"];
//["c", "f", "i"];

##問題1 回答例

let array = [];
let inArray = [];

let num = 1;
for (let i = 1; i < 5; i++) {
  for (let g = 0; g < i; g++) {
    inArray.push(num);
    num++;
  }
  array.push(inArray);
  inArray = [];
}
console.table(array);

##問題2 回答例

let x = ["a", "b", "c"];
let y = ["d", "e", "f"];
let z = ["g", "h", "i"];

let X = [], Y = [], Z = [];

const array = [x, y, z];
const array2 = [X, Y, Z];

for (let i = 0; i < 3; i++) {
  for (let g = 0; g < 3; g++) {
    array2[i].push(array[g][i]);
  }
}
console.log(X);
console.log(Y);
console.log(Z);

※あくまで駆け出し半人前エンジニアが作った回答となります。あまり鵜呑みにしないで下さい。

3
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?