Quiz on Ice Cream Flavors with Javascript - Testing Your Knowledge!

Looking for some assistance with the quiz below.

/*
 * Challenge: Ice Cream Order (3-6)
 *
 * Create a single if statement to output the message:
 * 
 * "I'd like two scoops of __________ ice cream in a __________ with __________."
 * 
 * ...only under these conditions:
 *   - flavor is "vanilla" or "chocolate"
 *   - vessel is "cone" or "bowl"
 *   - toppings is "sprinkles" or "peanuts"
 *
 * This test focuses on the if statement and boolean operators. 
 * The exact output string doesn't have to match.
 */

// Adjust the values of `flavor`, `vessel`, and `toppings` to test your code
var flavor = "strawberry";
var vessel = "hand";
var toppings = "cookies";

// Insert your code here
if ((flavor === "vanilla" || flavor === "chocolate") && (vessel === "cone" || vessel === "bowl") && (toppings === "sprinkles" || toppings === "peanuts")) {
    console.log("I'd like two scoops of " + flavor + " ice cream in a " + vessel + " with " + toppings + ".");
}
else {
    
}

I'm struggling to figure out how to display the statement only when the specified conditions are met. When I use undefined variables, they still show in the console log. I'm hoping to avoid that. I hope this explanation makes sense. Any guidance would be highly appreciated. Thank you!

Answer №1

Your current condition check, === 'str1' || 'str2, is incorrect.

To improve clarity and understanding, it's better to explicitly specify all the conditions in your code.

if ((flavor === "vanilla" || flavor === "chocolate") && (vessel === "cone" || vessel === "bowl") && (toppings === "prinkles" || toppings === "peanuts")){

Answer №2

Here is a suggestion for you to try:

let flavor = "strawberry";
let vessel = "cone";
let toppings = "whipped cream";

if (
    ["vanilla", "strawberry"].includes(flavor)
    && ["cone", "cup"].includes(vessel)
    && ["sprinkles", "whipped cream"].includes(toppings)) {

       console.log("I'd like one scoop of %s ice cream in a %s with %s topping.", 
                    flavor, vessel, toppings);
}

Check out this Codepen demo


For more information on the includes() method, visit MDN

Answer №3

let chosenFlavor = "chocolate"||"vanilla";
let chosenVessel = "bowl"||"cone";
let chosenToppings = "sprinkles"||"peanuts";

if((chosenFlavor === "chocolate" || chosenFlavor === "vanilla") && (chosenVessel === "bowl" || chosenVessel === "cone") && (chosenToppings === "peanuts" || chosenToppings === "sprinkles")){
    console.log("I feel like having two scoops of " + chosenFlavor + " ice cream in a " + chosenVessel + " topped with " + chosenToppings);
}


Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Double triggering of $stateChangeStart event in Angular UI Router (extras)

I'm having trouble understanding why the addition of Angular UI Router Extras causes the $stateChangeStart event to trigger twice (on init!) in a situation where I have some asynchronous ajax check and call e.preventDefault(). Here is a short example ...

What is the best way to trigger a new api call after the previous one has successfully completed?

I'm just starting to learn about Angular and RxJS, and I have a specific scenario that I'm struggling with. I need to make a new API call after a previous one is successfully resolved within the Angular/RxJS context, but I'm not sure how to ...

NextAuth.js in conjunction with nextjs version 13 presents a unique challenge involving a custom login page redirection loop when using Middleware - specifically a

I am encountering an issue with NextAuth.js in Nextjs version 13 while utilizing a custom login page. Each time I attempt to access /auth/signin, it first redirects to /login, and then loops back to /auth/signin, resulting in a redirection loop. This probl ...

Is there a more efficient method for coding this switch/case in TypeScript?

I'm working on a basic weather application using Angular and I wanted some advice on selecting the appropriate image based on different weather conditions. Do you have any suggestions on improving this process? enum WeatherCodition { Thunderstorm ...

The deployment of the Twilio React plugin encountered an issue: Error message stating that the specified resource '/Configuration' could

Currently following this tutorial: I am using a Twilio trial account and attempting to deploy a React plugin. When I run npm run deploy, I encounter the following error message: Error occurred when trying to get Configuration with status code 404, disp ...

The installed NPM package does not contain the necessary TypeScript compiled JS files and declaration files

I have recently released a TypeScript library on NPM. The GitHub repository's dist (Link to Repository Folder) directory includes all compiled JavaScript and d.ts files. However, after running npm i <my_package>, the resulting module contains on ...

The useMutation function trapped in an endless loop

I've been encountering an issue while running a query to save an entity in the database using usemutation. The saveVisa() mutation seems to be stuck in an infinite loop, creating the same element multiple times without any clear reason. import {React, ...

How to retrieve and modify JSON data in Node.js using Stream with both Get and Post methods

Exploring Node.js for the first time, I'm currently tackling a project where I aim to utilize Request for streaming data from one endpoint to another. The objective is to leverage Request for extracting and posting an altered JSON body through a pipe ...

preserve functionality when switching between pages

I have created two simple functions that can change the background color of the body. <script type="text/javascript"> function switchBackground(color){ document.body.bgColor=color; } </script> I am using links with onclick to execute thes ...

Inject the ng-repeat variable into a personalized directive

When working with an ng-repeat and a custom directive, I am facing the challenge of passing the "item" variable from ng-repeat to the directive. Here is an example code snippet illustrating this situation: <li ng-repeat="item in list"> <div c ...

Tips for limiting decimals to two digits in AngularJS

In my AngularJS math operations, I am encountering an issue where the percentage/VAT price is displaying as 0.35000000000000003 instead of just 0.35. How can I trim the decimals after 2 digits? 0.35000000000000003 - Is there a way to limit this number to ...

What are the best techniques for streamlining nested objects with Zod.js?

As a newcomer to zod.js, I have found that the DataSchema function is extremely helpful in verifying API data types and simplifying the API response easily. However, I'm curious if there is a way to streamline the data transformation process for myEx ...

Grunt-jsmart does not generate any files and lacks error reporting capabilities

After dabbling with grunt and jsmart, I stumbled upon the grunt-jsmart plugin which seemed to be a perfect fit for my needs. However, despite trying various examples, I can't seem to get it to work within my grunt workflow. It's not generating th ...

Detecting Unflushed Requests in Jasmine and AngularJS

I'm encountering some issues passing certain tests after implementing $httpBackend.verifyNoOustandingRequest(). Interestingly, excluding this from my afterEach function allows the tests to pass successfully. However, including it causes all tests to ...

Issue with nextElementSibling not applying CSS style

My current issue revolves around a button that is designed to open or close a collapsible div. The HTML structure of this element looks like the following: <div class='outer-collapsible'> <button type='button' class='col ...

Creating a Timeout Function for Mobile Browsers in JavaScript/PHP

Currently, I am developing a mobile-based web application that heavily relies on AJAX and Javascript. The process involves users logging in through a login page, sending the data via post to the main page where it undergoes a mySQL query for validation. If ...

Initiate search on new page with form action

I am encountering an issue with my search field setup in index.php. It seems that when I navigate through the pagination links to pagination.php, the form data is getting reset. <?php include('db.php'); if (isset($_GET["page"])) { $pa ...

React Array Not Behaving Properly When Checkbox Unchecked and Item Removed

When using my React Table, I encountered an issue with checkboxes. Each time I check a box, I aim to add the corresponding Id to an empty array and remove it when unchecked. However, the current implementation is not functioning as expected. On the first c ...

Dispatching actions in `componentDidMount` is restricted in Redux

Update at the bottom of post I've created a React container component called AppContainer, which checks if the user is authenticated. If the user is authenticated, it renders the app's routes, header, and content. If not, it displays a Login com ...

Regular expression that matches a string with optional leading and trailing spaces and a hyphen in the middle

I need help creating a JavaScript regex that can match strings like the examples provided. The string may have whitespace at the front, back, or both but not within the string itself. Examples: 'Z5LW-KRT2' MATCH ' Z5LW-krt2' ...