Issue with celsius to fahrenheit project

Hi!

My mind goes completely blank when I get to the conditionals project with Celsius and Fahrenheit. Any suggestions on something to review? The farthest I got with the code is:

let raceNumber = Math.floor(Math.random() * 1000);
let runnerRegistered = true;
let runnerAge = 29;

switch()

I would suggest the conditionals material, and perhaps take notes as you go? :slight_smile:

Hey @uhlnoor,

You’ve mentioned you’ve had some problems understanding conditionals. Coding is a difficult subject and we’ve all had times when we couldn’t get our heads’ around a concept.

Let’s look through a basic example of conditionals that is unrelated to the the problem you are trying to solve.

(If I can remember anything from my computing diploma, we will look at how we could write it in a natural way, that is, one that we understand as opposed to computers.)

We have the following scenario:

I have asked you to purchase me some groceries and I provide you with an item and an alternative. Below is the list:

One jar of Marmite or if they don’t have it forget about this item;
One Loaf of Bread or if they don’t have it forget about this item ;
One block of cheddar cheese or if they don’t have it forget about this item; and
One pint of Milk or if they don’t have it forget about this item.

We can pretty much understand that as humans as we’re taught to understand instructions, that is, we’re clever; on the other hand, computers are stupid. They aren’t as flexible as us and cannot really work their way around resolving a problem. They have a tantrum and misbehave if they don’t get their own way.

Now we will take this list into more of a structured way.

IF THE SUPERMARKET HAS MARMITE IN STOCK PURCHASE ONE JAR OF MARMITE
  
  ELSE DO NOT PURCHASE

IF THE THE SUPERMARKET HAS LOAVES OF BREAD IN STOCK PURCHASE ONE LOAF OF BREAD
  
  ELSE DO NOT PURCHASE

IF THE SUPERMARKET HAS ONE BLOCK OF CHEDDAR CHEESE IN STOCK PURCHASE IT 
 
  ELSE DO NOT PURCHASE

IF THE SUPERMARKET HAS PINTS OF MILK IN STOCK PURCHASE ONE PINT
   
  ELSE DO NOT PURCHASE

The above is now starting to look more code friendly, and it can be more easily seen how we translate a real-world problem, in this case, a shopping list into a more structured way for computers to understand.

Now we can transform this into code, in this case JavaScript:


//Multidimensional array storing the item and whether it is in stock or not
const supermarketStock = [
   ['marmite', true],
   ['bread',  false],
   ['cheese', true],
   ['milk', false]
];

const shoppingList = ['marmite', 'bread', 'cheese', 'milk'];

//This function takes an array of shopping list items and an array of stock items
const purchaseShopping = undefined => {
   //This first if statement checks that the items match and whether they are in stock or not by firstly accessing the second value of first item in the array in the supermarketStock array 
   if (supermarketStock[0][0] === shoppingList[0] && supermarketStock[0][1] === true) {
     // It returns true if the first shopping list item matches the first in stock item and it is in stock
      return true;
  } else if (supermarketStock[1][0] === shoppingList[0] && supermarketStock[1][1] === true) {
     return true;
  }  else if(supermarketStock[2][0] === shoppingList[0] && supermarketStock[1][1] === true) {
     return true;
  } else {
    // It returns false if the first shopping list item doesn't match or it is not in stock and will continue in this fashion for the rest of the function
    return false;
 }
};

This is not how it should be coded, and, frankly is bad practice, but it’s a demonstration.

Hopefully this is a little easier for you to visualise how we get from a problem to a solution in code and more importantly how conditionals work.

Any questions, just ask.
Best wishes,

Richard.

P.S I’ve not tested my code and there may be some errors.

EDIT: I’ve checked and my logic is flawed. This does show how easy it is to make mistakes; but we learn from them!

1 Like

I might go with something like this:

const store = { //use an object to represent the store
  bread: 0,
  cheese: 6,
  marmite: 1,
  milk: 0
};

const goShopping = items => {
  const basket = []; //basket will 'filled' with successfully purchased items
  for(let item of items) { //iterate through the items array
    if(store[item]) { //see if the store has the item
      console.log(`Successfully purchased ${item}.`); //print success message
      basket.push(item); //add the item to the basket
      store[item] -= 1; //subtract 1 from the store's inventory of the item
    } else {
      console.log(`Sorry, but ${item} is out of stock.`); //print failure message if the store doesn't have the item
    }
  }
  return basket; //return the array of successfully purchased items
}

const shopList = ['marmite', 'milk', 'cheese', 'bread']; //list of items to purchase

console.log(store); //show store prior to shopping
const itemsPurchased = goShopping(shopList); //assign the return value of goShopping() to a variable
console.log(`I purchased the following items: ${itemsPurchased.join(', ')}.`); //print a message with the items purchased
console.log(store); //show store after shopping

Output:

{ bread: 0, cheese: 6, marmite: 1, milk: 0 }
Successfully purchased marmite.
Sorry, but milk is out of stock.
Successfully purchased cheese.
Sorry, but bread is out of stock.
I purchased the following items: marmite, cheese.
{ bread: 0, cheese: 5, marmite: 0, milk: 0 }

1 Like

@uhlnoor, Are you working on Celsius and Fahrenheit or the Raceday project? From the code you posted, it looks like Raceday.

Wow man, this is true love for the community! Can I give this guy some props or something? Thank you for breaking it down like that. You really helped me understand the material better. I came into this course with an understanding of Java and so level understanding of if’s, else’s, if else’s and then loops. But I was having issues putting it all together here, so you really made it so much easier man. Thank you!

1 Like

I’m on the Raceday project, sorry. I didn’t know Celsius and Fahrenheit are another project.@midlindner

So I followed the ternary operator up until “Late adults run at 11:00 am”, can I use the following technique to do that part? Especially since there’s not an actual way to do if else statements:

Also, what does it mean when it says “function example(…) and the return

I copy pasted this image from: Mozilla MDN

@midlindner, that is a perfect solution, and one I would have chosen if I’d been more widely awake :wink:

1 Like

Hello @uhlnoor,

I’m pleased my above explanation helped and, I’m also pleased that it was refactored into something more coherent.

Let us get back to your question above:

Firstly, you ask what does it mean when it says:

function example(...) {

}

That’s just a function declaration:

function     example      (   ...   )
[keyword] [functionName] [parameters]

Secondly, there is a way to do if else statements using ternary operators and your above screenshot exemplifies this.

Let us make this more practical:

This function is functionally equivalent to

function myExampleFunction(string) {
	return  (string === true) ? true
  		    :(string === false) ? false
  		    : "The string is neither true nor false";
}
     

This function:

function myExampleFunction(string) {
   if (string === true) {
      return true;
   } else if (string === false) {
      return false;
  } else {
      return "The string is neither true nor false";
  }
}

The output of both is the same so:

console.log(myExampleFunction(true));

would log the boolean value of true to the console;

console.log(myExampleFunction(false));

would log false to the console; and

console.log("true");

would log "This is neither true nor false" because it is a string value
and not a boolean data type. 

Returning to the ternary operator it follows this vein:

//Normal functional declaration 
function myExampleFunction(string) {
 // specify a return type in this case as we only want to use the logic for
// this function and return the values
//this follows (conditional statement) ? return value
// in plain English if the conditional statement evaluates to what we want
// it to then return a truthty value
   return (string === true) ? true //functionally equivalent to an if statement
              : (string === false) ? false // functionally equivalent to an else-if statement
              : string; // functionally equivalent to return string;
}

Hope this helps and get back to use if it doesn’t. This is a wealth of experience.
Best wishes,

Richard.