Code worked yesterday but not today. Why?

Hello,

Yesterday I was working on a project that used a random number generator. I was able to generate numbers consistently and try to debug other parts of the program. Today the number generator doesn’t work. I have went back through the code, and used previous versions of the generator that worked before, but nothing is showing up when I console.log the generator. Have any ideas why this is happening?

We would have to see your code to help you find the problem.

function getQuote() {
return Math.floor(Math.random() * Math.floor(myArray.length));}

That’s going to return a number, not a quote, but as long as myArray has elements it will return a random number.

no need to use Math.floor here

It was a part of a larger function at one time, and I took everything else out to troubleshoot the problem.

I have an object oriented array and I was unable to pull the quote or the author out of it. Using the random number generator I was able to get the object though

Also, it looks like it’s working for you

@Hello_Forum,

This code should work everywhere

const myArray =  ['first', 'second', 'third'];

function getQuote(){
  return Math.floor(Math.random() * myArray.length);
}

const myQuote = myArray[getQuote()];
const output = myQuote==null?"Error: the quote library is empty!":myQuote;

console.log(output);

It is great that you know how to solve this, but instead of posting your solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

People often want help figuring out a problem rather than having it solved for them.

Thank you for understanding.

1 Like

It’s okay in this instance, but I do agree with @ArielLeslie, but it’s always nice to fine a quick solution, lol. I had that as my original framework and it didn’t work @thetradecoder. It didn’t work (I’m guessing because it’s a nested array), but thanks for the help :slightly_smiling_face:

this is what I am working on:
Random Quote Machine

I can get the object, but I can’t get the quote within the object

I got this far, but when I run the code it gives me different quotes and authors, not the same. I thought the getQuote function would stay as a static number throughout the rest of the program. Any idea’s on how to fix it?

function getQuote() {
  let num = Math.floor(Math.random() * myArray.length);

  let obj = myArray[num];
  
  
  return obj;
}

function displayQuote() {
  let random = getQuote();
  
  let text = random.quote;
  
  
  
  return text;
}

function displayAuthor () {
  let random = getQuote();
  
  
  
  let anon = random.author;
  
  return anon;
}

@Hello_Forum, random is always random, it is not fixed, every time it’s called it gives a different number. So it should not be called more than one time for one family

Every time you call the function, it runs again. If you want to get a single random number and reuse it, you can store the result in a variable.

1 Like

I got it working properly. Thanks @thetradecoder and @ArielLeslie :smile:

Congratulations! Happy coding.