This question already has answers here:
What is the difference between "let" and "var"?
(39 answers)
Is using 'var' to declare variables optional? [duplicate]
(14 answers)
Closed 6 days ago.
new to this. I'm building a todo app and I want the loop to stop when the user enters 'quit'. But this doesn't work and it goes into an infinite loop. What am I doing wrong?
This is the intial part of the code. PS: for now I'm only testing the quit part. I will be adding the inputs to an array later.
let userInput = "";
while (userInput !== 'quit') {
let userInput = prompt("What would you like to do?");
let todoList = [];
if (userInput == 'new') {
let userInput = prompt("Enter a new todo")
console.log(`${userInput} added to the List`)
}
}
console.log("it's over");
Related
This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
What is the difference between the `=` and `==` operators and what is `===`? (Single, double, and triple equals)
(5 answers)
Closed 11 months ago.
So I'm making an idle game and I want when you buy a 'carrot patch' in this example, the price of said 'carrot patch' will raise by 50%. However this doesn't happen.
Here's my code:
var patchPrice = 10;
function buyPatch() {
if (affordable = true) {
var patchPrice = patchPrice * 1.5;
}
}
you have declared the patchPrice variable twice. so it will overwrite the previous value. just remove the second var and it will work.
These were problems with js before es6 I recommend you to use es6 const and let to declare variables.
Try == in the comparison or simply omit the (== true) as it is already a boolean.
var patchPrice = 10;
function buyPatch() {
if (affordable == true) {
var patchPrice = patchPrice * 1.5;
}
}
Avoid using "VAR" unless you wanna work with function scope variables.
This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 1 year ago.
hi what can I use inspite of eval function for that code :
product_pcs = 12;
product_code = "lkb_12" ;
eval(product_code +"_pcs_1 = product_pcs");
alert (lkb_12_pcs_1);
I recommend you build an object instead
const products = {};
prodcuts['lkb_12'].pcs = 12;
prodcuts['lkb_12'].name = 'test';
prodcuts['lkb_13'].pcs = 14;
prodcuts['lkb_13'].name = 'test2';
// ...
``
This question already has answers here:
Check variable equality against a list of values
(16 answers)
What's the prettiest way to compare one value against multiple values? [duplicate]
(8 answers)
Closed 2 years ago.
i have created an input field but the logical OR operator is not working as intended.
the code is below.
'use strict'
let search = document.getElementById('search');
console.log(search);
let link = document.getElementById('link');
console.log(link);
link.addEventListener('click', doSomething);
function doSomething(ev) {
console.log(ev.type);
console.log(ev.target);
if (search.value == 45 || 35) {
alert('hi');
} else {
alert('you');
}
}
This question already has answers here:
Javascript if statements not working [duplicate]
(3 answers)
Closed 2 years ago.
I'm trying to do the following:
if (x) {
// then this
} else if (calcDifference = 7) && (currentCMSI = 7) { // problem line
// then this
} else if {
// then this
}
Or this:
if (x) {
// then this
} else if (calcDifference = 7 && currentCMSI = 7) { // problem line
// then this
} else if {
// then this
}
But I keep getting errors, what's the correct syntax for comparing if 2 different variables are both true in an else if statement?
Neither!
= is an assignment operator
== is used to compare two variables
So to answer your question, the second suggestion but with==
if (calcDifference == 7 && currentCMSI == 7)
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
I have a form that uses the get method and contains an array:
http://www.example.com?name[]=hello&name[]=world
I'm trying to retrieve array values 'hello' and 'world' using JavaScript or jQuery.
I've had a look at similar solutions on Stack Overflow (e.g. How can I get query string values in JavaScript?) but they seem to only deal with parameters rather than arrays.
Is it possible to get array values?
There you go: http://jsfiddle.net/mm6Bt/1/
function getURLParam(key,target){
var values = [];
if (!target) target = location.href;
key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var pattern = key + '=([^&#]+)';
var o_reg = new RegExp(pattern,'ig');
while (true){
var matches = o_reg.exec(target);
if (matches && matches[1]){
values.push(matches[1]);
} else {
break;
}
}
if (!values.length){
return null;
} else {
return values.length == 1 ? values[0] : values;
}
}
var str = 'http://www.example.com?name[]=hello&name[]=world&var1=stam';
console.log(getURLParam('name[]',str));
console.log(getURLParam('var1',str));