if runs with low-number >= high-number? [duplicate] - javascript

This question already has answers here:
Why is string "11" less than string "3"? [duplicate]
(6 answers)
Closed 3 years ago.
I am making a mistake or something strange is going on but I am not seeing it.
localStorage value = 9
if (localStorage.getItem('TotalProducts') >= '10') {
alert('total products'+localStorage.getItem('TotalProducts'));
}
Why am I receiving the alert?
Alert content is total products9
Thanks everyone for helping.
For others read the comments below there is some really useful information in there.

if (+localStorage.getItem('TotalProducts') >= 10) {
alert('total products'+localStorage.getItem('TotalProducts'));
}
Convert your TotalProducts value of localStorage to a number and then compare it to 10.

Related

Is there a random function in javascript that doesn't take the range of two numbers? [duplicate]

This question already has answers here:
Getting a random value from a JavaScript array
(28 answers)
Get a random item from a JavaScript array [duplicate]
(13 answers)
Closed 2 years ago.
I want the computer to randomly choose either 8 or 2.
I tried doing (Math.random()*8)+2, it chooses a random number BETWEEN 8 and 2 but I want the computer to randomly choose 8 or 2.
So I tried a different way, and I did this:
if(b==0){
var a = 2;
}
else{
var a = 8;
}
However, it is not working. Is there a more efficient and better way?
Thank you,
Paul
Something like this should do.
function randomSelection(values) {
return values[Math.floor(Math.random() * values.length)];
}
numbers = [2,8];
randomSelection(numbers);

Why is my reduce() function in javascript not working? [duplicate]

This question already has answers here:
JavaScript reduce throwing error for more than two items
(4 answers)
Closed 2 years ago.
arr is an array of customers with different phone plans, but I dont know why my reduce function isn't working to add up my numOfLines. e.g.
arr=[{
customer: {
whatPlan: "no contract",
monthlyCost: 50,
tax:0.4
},
monthlyFee:1,
numOfLines:1
}..........]
console.log(arr.reduce((total, item)=>{return total + item.numOfLines;}));
Can someone help me regarding this? Thanks in advance!
arr.reduce((total, item)=>{return total + item.numOfLines},0);
You need to add "0" to make it work

Find and delete cookie with javascript [duplicate]

This question already has answers here:
How to delete a cookie?
(13 answers)
Closed 6 years ago.
How would like to with javascript find a cookie that starts with 'AzureAppProxyAccess' and delete that cookie? It always has a series of random numbers at the end of the name. It is in the same domain so I have access to it.
This is what I have tried with jquery but I would like just javascript.
for (cookie in $.cookie()) {
if(cookie.substring(0, 19) === "AzureAppProxyAccess") {
$.removeCookie(cookie);
}
};
You can use indexOf in strings.
cookie.indexOf('AzureAppProxyAccess') > 1 ? doSomething : somethingElse
Or, like it was said in one of the comments, use RegEx.

How to check if location.hash has `#access_token=` and if so, do something? [duplicate]

This question already has answers here:
How to check whether a string contains a substring in JavaScript?
(3 answers)
Closed 6 years ago.
Right now, I'm just doing the following for my Auth0 login -
if (location.hash) {
// do something
}
How can I make sure it will only do this when it includes #access_token= ?
Use indexOf like so.
if(location.hash.indexOf("#access_token=" > -1) {
// Do stuff
}
You can learn more on indexOf here.

why does coffeescript print to the console by default, and how to disable this? [duplicate]

This question already has answers here:
Prevent Node.js repl from printing output
(6 answers)
Closed 7 years ago.
Why does this code print the entire loop to the coffeescript console if i don't console.log explicitly?
multiples = (num * 10 for num in [0..1000])
how can i disable this behavior?
in Python this works without printing anything
multiples = [x*10 for x in range(1000)]
Try to add return true at the end.

Categories

Resources