What is the best way to implement a double IF condition in JavaScript and Vue?

This particular scenario involves the execution of the runtime function twice - initially, the function operates as expected, and subsequently it swaps template1 with data from template2.

We seek assistance in resolving this issue.

<template>
  <div>

<v-runtime-template :template="template1"></v-runtime-template>
<v-runtime-template :template="template2"></v-runtime-template>

  </div>
</template>


<script>
import VRuntimeTemplate from "v-runtime-template";
import axios from 'axios'
export default {

    data: () => ({
      x: 0, y: 0,
            template: null,
      template1: null,
      template2: null,
    }),
    components: {
    VRuntimeTemplate,
  },

    mounted() {
        let vm = this
    vm.runtime();

    },
    methods: {

    runtime(){
        /*  If the condition is met, assign a range of indexes to the second block */
      if (this.x == -1){this.x = 11; this.y = 24;}

        /*  Likewise, if this condition is true, assign index range to the first block */
      if (this.x == 0){this.x = -1; this.y = 12;}

this.template = `
    <div class="row justify-content-center">
      <div v-for="(mult, index) in mults">
         <div v-if="(index > x & index < y)">
            <div class="card">
            <!--       -->
            </div>
        </div>
      </div>
    </div>`;

    /* Determine which block was created and assign it to the appropriate template (either template1 or template2) */
if (this.x == 11){this.template2 = this.template;}
if (this.x == -1){this.template1 = this.template;}

    /* There seems to be an error here where two identical blocks are obtained */
    }
</script>

Why is it that when x equals 11, the condition x == -1 evaluates to false but still meets the criteria? How can the IF condition be adjusted to prevent template1 from changing during the second invocation of the runtime () function?

Answer №1

The symbol & in JavaScript is utilized for performing a bitwise AND operation:

console.log(3 & 5);
console.log(0b11 & 0b101);

If you are looking for a boolean AND operation, then you should use && in JavaScript:

console.log(true && false);
console.log(true && true);

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

Developing an npm module that is compatible with both web browsers and Node.js

Currently, I am in the process of developing an npm package that will cater to both web apps and other node modules. If my focus was solely on browsers, I would simply assign window.myExport = myExport; as a direct solution (unless there is a more contemp ...

Having trouble with the jQuery load function not functioning properly

I have encountered an issue with this code snippet while running it on XAMPP. The alert function is working fine, but the HTML content fails to load. I have included links to jQuery 3.2.1 for reference. index.html $(document).ready(function(){ $("#b ...

JavaScript: Utilizing numbers to extend a sequence

My code is saved in a Google Sheets document and here is how it looks: ss = SpreadsheetApp.getActiveSpreadsheet(); function onOpen() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var menuEntries = [ {name: "10 Rows", functionName: "TenRows"}, ...

Error with Ajax-bound selection (dropdown) - possibly due to an empty list in certain browsers

Upon taking over a project involving ASP.Net AJAX and JSON, I encountered an issue on a page that loads a large select (combo box) list of 1,430 entries. This list loads successfully on our main search page but produces an error in MicrosoftAjaxTemplates.d ...

Choosing the default checkbox option as selected in a ReactJS application

Is there a way to preselect checkboxes in ReactJS based on certain output data? For example, I have the following output: {permission:{group:["can_view"],"topGroup":["can_update"]}} . After sending this data to the database and receiving back the edited ...

How can I access the express variable from within AngularJS?

initial exports.render = function(req,res){ res.render('index', { title: 'welcome', username : req.user ? req.user :'' }); }; I am looking to transfer object req.user for utilization h2 Hello #{user ...

Update the table that includes a php script

I have a piece of PHP code embedded within a table tag that displays text from a database. I am looking for a way to automatically refresh this table every minute with updated content from the database, without refreshing the entire page. While I have co ...

Leveraging the power of LocalStorage in Ionic 2

Trying to collect data from two text fields and store it using LocalStorage has proven tricky. Below is the code I have set up, but unfortunately it's not functioning as expected. Can you provide guidance on how to resolve this issue? In page1.html ...

Updating the display text length on vue-moment

Currently, I am attempting to showcase an array of numbers const days = [1, 7, 14, 30, 60] in a more human-readable format using vue-moment Everything is functioning correctly {{ days | duration('humanize') }} // 'a day' // '7 d ...

Setting the base URL in Next.js according to an environment variable: a step-by-step guide

I currently have a Strapi backend and Next.js frontend application deployed on DigitalOcean. Within DigitalOcean, I have configured an environment variable for the frontend: API_URL = ${APP_URL}/api To generate the base URL, I retrieve this variable using ...

JQuery selector is successfully working while vanilla JavaScript is not functioning as expected

This problem is unique because I am experiencing issues with querySelector and querySelectorAll in my JavaScript code. While JQuery works fine, vanilla JS does not work as expected. I would appreciate any insights on why this might be happening. Thank you ...

Tips for effectively engaging with a Component's aggregationUnleash the full potential of

After configuring an aggregation for my Component, here is what it looks like: aggregations : { busyDialog : { type: "sap.m.BusyDialog", multiple: false } } The aggregation is named ...

Activate the CSS on a click event using the onClick method

I am trying to implement a transition that triggers when clicking on a specific div element. Currently, the transition only occurs with the active css class. How can I achieve this effect by simply clicking on the div itself? I am using reactjs and believe ...

Why is my Vue view not being found by Typescript (or possibly Webpack)?

npx webpack TS2307: Cannot locate module './App.vue' or its corresponding type declarations. I am currently utilizing webpack, vue, and typescript. My webpack configuration is pretty basic. It uses a typescript file as the entry point and gener ...

In Vue JS, the v-for directive is incorrectly looping through data as individual characters

Here is the template for my component: <template> <v-card class="mx-auto" width="500" outlined ><v-list-item three-line> <v-list-item-content> <div class="text-overline mb- ...

Discovering the position of digits within a string using Javascript

Could someone help me find the positions of the numbers (1994 and 27) within this string? I attempted to split the string but I'm unsure about how to proceed from there. let str = 'year of birth: 1994, and i'm 27 yo' ...

How can the entire row in an ngTable be turned into a clickable hyperlink, allowing it to open in a new tab or window, using AngularJS?

Currently, I am using AngularJS in conjunction with ngtable. Whenever I create a table, my usual practice is to include ui-sref on the <tr> element as shown below: <tr ng-repeat="element in $data" ui-sref="app.some.details.info({id: element.id}) ...

I am puzzled by the fact that the data stored in Vue.js Vuex using Firebase is not accessible when reloading the page in

When a user hits the reload button on the web browser, I am unsure of what exactly happens... After logging in for the first time, it is fine to display user data. The store state data and caching data from localStorage are accessible. However, upon reloa ...

Tips for preventing the error: Combining spaces and tabs?

Every time I use my Vue.js app, this pesky 'error' message keeps popping up. Annoying Error: Mixed spaces and tabs (no-mixed-spaces-and-tabs) at location src/components/Landing.vue line 388: I'm curious about how to get rid of this annoyi ...

embed animated text in a div element on an HTML page

Is there a way to make the text "try again" inside this div move back and forth? Whenever the player loses and the smily face appears, I have defined that the text in the arrow will change to "try again", and I would like the player to click it in order to ...