else if is not printing the console value - javascript

I have JavaScript code which displays a multi coffee value.
If I am trying to display a single value coffee, its not going inside the else if.
I modified existing code but getting an undefined error.
Can you tell me how to fix it?
var multCoffees = false;
var singleCoffee = false;
if (Coffees.length > 1) {
multCoffees = true;
}
if (Coffees.length > 1) {
singleCoffee = true;
}
if (apptTimeCell) {
apptTimeHTML = MyDay.dish(allData, multCoffees, singleCoffee);
apptTimeCell.innerHTML = apptTimeHTML;
} else {
apptTimeCell = Util.cep("span", {
className: "appt-time"
});
patientRowTD.insertBefore(apptTimeCell, patCell);
}
dish: function (allData, multCoffees, singleCoffee) {
if (multCoffees) {
var htmlArr = [];
htmlArr.push(allData.APPT_TIME_DISPLAY, "<br/><span class='sub-detail'>", allData.MNEMONIC, "</span>");
console.log("multiCoffee" + allData.PROVIDER_MNEMONIC);
return htmlArr.join("");
} else if (singleCoffee) {
console.log("inside if" + allData.PROVIDER_MNEMONIC);
var htmlArr = [];
htmlArr.push(allData.APPT_TIME_DISPLAY, "<br/><span class='sub-detail'>", allData.PROVIDER_MNEMONIC, "</span>");
console.log("singleCoffee" + allData.PROVIDER_MNEMONIC);
return htmlArr.join("");
} else {
return allData.APPT_TIME_DISPLAY;
}
},
Working code:
var multCoffees = false;
if (Coffees.length > 1) {
multCoffees = true;
}
if (apptTimeCell) {
apptTimeHTML = MyDay.dish(allData, multCoffees);
apptTimeCell.innerHTML = apptTimeHTML;
} else {
apptTimeCell = Util.cep("span", {
className: "appt-time"
});
patientRowTD.insertBefore(apptTimeCell, patCell);
}
dish: function (allData, multCoffees) {
if (multCoffees) {
var htmlArr = [];
htmlArr.push(allData.APPT_TIME_DISPLAY, "<br/><span class='sub-detail'>", allData.MNEMONIC, "</span>");
console.log("multiCoffee" + allData.PROVIDER_MNEMONIC);
return htmlArr.join("");
} else {
return allData.APPT_TIME_DISPLAY;
}
},

Suppose Coffees.length is 2. You do this...
if (Coffees.length > 1) {
multCoffees = true;
}
...and 2 > 1, so now multCoffees is true, but then you do this, which checks the same thing...
if (Coffees.length > 1) {
singleCoffee = true;
}
and, since 2 > 1 still, now BOTH multCoffees AND singleCoffee are true. So when you try to do
if (multCoffees) {
...
} else if (singleCoffee) {
...
}
the first if branch is true, so it is executed, and the else branch is thus ignored (despite also being true). You probably meant to instead start with
if (Coffees.length == 1) {
singleCoffee = true;
} else if (Coffees.length > 1) {
multCoffees = true;
}

replace your first two ifs with this block:
if (Coffees.length == 1) {
singleCoffee = true;
}
else if(Coffees.length > 1){
multCoffees = true;
}
and then try it again!

Related

This palindrome code returns error only while submitting in leetcode, its working in my local for same testcase

This code return "false" for test input "1" only when I submit in leet code. It's working when I do it in my local or leet code editor for the same input
var temp = 0;
var rev = 0;
var palindromeCheck = function (org) {
temp = org % 10;
rev = rev * 10 + temp;
org = parseInt(org / 10);
if (org > 0) {
palindromeCheck(org);
}
return rev;
};
var isPalindrome = function (x) {
if (x == 0) {
return true;
}
else if(x > 0) {
var value = palindromeCheck(x);
if (value === x) {
return true;
}
else {
return false;
}
}
else {
return false;
}
};
your palindromeCheck() function return a number. The problem comes from this part, where you strictly compare a number and a string:
if (value === x) {
return true;
}
else {
return false;
}
try to do :
if (value == x) {
return true;
}
else {
return false;
}
or
return (value == x);
else you can just use parseInt() function.

Communication issue between functions into JS and JSX files

I have made this function into a JS file...
function getColors(isPick, isForecolor)
{
var chosenFunction = 'getColor(' + isPick + ', ' + isForecolor + ')';
csInterface.evalScript(chosenFunction, function(result)
{
if(result !== 'undefined')
{
if (isForecolor == true){
foregroundHexColor = result;
// etc...
}
else
{
backgroundHexColor = result;
//etc..
};
};
});
};
which get a hexadecimal color value from this function from a JSX file.
function getColor(isPick, isForecolor)
{
var color_PickerCase;
var decimal_Color;
var hexadecimal_Color;
if (isForecolor == true)
{
color_PickerCase = app.foregroundColor.rgb.hexValue;
}
else
{
color_PickerCase = app.backgroundColor.rgb.hexValue;
};
if (isPick == true)
{
if (app.showColorPicker(isForecolor)){
decimal_Color = color_PickerCase;
hexadecimal_Color = decimal_Color.toString(16);
}
else
{
return;
};
}
else
{
decimal_Color = color_PickerCase;
hexadecimal_Color = decimal_Color.toString(16);
};
return hexadecimal_Color;
};
In some way it works, but for some reason I have to do the same thing two times so to get the value!!! Any idea why is this happening?
Thank you for your time!!!
UPDATE: A correction, it works only at first click. Then needs to clicked two times so to get the value!!!
Well, here is the solution...
function getColor(isPick, isForecolor)
{
var color_PickerCase;
var decimal_Color;
var hexadecimal_Color;
if (isPick === true && app.showColorPicker(isForecolor) === false)
{
return;
}
if (isForecolor === true)
{
color_PickerCase = app.foregroundColor.rgb.hexValue;
}
else
{
color_PickerCase = app.backgroundColor.rgb.hexValue;
}
decimal_Color = color_PickerCase;
hexadecimal_Color = decimal_Color.toString(16);
return hexadecimal_Color;
};
As joojaa from graphicdesign said, I was asking for the color before picking it and I was getting the color form the last time!!!

Why my array data does not print in render function REACTJS?

I am stuck in reactjs.
I have a function in which there is an array containing some values, but when I want to access that array in render function and pass it using props to another function, it returns a blank array.
what should I do to resolve this problem?
Like this:
In Function:
this.usersAnswerXML = ["ID0", "ID1", "ID2"]
In Render:
this.usersAnswerXML = []
Here is my code, what am I doing wrong?
handleSplitContentClick(contentId, selectionType) {
let isCorrect
if (selectionType == 'selected') {
const index = this.correctAnswers.indexOf(contentId);
if (index > -1) {
this.userCorrectAnswers.push(contentId);
if (this.correctAnswers.length === this.userCorrectAnswers.length &&
this.userUncorrectAnswer.length == 0) {
isCorrect = this.correct
} else {
isCorrect = this.incorrect
}
} else {
this.userUncorrectAnswer.push(contentId);
isCorrect = this.incorrect
}
} else if (selectionType == 'disselected') {
const index = this.correctAnswers.indexOf(contentId);
if (index > -1) {
this.userCorrectAnswers.splice(index, 1);
isCorrect = this.incorrect
} else {
this.userUncorrectAnswer.splice(index, 1);
if (this.correctAnswers.length === this.userCorrectAnswers.length &&
this.userUncorrectAnswer.length == 0) {
isCorrect = this.correct
} else {
isCorrect = this.incorrect
}
}
}
this.userAnswerXML = this.userCorrectAnswers.join(',')
this.usersAnswerXMLs = this.userAnswerXML + ',' +
this.userUncorrectAnswer.join(',')
this.usersAnswerXML = this.usersAnswerXMLs.split(',')
console.log(this.usersAnswerXML)
if (window.uaXML) {
this.userAnswerXML = window.uaXML
console.log(this.userAnswerXML + "data")
}
// this.usersAnswerXML = window.uaXML
console.log(window.uaXML)
this.userAnswerXML = "<smans type='4'><div id='textID0' userAns='" +
this.usersAnswerXML + "'></div></smans>"
$("#special_module_user_xml").val(this.userAnswerXML )
console.log(this.usersAnswerXML)
}
} // Editor's note: this is an extra curly brace
render() {
if (this.props.remedStatus) {
console.log(this.usersAnswerXML)
console.log("inside remed")
return (
<HotspotRemed
xml={this.receivedXML}
userXml={this.usersAnswerXML}
correctAnswer ={this.ansString}
type={this.splitType}
/>
)
} else {
return (
<div className="previewtemplate" style ={template}>
{this.templateArea(this.state.templateType)}
</div>
);
}
}
} // Editor's note: another extra curly brace
} // Editor's note: another one again

Screeps: Get the id of an object

I tried to write a code that prevents 2 creeps (harvester) from going to the same destination and binds them to that destination until its full.
on line 55 I get an error which is understandable
55: creep.memory.targetID = targets[checkTarget].id;
for a reason that I am not seeing targets[checkTarget] is null
Can anyone tell me what i am doing wrong?
var roleHarvester = {
/** #param {Creep} creep **/
run: function(creep) {
if (!creep.memory.targetID) creep.memory.targetID = "not given";
console.log(creep.memory.targetID);
if (creep.carry.energy < creep.carryCapacity) {
var nearestSource = creep.pos.findClosestByPath(FIND_SOURCES_ACTIVE);
if (creep.harvest(nearestSource) == ERR_NOT_IN_RANGE) {
creep.moveTo(nearestSource, {
visualizePathStyle: {
stroke: '#ffaa00'
}
});
}
} else {
if (creep.memory.targetID != "not given") {
console.log("should not be not given");
if (Game.getObjectById(creep.memory.targetID).hits == Game.getObjectById(creep.memory.targetID).hitsMax) {
creep.memory.targetID = "not given";
var targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_SPAWN ||
structure.structureType == STRUCTURE_EXTENSION ||
structure.structureType == STRUCTURE_TOWER) && structure.energy < structure.energyCapacity;
}
});
} else {
if (creep.transfer(Game.getObjectById(creep.memory.targetID), RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(Game.getObjectById(creep.memory.targetID), {
visualizePathStyle: {
stroke: '#ffffff'
}
});
}
}
} else {
var targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_SPAWN ||
structure.structureType == STRUCTURE_EXTENSION ||
structure.structureType == STRUCTURE_TOWER) && structure.energy < structure.energyCapacity;
}
});
console.log(targets);
for (var checkTarget in targets) {
console.log(checkTarget);
console.log(targets[checkTarget]);
for (var name in Game.creeps) {
if (targets[checkTarget] = Game.getObjectById(Game.creeps[name].memory.targetID)) {
var fail = true;
break;
}
var fail = false;
}
if (fail == true) continue;
console.log(Game.getObjectById(targets[checkTarget]));
creep.memory.targetID = targets[checkTarget].id;
break;
}
}
}
}
};
module.exports = roleHarvester;
Something wrong in the following if condition:
for(var name in Game.creeps) {
if(targets[checkTarget] = Game.getObjectById(Game.creeps[name].memory.targetID)){
Probably you mean == ?

Javascript function returns undefined instead of int

My js/jquery function does not work properly and instead of INT returns undefined.
function __getLastSelectedCategory(table_id) {
if ( jQuery('.categories_table[data-table-id="1"]').find('td.active').length > 0 ){
console.log('check1');
if (table_id != '0') {
console.log('check2');
var checkTableId = parseInt(table_id) - 1;
var table = jQuery('.cl_categories_display ').find('table[data-table-id="' + checkTableId + '"]');
if (table.find('td.active').length > 0) {
console.log('check3');
console.log('table id: ' + table.find('td.active').data('category-id'));
return table.find('td.active').data('category-id');
} else {
console.log('check4');
__getLastSelectedCategory(checkTableId);
}
} else {
console.log('check5');
var lastTable = jQuery('.cl_categories_display ').find('table:last');
var lastTableId = lastTable.data('table-id');
if (lastTable.find('td.active').length > 0) {
console.log('check6');
return lastTable.find('td.active').data('category-id');
} else {
console.log('check7');
__getLastSelectedCategory(lastTableId);
}
}
} else {
console.log('check8');
return null;
}
}
when I run this function I see in console:
check 1
check 5
check 7
check 1
check 2
check 3
table id: 1
last cat: undefined
so the recursion works fine, but instead of integer (console printed "table id: 1") ir returns undefined. What could be wrong?
You forgot return from recurse call: It returned value from the inner function to the outer, but did not return it from outer function to the caller. Try this:
function __getLastSelectedCategory(table_id) {
if ( jQuery('.categories_table[data-table-id="1"]').find('td.active').length > 0 ){
console.log('check1');
if (table_id != '0') {
console.log('check2');
var checkTableId = parseInt(table_id) - 1;
var table = jQuery('.cl_categories_display ').find('table[data-table-id="' + checkTableId + '"]');
if (table.find('td.active').length > 0) {
console.log('check3');
console.log('table id: ' + table.find('td.active').data('category-id'));
return table.find('td.active').data('category-id');
} else {
console.log('check4');
return __getLastSelectedCategory(checkTableId);
}
} else {
console.log('check5');
var lastTable = jQuery('.cl_categories_display ').find('table:last');
var lastTableId = lastTable.data('table-id');
if (lastTable.find('td.active').length > 0) {
console.log('check6');
return lastTable.find('td.active').data('category-id');
} else {
console.log('check7');
return __getLastSelectedCategory(lastTableId);
}
}
} else {
console.log('check8');
return null;
}
}

Categories