質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

Q&A

解決済

3回答

782閲覧

JS クラスの動作確認 console.logでうまく表示されない

nahi123

総合スコア14

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

0グッド

0クリップ

投稿2019/11/13 04:55

2つの配列を比較して、同じ値がある場合は、一つだけを配列に入る関数です。
正常に動作するかを確認するために最後にconsole.logで表示しようとしたのですが、結果が、
Set {
has: [Function],
values: [Function],
add: [Function],
remove: [Function],
size: [Function],
union: [Function] }
になってしまいます。

function Set() { // the var collection will hold the set var collection = []; // this method will check for the presence of an element and return true or false this.has = function(element) { return (collection.indexOf(element) !== -1); }; // this method will return all the values in the set this.values = function() { return collection; }; // this method will add an element to the set this.add = function(element) { if(!this.has(element)){ collection.push(element); return true; } return false; }; // this method will remove an element from a set this.remove = function(element) { if(this.has(element)){ var index = collection.indexOf(element); collection.splice(index,1); return true; } return false; }; // this method will return the size of the set this.size = function() { return collection.length; }; // change code below this line this.union = function(arr) { var newSet = new Set() this.values().forEach(el => newSet.add(el)) arr.values().forEach(el => newSet.add(el)) return newSet; } // change code above this line } var setA = new Set(); var setB = new Set(); setA.add('a') setA.add('b') setB.add('b') setB.add('c') setB.add('a') setB.add('a') console.log(setA.union(setB)); //期待するのは、["a", "b", "c"]

素人なので、よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答3

0

ベストアンサー

組み込みのSetがあるので、他の回答者さんがおっしゃるとおり、そちらを使うのが定石です。
組み込みのものは速くて安心です。
こちらでは、質問者さんが上書きされているSetの話をします。

結論から言えば、意図した動作はしていると思います。
最後を

JavaScript

1console.log(setA.union(setB).values());

にすればいいのではないでしょうか。

prototypeやclassを使う実装の方がいいかもしれませんが、そこは後で調べてください。

投稿2019/11/13 06:39

dameo

総合スコア943

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

nahi123

2019/11/13 15:35

ありがとうございます。動作しました。 Setクラスを作る勉強してたので、質問しました。
guest

0

今のままではSetクラスのcollectionにアクセスできないと思います。
まずはcollectionの箇所をthis.collectionに書き換えましょう。

js

1function Set() { 2 // the var collection will hold the set 3 this.collection = []; 4 // this method will check for the presence of an element and return true or false 5 this.has = function(element) { 6 return (this.collection.indexOf(element) !== -1); 7 }; 8 // this method will return all the values in the set 9 this.values = function() { 10 return this.collection; 11 }; 12 // this method will add an element to the set 13 this.add = function(element) { 14 if(!this.has(element)){ 15 this.collection.push(element); 16 return true; 17 } 18 return false; 19 }; 20 // this method will remove an element from a set 21 this.remove = function(element) { 22 if(this.has(element)){ 23 var index = this.collection.indexOf(element); 24 this.collection.splice(index,1); 25 return true; 26 } 27 return false; 28 }; 29 // this method will return the size of the set 30 this.size = function() { 31 return this.collection.length; 32 }; 33 // change code below this line 34 35 this.union = function(arr) { 36 var newSet = new Set() 37 this.values().forEach(el => newSet.add(el)) 38 arr.values().forEach(el => newSet.add(el)) 39 return newSet; 40 } 41 42 // change code above this line 43}

それと、unionの戻り値はSetクラスになっているので、newSetのcollectionをlog出力することで期待する結果が得られるかと思います。

js

1console.log(setA.union(setB).collection);

投稿2019/11/13 05:07

KaiShoya

総合スコア551

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

素人なので、よろしくお願いします

javascriptにSetオブジェクトがあるので

javascript

1var setA = new Set(); 2var setB = new Set(); 3 4setA.add('a') 5setA.add('b') 6setB.add('b') 7setB.add('c') 8setB.add('a') 9setB.add('a') 10 11console.log(setA); // ["a","b"] 12console.log(setB); // ["b","c","a"]

となります

投稿2019/11/13 05:02

yambejp

総合スコア114843

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問