JavaScript If Statements Not Working

Javascript if statements not working

You're assigning with =. Use == or ===.

if( 0 == number ){

text.value = "You didn't enter a number!";
}

Also, be wary of your brace placement. Javascript likes to automatically add semicolons to the end of lines. Source.

Why my if statement is not working while the condition is true?

Your return statement is in the scope of the filter block. So you are returning the html as the boolean of the filter method which will always be true. And then when the filtering is done, you dont do anything with the filtered items.

If you want to return the filtered item, you have to first end the filter block, then find the item and return it.

See this:

import { useState } from "react";
import Select from "react-select";
import "./styles.css";

export default function App() {
const [attributes, setAttributes] = useState([{ label: "", value: 1 }]);
// Data need to break and store label info in another variable and add value dynamically
const addAttributes = [
{ label: "colors", value: 1 },
{ label: "size", value: 2 }
];

// Attributes data
const fakeAttributes = [
{
label: "colors",
object: [
{ label: "Black", value: 1 },
{ label: "Green", value: 2 }
]
},
{
label: "size",
object: [
{ label: "M", value: 1 },
{ label: "S", value: 2 }
]
}
];
return (
<div className="App">
<div className="flex flex-row gap-6">
<div className="basis-1/4">
<label className="block text-sm font-medium text-gray-700 mb-3">
{" "}
Size{" "}
</label>
<Select
onChange={(e) => setAttributes(e)}
className="shadow appearance-none border rounded w-full text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
options={addAttributes}
isMulti
/>
</div>

{fakeAttributes.map((attr) => {
const item = attributes.filter((list) => {
return list.label === attr.label;
})[0];
if (item) {
console.log("ok I am in");
return <h2>Output</h2>;
}
})}
</div>
</div>
);
}

Why is my else/if statement not working right in javascript?

Your "else" have an arguments, but this is not right

const calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours()
const idealSleepHours = getIdealSleepHours()
if (actualSleepHours === idealSleepHours) {
console.log('You got the perfect amount of sleep!')
}
else if (actualSleepHours > idealSleepHours) {
console.log('You got too much sleep!')
}
else{
console.log('You did not get enough sleep!')
}
};

Try this! Sorry for my english <3

simple javascript if not working

You previous line of code says var is_expanded = false;
which means if (is_expanded==false) will always evaluate to true.

So that is exactly what you are getting as output. What did you expect?

Next time when your same method is called, the value for is_expanded is again reset to false due to your first line of code. Then again it will alert no

If statement not working with a global variable

This is a very typical case of asynchronous code management.

You either need to place your code in the then callback:

measureUpSpd().then(x => {
console.log("payload => " + global.payload + " ")
if (typeof (global.payload) == typeof ("")) {
console.log("measurement started")
global.startTime = new Date().getTime();
console.log("end time => " + startTime);
ShowData();
global.payload = null
}
})

Or wrap everything in an async IIFE and await the result from measureUpSpd:

(async() => {
const x = await measureUpSpd();
console.log("payload => " + global.payload + " ")

if (typeof (global.payload) == typeof ("")) {
console.log("measurement started")
global.startTime = new Date().getTime();
console.log("end time => " + startTime);
ShowData();
global.payload = null
}
)();

Javascript : if statement is not working on while/for loop

I found the answer

<html>
<body>
<script>
var n=parseInt(prompt("Enter a number:"));
while(n>=1){
if(n%2!=0){
n=n+1;
}
console.log(n);
n=n/2;
if(n==1){break;}//this will stop Infinity loop
}
</script>
</body>
</html>


Related Topics



Leave a reply



Submit