How do I ensure the input is between 1 and 5 for the addRating() method?

Viewed 102

Please see the section in bold below. I would really appreciate it if someone could help me. How do I ensure the input is between 1 and 5 for the addRating() method? Perhaps there is some way that I can stop the code from continuing/processing the average rating, if one of the ratings is lower than 1 and higher than 5.

class Media {

  constructor(title) {
    this._title = title;
    this._isCheckedOut = false;
    this._ratings = [];
  }
  
  get title() {
    return this._title;
  }

  get isCheckedOut() {
    return this._isCheckedOut;
  }

  get ratings() {
    return this._ratings;
  }

  set isCheckedOut(status) {
    this._isCheckedOut = status;
  }

  toggleCheckOutStatus() {
    this.isCheckedOut = !this.isCheckedOut;
  }

  getAverageRating() {
    let ratingsSum = this.ratings.reduce((accumulator, currentRating) =>
    accumulator + currentRating);
    return ratingsSum/this.ratings.length;
  }

  // Here is the mentioned function
  // look inside addRating()
  addRating(newRating) {
    if (newRating < 1 || newRating > 5) {
      console.log("Invalid Input. Please enter an integer between 1- 
  5.");
      } else {
      this.ratings.push(newRating);
    }
  }
}

class Book extends Media {
  constructor(author, title, pages) {
    super(title);
    this._author = author;
    this._pages = pages;
  }
  get author() {
    return this._author;
  }
   get pages() {
    return this._pages;
  }
}

class Movie extends Media {
  constructor(director, title, runTime) {
    super(title);
    this._director = director;
    this._runTime = runTime;
  }
  get director() {
    return this._director;
  }
  get runTime() {
    return this._runTime;
  }
}

const historyOfEverything = new Book('Bill Bryson', 'A Short History of Nearly Everything', 544);

historyOfEverything.toggleCheckOutStatus();

console.log(historyOfEverything.isCheckedOut);

historyOfEverything.addRating(4);
historyOfEverything.addRating(5);
historyOfEverything.addRating(5);

console.log(historyOfEverything.getAverageRating());

const speed = new Movie('Jan de Bont', 'Speed', 116);

speed.toggleCheckOutStatus();

console.log(speed.isCheckedOut);

speed.addRating(1);
speed.addRating(1);
speed.addRating(5);

console.log(speed.getAverageRating());
3 Answers

You can throw an Error like this.

throw new Error("messages");

Also I would recommend that you describe the range of valid inputs with AND and not with OR value < 5 && value > 1

function addRating(value) {
  if (value < 5 && value > 1) {
    console.log("Everything is okay");
  } else {
    throw new Error("Invalid Input. Please enter an integer between 1-5.");
  }
}

addRating(12);

You could go old school with a prompt and put that in while loop. In this answer, the while loop is ignored if the passed value is correct. I commented out your push for my answer for testing.

function addRating(newRating) {
   while(newRating < 1 || newRating > 5){
     newRating = Number(prompt("Invalid Input. Please enter an integer between 1-5."));
  }
  
  //this.ratings.push(newRating);
}


addRating(41);

Related