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

Using CSS selectors in Framework7 Vue allows for precise targeting and styling

I am currently working on developing a Cordova/Phonegap application using vue.js and the Framework7. I have been able to utilize functions like "onClick" by using the "v-on:click="OnClick" attribute within an HTML element. It's worth noting that Frame ...

Tips for centering text vertically in an input box specifically for Internet Explorer (IE)

Here is the code I am working with: <input type="text" class="contactInput" value="my string"> .contactInput { border:0; margin:0; padding:0; background-color:#000000; color:#ffffff; height:22px; width:290px; padding ...

Align the items at the center of a Vue Router Link

I am looking to design a navigation bar that includes router-link items, with each item displaying an icon on the left side and text on the right side. An example of this design can be seen in the Firebase navigation bar. The navigation bar I created loo ...

Stop the flow of data in the RxJS stream depending on a specific value within the stream

I developed a straightforward component featuring a single button that initiates and halts a sequence of numbers generated by RxJS timer. import { Component, OnInit } from '@angular/core'; import { BehaviorSubject, Observable, timer, merge } fro ...

utilizing parent scope in a jQuery function callback

Currently, I am facing an issue concerning a jQuery callback working on a variable that is outside of its scope. To illustrate this problem, consider the code snippet below: $('#myBtn').on('click', function(e) { var num = 1; / ...

Generate various routes and subroutes from an array

I need to create routes for an array of items, each with its own main page and nested pages. Here is an example structure: myapp.com/[item] (main item page) myapp.com/[item]/about (about item page) myapp.com/[item]/faq (faq item page) My goal is to itera ...

The error encountered in Heroku logs is due to a Mongoose error where the uri parameter passed to openURI() must be a string, but is

I've been encountering an issue while trying to deploy my project on Heroku. Despite attempting various methods, one error remains unresolved. Here is the snippet of code that I added: useNewUrlParser: true,useCreateIndex: true, app.use(express.urlen ...

What techniques can I implement with puppeteer to efficiently warm up the cache?

I have a lengthy txt document containing around 1000 URLs that need to be accessed in order to warm up the Varnish cache. Since Puppeteer is required, it's crucial that there is important content loaded through AJAX calls. This is my initial attemp ...

When setting an empty URL with Fabricjs' setBackgroundImage function, a null reference error occurs in the _setWidthHeight

Recently, I stumbled upon an article detailing a method to clear the background of a fabric canvas... canvas.setBackgroundImage('', canvas.renderAll.bind(canvas)); In the development of my online design tool which utilizes Fabricjs 1.4.4, I have ...

Incorrect rendering on mobile screen width

I am facing an issue where my div does not display as 100% width in the mobile version. Below is the JSX code I am using: <div> <div expand="lg" className="login-header"> <h1>logo</h1> <h1&g ...

Is there a way to utilize a Javascript function to incorporate commas into a div tag?

Being new to JavaScript, I have heard about jQuery but I don't know how to use it. Please bear with me as I am not well-versed in this area. :) I am currently importing data dynamically from an Excel database to a website. Everything is working fine ...

Guide on transferring input element to h3 tag without using the URL

Is there a way to update the h3 tag every time a user clicks without updating the tab URL? I only want to update the h3 tag. I have attached all files and a screenshot of the URL. <!DOCTYPE html> <html> <head> </head> ...

Explain the inner workings of the setTimeout() function in JavaScript

My goal is to create a line in my code by placing points according to the line equation and adding a 100 millisecond delay before each point is displayed. However, when I try to run the code, it seems to wait for some time and then displays all the points ...

Ways to selectively deactivate client-side functionality

I have implemented a server-side rendered app with transitions, including a 404 error page that I placed in a lazy module to avoid increasing the size of loaded JavaScript. While this setup is functioning correctly, there is some flickering when the clien ...

Receiving a reply from the axios function

Whenever I try to call the lookUpItem function from ItemSearch.vue, I always get an undefined response. Code snippet from ItemSearch.vue: <script setup lang="ts"> import { lookUpItem } from '../systemApi' async fu ...

No errors found on minikube for the Angular app, just a blank page displayed

I recently created an Angular application and successfully built the image using Docker: # stage 1 FROM node:10-alpine as node WORKDIR /app COPY package*.json /app/ RUN npm install COPY ./ /app/ RUN npm run build # stage 2 FROM nginx:1.17-alpine COPY ...

Refresh the Server Component once data has been modified by the Client Component within Next.js

I am still grappling with this specific scenario and trying to figure out the best approach in Next.js 13. Can anyone provide guidance on the correct way to handle this? In a Server Component, I have a list of users being displayed, leveraging MongoDB, as ...

Issue with Material-ui IconButton's edge properties not functioning as expected

Is there a way to position the delete icon on the right side of the screen? It seems like using edge= "end" is not working as expected. If you'd like to take a look, here is the sandbox link: https://codesandbox.io/s/admiring-silence-vwfoe? ...

Developing a specialized directive to enhance bootstrap menuItems

I have created a custom directive in AngularJS for the navbar in Bootstrap. This directive uses ng-transclude and handles two types of list items (li) depending on whether it is a dropdown or not. However, I am experiencing issues with the dropdown functio ...

What function does the ng-template serve when encapsulated within an ng-select component?

Upon observing various instances of ng-select, I've noticed that it often involves wrapping a ng-template, as exemplified below: <ng-select [items]="cities" [(ngModel)]="selectedCity" bindLabel="name" bindV ...