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');
}
}
Related
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");
This question already has answers here:
How to check if two arrays are equal with JavaScript? [duplicate]
(16 answers)
Closed 3 years ago.
Hi this is probably an easy question. So I want to make a basic anagram function in Javascript.
The following snippet does not work
anagrams = (phraseOne, phraseTwo) => {
if (phraseOne.split("").sort() === phraseTwo.split("").sort()) {
return true
} else {
return false
}
}
However this does work
anagrams = (phraseOne, phraseTwo) => {
if (phraseOne.split("").sort().join("") === phraseTwo.split("").sort().join("")) {
return true
} else {
return false
}
}
Why? The arrays are identical before you join("") them
That's because strings are compared by value and arrays are compared by reference in JS. You can find out more about comparison here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness
This question already has answers here:
jQuery.inArray(), how to use it right?
(20 answers)
Closed 4 years ago.
I want to check if my array contains the Value Input, i want something like that, anyone have an idea on how to do that ?
if(jQuery.inArray($('#ValueInputTitle').val, variableValueInput) !== -1)
{
console.log("is in array");}
else {
console.log("is NOT in array");
}
You can use this,
if(jQuery.inArray($('#ValueInputTitle').val(), variableValueInput) !== -1)
{
console.log("is in array");}
else {
console.log("is NOT in array");
}
This question already has answers here:
Creating object with dynamic keys [duplicate]
(2 answers)
Closed 5 years ago.
let fileName = "test.c";
let testCase = "Case1";
let test = {};
test.fileName = testCase;
console.log(test)
I need fileName property to be dynamic
What is need is, like below
{
"test.c":"Case1"
}
Can any one help me
test.fileName = testCase;
Won't work in this case. Should be
test[fileName] = testCase;
You can use the ES6 computed property syntax:
{
[fileName]: "Case1"
}
This will be interpreted dynamically as:
{
"test.c": "Case1"
}
This question already has answers here:
What's the meaning of "=>" (an arrow formed from equals & greater than) in JavaScript?
(14 answers)
Closed 7 years ago.
Code example:
function unusedDigits(...args){
return [0,1,2,3,4,5,6,7,8,9].filter(o => args.join("").indexOf(o) === -1).join("")
}
Everything is clear here. Exept =>. What does this mean in javascript?
This is a ES6 arrow function which is a short syntax for function expression. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
So:
// ES5
var selected = [0,1,2,3,4,5,6,7,8,9].filter(function (o) {
return args.join("").indexOf(o) === -1;
});
// ES6
var selected = [0,1,2,3,4,5,6,7,8,9].filter(o => args.join("").indexOf(o) === -1);