Home > Mobile >  I'm trying to print the results of my arrays to the console, but I can't access the functi
I'm trying to print the results of my arrays to the console, but I can't access the functi

Time:11-22

I'm trying to print the results of my arrays to the console, but I can't access the functions inside the class. what can we do.

This is the result I want. But arranged according to oop principles

let arrya = [];
let arryb = [];

for (let i = 0; i <= 9; i  ) {
  function crad_a(max) {
    a = Math.floor(Math.random() * max);
    return a;
  }
  // console.log(crad_a(10));

  function crad_b(max) {
    b = Math.floor(Math.random() * max);
    return b;
  }
  // console.log(crad_b(10));
  i  ;

  arrya.push(crad_a(10));
  arryb.push(crad_b(10));
}
console.log(arrya);
console.log(arryb);

for (let j = 0; j <= arrya.length; j  ) {
  if (arrya[j] != arryb[j]) {
    if (arrya[j] > arryb[j]) {
      console.log("a"   " "   "win"   " "   " the game");
    } else {
      console.log("b"   " "   "win"   " "   "the game");
    }
  }
}

This is what I tried and got stuck

class card_number_one {
  constructor() {
    this.card_one = [];
    this.card_tow = [];
  }
  
  crad_a(max) {
    for (let d = 0; d <= 9; d  ) {
      a = Math.floor(Math.random() * max);
      return a;
    }
    d  ;
    this.card_one.push(crad_a(10));
  }
  
  crad_b(max) {
    for (let i = 0; i <= 9; i  ) {
      b = Math.floor(Math.random() * max);
      return b;
    }
    i  ;
    this.card_tow.push(crad_b(10));
  }
}

console.log(this.card_one); /// Not working for me

CodePudding user response:

Inside your console.log() call the this does not refer to the class (it is outline the class).

You could create an instance of the class, and then log the property of the instance, something like this:

class card_number_one {
  constructor() {
    this.card_one = [];
    this.card_tow = [];
  }
  crad_a(max) {
    for (let d = 0; d <= 9; d  ) {
      a = Math.floor(Math.random() * max);
      return a;
    }
    d  ;
    this.card_one.push(crad_a(10));
  }
  crad_b(max) {
    for (let i = 0; i <= 9; i  ) {
      b = Math.floor(Math.random() * max);
      return b;
    }
    i  ;
    this.card_tow.push(crad_b(10));
  }
}

// Create an instance of the class and save it in the "exampleCard" variable
const exampleCard = new card_number_one();

// Console log the property "card_one" of the "exampleCard" instance of your class
console.log(exampleCard.card_one);

There is some good information about JS classes here on MDN

CodePudding user response:

I would recommend reading this here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes it should help you solve this one. What I notice however;

Classes must be defined before they can be constructed

 // Defining your class
 class Card {
    // stuff
 }
         
 // Constructing your class
 let foo = new Card();

Then afterwards you can do a log of foo:

console.log(foo)
  1. Its good practice to use pascal notation over camel notation when defining your class, so class Card instead of class cardName.

  2. I am not 100% sure what your method is doing but try to keep your code DRY so instead of having 2 for loops, you can have one method which you can reuse.

So in your case that would be: ps:// I also changed your method to a getter, you can read more avout getters here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get

class Card {
  constructor(array, max) {
    this.array = array;
    this.max = max;
  }
  get maxNumber(){
    for (let i = 0; i <= this.max; i  ) {
      return Math.floor(Math.random() * this.max);
    }
  }

}

let array1 = [1,2,3,4];
let array2 = [5,6,7,8];

let card1 = new Card(array1, 10)
let card2 = new Card(array2, 20)

console.log(card1.maxNumber); // Returns 7
console.log(card2.maxNumber); // Returns 6

let difference = card1.maxNumber - card2.maxNumber // Assuming the number doesn't have to be greater than 0.

// Print your message
console.log(`Your game score is ${difference}, good job!`);

So now you have a reusable class, which you can use to load as many cards as you want :)

  • Related