Getting Things Wrong

Getting Things Wrong

So today, in a rather exciting bit of news, I got something wrong.

And that is exciting because I don't normally get things wrong. And that's not only because I'm good. It's also because I'm afraid to let go of the handrails sometimes. I'm afraid to take risks.

Today I took a risk on my JavaScript, and I got something completely wrong. I'm so glad that this was able to happen lol. 

The biggest thing that I am trying to figure out is: WHERE did I go wrong? In my thinking - because I know where I went wrong in my code (and I have now fixed it since).

So what happened? What did I get wrong?

Magic-8 Ball

Magic 8-Ball is a classic exercise that is used by Codecademy when using 'if'/'else if'/'else' statements. I'm not sure if they use it with all other languages but I would imagine they do too - I have most definitely done it in Python.

Basically, in the JavaScript exercise, I first came up with this bit of code: 

et userName = "Susanna"

userName ? console.log(`Hello, ${userName}!`) : console.log("Hello!");

const userQuestion = "Will I become a Software Engineer?"

console.log(`${userName}, your question is: ${userQuestion}`)

let randomNumber = Math.floor(Math.random() *11)

console.log(randomNumber);

(Omg this is so cool btw I didn't know that I could do this!)
And then this is the correct code that I needed to do this i.e. to product a magic 8 ball with a random answer based on a random number: 

let eightBall = "";

switch(randomNumber) {
  case(0):
    eightBall = "It is certain";
    break;
  case(1):
    eightBall = "It is decidedly so";
    break;
  case(2):
    eightBall = "Reply hazy try again";
    break;
  case(3):
    eightBall =' Cannot predict now';
    break;
  case(4):
    eightBall = 'Do not count on it';
    break;
  case(5):
    eightBall = 'My sources say no';
    break;
  case(6):
    eightBall = 'Outlook not so good';
    break;
  case (7):
    eightBall = 'Signs point to yes';
    break;
  case(8):
    eightBall = 'It is written';
    break;
  case(9):
    eightBall = 'It is meant to be';
    break;
  case(10):
    eightBall = 'Follow your heart';
    break;
}

console.log(eightBall)

console.log(`Your answer, ${userName}, is ${eightBall}`);


So I got there in the end... but only after following the video. 
I was quite pleased with myself that I don't have to follow the whole video or anything... I just muted it and cross-checked the code. 

This is the incorrect code that I put initially

let eightBall = "";

switch(eightBall) {
  case(0):
    console.log("It is certain");
    break;
  case(1):
    console.log("It is decidedly so");
    break;
  case(2):
    console.log('Reply hazy try again');eightBall = "Reply hazy try again");
    break;
  case(3):
    console.log('Cannot predict now');
    break;
  case(4):
    console.log('Do not count on it');
    break;
  case(5):
    console.log('My sources say no');
    break;
  case(6):
    console.log('Outlook not so good');
    break;
  case (7):
    console.log('Signs point to yes');
    break;
  case(8):
    console.log('It is written');
    break;
  case(9):
    console.log('It is meant to be');
    break;
  case(10):
    console.log('Follow your heart');
    break;
}

So what was my thought process and where did I go wrong with it initially?

I made two mistakes initially:
  • I made the variable in the Switch case eightBall and not randomNumber
    • What was I thinking and why did I do that? I don't really understand myself
    • I think, because I thought that I was writing text (i.e. ultimately outputting a string), I thought I had to put in a text-based (i.e. a string) variable as well
    • Why didn't I make the link that once the number was randomised it would FILL THE EMPTY STRING WITH WHATEVER IT NEEDED TO BE?
  • The other mistake I made was:
    • I'd only seen the switch/case example used with console.log before
    • So I'd incorrectly assumed that the example in the project had to be done in the way that I'd seen it done before
    • Instead it was '=' that had to/could be used in this example
    • How could I avoid making this mistake in future please? Thank you.
Just for fun, here is a picture of Venice Beach in LA... I'm listening to a song by Lana del Rey which is a play on words, but you'll have to look it up for yourself... Not something I'm sharing on here 😂😂


Comments

Popular posts from this blog

Hello World

Yosemite

Two Lines of Thought, Makes Me Wonder What I'm Missing