如何将课程内容保存到txt文件

时间:2019-05-17 09:06:26

标签: javascript node.js

我不知道如何从类中复制/保存内容,确切地说是从console.log复制到txt文件。 我是该领域的初学者,我正在尽力而为,但我失败了。提前致谢。 我想将console.log的输出保存到txt文件(这是upack卡座)

class Card {
  constructor(suit, value) {
    this.suit = suit;
    this.value = value;
  }
}

class Deck {
  constructor() {
    this.deck = [];
  }

  createDeck(suits, values) {
    for (let suit of suits) {
      for (let value of values) {
        this.deck.push(new Card(suit, value));
      }
    }
    return this.deck;
  }

  shuffle() {
    let counter = this.deck.length,
      temp,
      i;

    while (counter) {
      i = Math.floor(Math.random() * counter--);
      temp = this.deck[counter];
      this.deck[counter] = this.deck[i];
      this.deck[i] = temp;
    }
    return this.deck;
  }

  deal() {
    let hand = [];
    while (hand.length < 2) {
      hand.push(this.deck.pop());
    }
    return hand;
  }
}

let suits = ["Spades", "Hearts", "Diamonds", "Clubs"];
let values = ["Ace", "Jack", "Queen", "King", 2, 3, 4, 5, 6, 7, 8, 9, 10];
let deck = new Deck();


deck.createDeck(suits, values);
deck.shuffle();
console.log(deck.deal());

我从console.log(deck.deal());输出的文本是

 Card { suit: 'Hearts', value: 3 },
 Card { suit: 'Spades', value: 9 } 

我想使用那部分代码或其他内容将其复制到txt文件中

const fs = require('fs');
fs.writeFile("exampleFileTXT", "Hey there!", function(err) {
    if(err) {
        return console.log(err);
    }
    console.log("The file was saved!");
}); 

0 个答案:

没有答案