Comparing two variables JavaScript [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am new to programming and JavaScript so please bear with me if its a stupid question.
I initialised two variables
let firstName = "blah";
let FirstName = "bleh";
When i write a below if statement i expected the output to be "right on" since the variable names are different (case-sensitive), but i get "boink". Could anyone kindly explain whats happening here?
if (firstName = FirstName) {
console.log('boink')
} else {
console.log('right on')
}

Could anyone kindly explain whats happening here
Actually firstName = FirstName is an Assignment expression and it will return the value on the right handside which is "bleh" which is truthy value. So the first block is executed
You are using assignment operator you need to use comparison operator(== or ===)
let firstName = "blah";
let FirstName = "bleh";
if (firstName === FirstName) {
console.log('boink')
}
else {
console.log('right on')
}

Related

Array[key].push Is Not Defined in JavaScript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
When trying to push items to an array in javascripts it gives an error, the following pseudo code illustrate what is happening:
var data = new Array();
for(...) {
data[key].push(item[i]);
}
It is showing the following error:
Cannot read property 'push' of undefined
Thanks
If you need a 2d array, you have to initialize each element of the outer array to be an array.
// Have to check every array item, if it's not an array already, make it so
for(...) {
if (!data[key]) {
data[key] = [];
}
data[key].push(item[i]);
}
You could always do the following if you know the number of inner arrays you need:
var data = [[],[],[],[],[],[]];
In your example, since they variable name is key, I'm assuming you actually want an object of arrays. If you know the keys ahead of time, you can use the following literal.
var data = {
myKey1: [],
myKey2: []
}

What is the correct syntax to use && and ! in an if condition? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
What is right syntax to check values based on if condition using && and ! operator?
I am getting an error and am confused by !.
main.js
if($scope.sysRecomm === 'High' && !($scope.busDecision==='21')){
$scope.disableSubmitButton = true;
}
if($scope.sysRecomm === 'High' && $scope.busDecision !== '21'){
$scope.disableSubmitButton = true;
}
use like this:
if($scope.sysRecomm === 'High' && $scope.busDecision !='21'){
&& requires both side of the operator to hold TRUE to return true value of entire expression.
For example:
if(X && Y) {
// this block is executed only if X & Y are true
}
Coming to negation operator ! , it negative the truth value of whatever expression it is attached to:
For example if negation operator is used inside a if block:
if(!X){
// executed only when X is false.
}

If/Else only works for first if [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm working with this, but the issue is that only the first if ever functions correctly. If I change the order, each if statement works on its own, but the logic where if the first is false, then check the second, and so on isn't working. What am I missing here?
$("#search_button").click(function(){
var table = $('#main_index').DataTable();
var search_term = $("#second_select2 option:selected").text();
var first_s = $("#first_select2 option:selected").text();
if (first_s = 'District'){
table.columns(1).search(search_term).draw();
}
else if (first_s = 'Territory'){
table.columns(2).search(search_term).draw();
}
else if (first_s = 'Region'){
table.columns(0).search(search_term).draw();
}
else {
console.log('error');
}
});
If I console.log the search_term and first_s variables, I can see them changing and correctly working. And, as I said, without the if/else statements each of these works on its own.
In all your comparisons, you need to replace = by ===
You are using the assignment operator (=) in your comparisons. you should be using the identity operator: ===.
if (first_s = 'District'){
needs to be
if (first_s === 'District'){
as do the rest of the checks.

Function not returning the value [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am working on a javascript quiz programme , and i have return a function to check what difficulty level the user wants . below is the code and the jsfiddle :
function getdifficulty(){
var j = 0;
var level = prompt('what level would you like 1. easy 2. intermediate 3.hard' , '')
if(level == easy){
j = questionseasy[0];
}
else if(level == intermediate){
j = questionseasyenuf[0];
}
else{
j = questionshard[0];
}
alert("you did it");
}
getdifficulty();
Jsfiddle here
now the problem is the the alert is not showing up ? whats the problem with this short piece of code ? (In the real programme though i will not use an alert but return statement , i even tried using document.write or console.log but none worked) .
prompt() returns a string. You need to make your comparisons strings by encapsulating them in double quotes (").
function getdifficulty(){
var level = prompt('what level would you like 1. easy 2. intermediate 3.hard' , '')
if(level == "easy"){
alert("easy");
}
else if(level == "intermediate"){
alert("intermediate");
}
else{
alert("hard");
}
}
getdifficulty();
JSFiddle
Also, in the implementation you have provided in your post, questionseasy, questionseasyenuf, and questionshard will not be defined. You must bring them into the scope of the function before you can start using them.
questionseasy is undefined. You can trace javascript as it runs in your browser console and see this.

Javascript eval in function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I want to make a function that has to generate a random number between 2 numbers and you can save it in a variable. I'm using this code:
function rand(ran1, ran2, randVar) {
var randomNumb = Math.floor(Math.random()*(ran2 - ran1)) + ran1;
eval("var " +randVar+ " = "+randomNumb+";");
}
rand(12, 49, rand1)
alert("Your number is: "+rand1)
The error I get is: Can't find variable: rand1
Can anyone help me?
Using eval for this is entirely unnecessary. I'd recommend something like this instead:
function rand(ran1, ran2) {
var randomNumb = Math.floor(Math.random()*(ran2 - ran1)) + ran1;
return randomNumb;
}
var rand1 = rand(12, 49);
alert("Your number is: " + rand1);
Notice that in calling the function, the only real difference is in where you place the identifier, rand1.
Further Reading
Functions
return

Categories

Resources