News Articles if-statement

How to conditionally set font-size on Sass depending on string length

I want to conditionally set the font-size of a div depending of the string length with Scss. Something like this: if id.length > 30 id.style.fontSize = 2vw Source: CSS – Stack Overflow

First function is faster when empty but when both are filled, first function is slower

Sorry, question may not be so clear. I’ve been interested in writing a perfect square checking function in Javascript. For the standard case, I have written the below: for(let i = 1; i < 1000000; i++) { if(Math.sqrt(i) % 1 == 0) { // Is perfect square } } This function typically finishes in about […]

check 2 arrays if the result is equal in vue

I need help, I have two array like this let A = [‘a’, ‘b’, ‘c’] let B = [‘a’, ‘b’, ‘c’] and I try this but not work if(A === B) { console.log(‘EQUAL’) } else { console.log(‘NOT EQUAL’) } how to check if they are equal in vue? how to implement it? Source: Vue – […]

change style if two conditions are true

I have a small app that should get two price values from elements on a website. This is achieved by iterating over the .textContent of the two querySelectors (cardStartBidValueString and cardCurrentBidValuesString). Then I also get all elements with a specific CSS selector (cardBg). What I want to do is to check if the start price […]

else if statement declaration or statement expected

im trying to get this else if statement to work using JavaScript, my thinking is that something is wrong with the parenthesis but I cannot for the life of me figure out what. function test() { var visanum = document.getElementById("visanum") var cardtype = document.getElementById("cardType") var masterCard = document.getElementById("MasterCard") var visa = document.getElementById("Visa") var masternum = […]

Checking variable outside of its scope

In my code I want to execute a query conditionally and then check its loading variable inside the template, but only if the query is executed and don’t want to have any TypeScript errors in my file. How do I check this variable which might or might not exist in its context? if( executeMyQuery ) […]

(HTML && JS) My program written for class is only returning "0" when it’s supposed to calculate a number based on order of clicks

I’m struggling with my beginners coding class hw and at this point I’m desperate. I’m trying to make a program that shows a series of five images, and asks the user if they see their number on it. if yes, it should add 1,2,4,8,16 (in that order; depending on the image shown)to the total number. […]

If statement best practice-implicit vs explicit [closed]

Which approach is the best practice in if statements while using a boolean-valued function call in the condition? Is it better to be explicit or implicit? if (checkValue(currentValue)){ alert("This is true"); }else{ alert("This is false"); } vs if (checkValue(currentValue) === true) { alert("This is true"); }else{ alert("This is false"); } Source: JavaSript – Stack Overflow

how i can to do that, without ternary operator

function dayOfProgrammer(year) { let isLeefYeer = year % 4 === 0 ? year % 100 === 0 ? !!(year < 1918 || year % 400 === 0) : true : false; return `${year === 1918 ? 26 : isLeefYeer ? 12 : 13}.09.${year}`; } Source: JavaSript – Stack Overflow

useState not reading variable outside of if statement

I’m trying to set a variable "list" in an "if" statement, and after that, update my data via useState, but if I write setData outside the "if" statement, it shows that the variable "list" is not defined, so I must rewrite all the code inside the "if" and the "else" statement (which is not desirable) […]

CSS animation triggers only first time

I am making a witcher themed page as a side project and have come across a problem. I have made an alchemy page and have all ingredients listed, with all elements that can be extracted beside them. I have made a function that triggers animation with the following logic: Clicking an ingredient will trigger the […]

If statements are not working in using javascript or jsx

Having issues to get code block to log to console Does not work. Nothing is logged to console var data = {"_id": "615fc3db8b6a6311aa3295ca", "isComplete": false, "remindTime": {"date": "2021-10-08", "time": "12:08:40 am"}, "subtitle": "Fdfdff", "title": "Dfdf"} if (data.length > 0) { console.log(‘my data’, data); } WHAT WORKS: var data = {"_id": "615fc3db8b6a6311aa3295ca", "isComplete": false, "remindTime": {"date": […]

Creating loops and adding else statement

Question 1: How do I add a else statement to this code with a string of ‘what is this’? let callback = (num) => { if (num % 2 == 0) return ‘the number is even’; else { return ‘the number is odd’}; } console.log(callback(2)); Question 2: How do create a loop that iterates from […]

Set value to zero based on user input form condition using javascript

I have created a simple calculator. I have set the user input with if-else condition to show / hide input form fields using radio buttons. Here is the code: Amount Saved For retirement : Yes <input type="radio" onclick="javascript:yesnoCheck();" name="yesno" id="yesCheck"> No <input type="radio" onclick="javascript:yesnoCheck();" name="yesno" id="noCheck"> <div id="ifYes" style="visibility:hidden"> <div class="i_container"> <div class="i_row"> <label id="saved_amount_label_id" […]

Find duplicate names within an array of different files

A bit of a different use case from the ones I was suggested above. I need to loop through and check each file name within an array of files and push the files that have the same name into a new array so that I can upload them later separately. This is my code so […]

Game where two players have to try to guess a random number

I wanted to create a game where two players have to try to guess a random number. Here’s the code i tried to use: const player1 = ’87’ const player2 = ’55’ const randomNumber = ’55’ //Math.floor(Math.random() * 100 + 1) console.log(‘Player 1:’, player1) console.log(‘Player 2:’, player2) if(randomNumber != player1 || randomNumber != player2){ //if […]

this.state is not a function

need a helping hand, a stuck in a simple conditional rendering in reactJS the code is: I am changing the login and logout button based on the conditions. for eg. if the user is logged in, the button should be Logout and if the user is logged in then the button should be Login. I […]

counting characteres in string using object

I’m trying to count the characters in a string using an object. This is the function that I wrote: function maxChar(str) { let obj = {} for(let char of str){ if(obj[char]){ obj[char] += 1 } obj[char] = 1 } console.log(obj) } When I run the function with the string "Hello There!" it returns: { : […]

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.