Steps for appending a string to a variable

Currently working on creating a price configurator for a new lighting system within homes using Angular 7. Instead of using TypeScript and sass, I'm coding it in plain JavaScript.

Page 1: The user will choose between a new building or an existing one, along with options for Wi-Fi/wires [z1, z2...]

<label>New Building</label>
<input type="radio" value="z1">
  1. The user then needs to specify the size of the building, which will add different controls on page number 3 [-o1, -o2...]

     <select><option value="-o1">Small</option>...
    
  2. Selection of what to build (lights, aircon, detectors, etc.), potentially adding more controls in step 4 [y8, y9...]

  3. Choose additional controls such as Android devices, TVs, keychains, movement detectors (can be multiple) [-5x2-24x2]
  4. Calculate the total price

I plan to store these values as strings in a variable and then use them to assign prices accordingly. For example, z1-o1-y8-i-5x2-24x2 translates to a new building, small size, lights, On/Off + timing controls, and specific controllers repeated twice each with their respective prices. It's quite complex, and I'm not sure if I'm on the right track. Any guidance on how to accomplish this would be greatly appreciated. Thank you.

Answer №1

To calculate the total price of all options with individual prices, follow these steps:

Begin by creating an object that lists each option along with its corresponding price:

const prices = {
  z1: 4999,
  z2: 7499,
  o1: 13000,
  ...and so on
}

Next, create an array containing all the available options:

const options = ['z1', 'o1', 'y8', 'i', and more];

Use a function to loop through the array and determine the total price:

function determinePrice(options) {
  let price = 0;
  for (let option of options) {
    price += prices[option];
  }
  return price;
}

When adding options multiple times, enter them individually instead of duplicating entries in the array.

For this method to work effectively, ensure that all components have access to the same array for storing selected options. Implementing this will vary based on your project's structure; refer to Angular Component Interaction for ideas:

Angular Component Interaction

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

What is the reason TypeScript does not display an error when assigning a primitive string to an object String?

From my understanding in TypeScript, string is considered as a primitive type while String is an object. Let's take a look at the code snippet below: let s: string = new String("foo"); // ERROR let S: String = "foo"; // OK It's interesting to ...

How to utilize *ngFor alongside the async pipe for conditional rendering in Angular 8 HTML

.html <ng-container *ngFor="let contact of listContact | async; let index = index;"> <h6 class="title" *ngIf="contact && contact['type']"> {{contact['type']}} </h6> <div> {{conta ...

To optimize your bundling process, include the leaflet.css file from the node_modules/leaflet directory using webpack

Currently, I am working on developing a map application using webpack and leaflet. While I can successfully require leaflet.js from my map.js file, I encounter an error when trying to call leaflet.css. The configuration in my webpack.config.js is as follo ...

The code for filtering an array in Angular 9 seems to be failing to display the expected filtered items

Here is the JSON data: [ { "products": [ { "id": "1", "name": "Apple", "price": "free", "category": "Fruits" }, { And so on And this : [{ "categories": [ ...

Invoke the identical function in between two functions that make asynchronous AJAX calls

I seem to be a little lost at the moment. The code snippet below illustrates my intent, which is to use the insertChilds() function to insert child elements in the DOM in a cascading manner (or at least that's what I'm aiming for...). The challe ...

The previous link is still present in the current URL

Having some trouble with my button code here. I'm new to HTML and I have a button that references another HTML file using a parameter. <a class="btn btn-primary" role="button" href="reto_resultado/{{ clave_primaria }}">Check</a> The issu ...

If a quote character is detected in a string, color the line red

My goal is to highlight each line from a string in red if it contains the ">" character. I am applying this logic to a database query result, and while it's successful, I'm encountering an issue where an HTML line break is inserted after each "qu ...

Error message: Conflicting type declarations across multiple files

I am facing a challenge with my TypeScript 'snippets' project. It seems that multiple .ts files contain type names (like Foo) that are the same. //file-a.ts type Foo = { } //file-b.ts type Foo = { } When attempting to compile, I encounter ...

`The Best Times to Utilize Various Image Formats Within a Single Webpage`

If I have two identical images displayed on a page at different sizes, what is the best option for optimizing performance? On one hand, using an image already sized correctly can improve performance, especially on mobile devices. On the other hand, using t ...

NG02100: InvalidPipeArgument: 'Cannot parse "Wed Jun 29 2022 10:19:00 GM" as a date' when using the DatePipe in Angular

It's frustrating not knowing where in my code I'm triggering this error, but every time I click on an item, this error pops up: https://i.stack.imgur.com/Gmu8S.png I suspect this error indicates a need to convert a string to a date, though I ca ...

What are the steps for importing KnockOut 4 in TypeScript?

It appears straightforward since the same code functions well in a simple JS file and provides autocompletion for the ko variable's members. Here is the TypeScript code snippet: // both of the following import lines result in: `ko` undefined // impo ...

What can Cordova and express js bring to the table together?

I'm feeling pretty lost when it comes to displaying express views in a Cordova app client. It seems like the app would have to send a GET request and then the express application would render the view. But I'm unsure about how to actually make t ...

Optimizing image centering in Next JS as screen size expands

I've been struggling to work with NextJS's Image component. My goal is to set up a banner image that only covers a specific amount of space on the screen, regardless of screen size. I have managed to achieve this by using max-height: 30vh and ov ...

What is the best approach for looping through a JSON object?

Currently constructing a project using Angular and incorporating redux. In my JSON object, there are nested objects with specific values. Let's imagine: name: "john", sex: "m" children: [ { name: "joe", sex: "m" children: [ { name: " ...

What is the approach for for loops to handle non-iterable streams in JavaScript?

In the realm of node programming, we have the ability to generate a read stream for a file by utilizing createReadStream. Following this, we can leverage readline.createInterface to create a new stream that emits data line by line. const fileStream = fs.cr ...

Using Multiline Strings for Arguments

Is there a way to successfully pass multi-line strings containing spaces and tabs as a parameter to an express server? Below is the Express.js code snippet which accepts the parameter: app.get('/:prompt', async (req, res) => { ...

How come my header is not spanning the full width of the page?

Hey there, I've been struggling with a specific issue on my website and can't seem to find a solution anywhere else. The header on my site is supposed to stretch across the entire width of the screen, but for some reason, there's always a ga ...

One way to position a sidebar between two divs is to ensure it adjusts seamlessly to the size of the browser window when resized

In my layout, I have two grids: one for the header and one for the footer. In between these two grids, I am using a sidebar on the left side. Below is the JavaScript code I am using: function adjustSize() { var heights = window.innerHeight; docum ...

Is it possible to save an entire webpage that infinitely scrolls without actually manually scrolling through it?

I'm dealing with a webpage that has infinite downward scrolling. I tried automating the scrolling, but eventually the page became too large to continue scrolling. To fix this, I manually removed some DIV blocks from the source code which decreased the ...

Ways to update a component upon becoming visible in Angular

Within my Angular 4.3.0 project, the header, left panel, and right panels are initially hidden on the login page until a successful login occurs. However, once they become visible, the data is not pre-loaded properly causing errors due to the ngOnInit meth ...