How to use elseif statement to show hidden text - if-statement

New to Javascript, need some help with hidden panels.
I want the user to answer some questions in a radio button form which will have different values assigned to the checked answers. There will be 3 different options of results in hidden panels, one result will be displayed at the end depending on the score.
Link to full code and html here
function results() {
var lRisk = document.getElementById('lowRisk')
var mRisk = document.getElementById('mediumRisk')
var hRisk = document.getElementById('highRisk')
if ((score >= 0) && (score =< 15)) {
lRisk.style.display = 'inline';
} else if ((score >= 16) && (score <= 25)) {
mRisk.style.display = 'inline';
} else {
lRisk.style.display = 'inline';
}
Would be very grateful for any help/advice!

If you need to display only ONE control based on the score, you should make all of them hidden in the start then do your conditions.
function results() {
var lRisk = document.getElementById('lowRisk')
var mRisk = document.getElementById('mediumRisk')
var hRisk = document.getElementById('highRisk')
lRisk.style.display = 'none';
mRisk.style.display = 'none';
hRisk.style.display = 'none';
if (score >= 0 && score <= 15) {
lRisk.style.display = 'inline';
} else if (score >= 16 && score <= 25) {
mRisk.style.display = 'inline';
} else {
hRisk.style.display = 'inline';
}
}

Related

How to loop multiple AVPlayers in Swiftui?

I am trying to play different sounds from firebase storage and users will be able to play multiple sounds at the same time (max 5). These sounds has to be in loop until users stop them. I tried some of approaches from another answers but I faced with some bugs since they are not for multiple AVPlayers. Here is my code
ForEach($data.categoriesArray[i].sounds) { $sound in
let n = sound.number
Button(action:{
if(isItActiveArray[i][n] == false){
let storage = Storage.storage().reference(forURL: sound.soundURL)
storage.downloadURL{(url, error) in
if error != nil {
print(error)
} else{
sound.assignedPlayer = PlayerToAssign(array: isPlayerFull)
fullPlayersCount += 1
isPlayerFull[sound.assignedPlayer] = true
players[sound.assignedPlayer] = AVPlayer(url: url!)
players[sound.assignedPlayer].play()
}
}
isItActiveArray[i][n] = true
} else{
players[sound.assignedPlayer].pause()
fullPlayersCount -= 1
isPlayerFull[sound.assignedPlayer] = false
sound.assignedPlayer = -1
isItActiveArray[i][n] = false
}
}){
ZStack{
SoundView(sound: sound)
if(!userViewModel.isSubscriptionActive && i != 0){
Image("lock")
.resizable()
.scaledToFit()
.frame(width:35)
.position(x:100,y: 10)
}
}
}.opacity(isItActiveArray[i][n] ? 0.5: 1)
.disabled(fullPlayersCount < 5)
}

Problem with If/Then code with Google Sheet Script

Someone please help.
I am trying to create a simple script which records button presses in order.
I have:
function P1Buzzer() {
var sheet = SpreadsheetApp.getActiveSheet();
var row = 6;
var col = 1;
var Name = sheet.getRange(row, col).getValue();
var sheet = SpreadsheetApp.getActiveSheet();
var row = 192;
var col = 1;
var AnswerPosition = sheet.getRange(row, col).getValue();
if (AnswerPosition = 1) {
sheet.getRange(199, 1).setValue(Name);
var AnswerPosition = AnswerPosition + 1
sheet.getRange(192, 1).setValue(AnswerPosition);
}
if (AnswerPosition = 2) {
sheet.getRange(200, 1).setValue(Name);
var AnswerPosition = AnswerPosition + 1
sheet.getRange(192, 1).setValue(AnswerPosition);
}
}
The problem I have is that when I run the script it does both the commands, ignoring the if command.
Any ideas?
First, you are setting the value to 1 by using = instead of ==. Second, if it was 1, you add one to it and then check to see if it is 2. I'll bet it is!
if (AnswerPosition == 1) {
sheet.getRange(199, 1).setValue(Name);
var AnswerPosition = AnswerPosition + 1
sheet.getRange(192, 1).setValue(AnswerPosition);
} else if (AnswerPosition == 2) {
...

How to write If ELSE statement in Google Sheets to Hide columns dependent on cell value

Trying to write and if statement in Google sheets to hide a range of columns dependent on cell value in D4
If D4 == 1 hide columns H:AA
If D4 == 2 hide columns O:AA
Else show A:AA
Code below:
var cell = "D4";
var value1 = 1;
var value2 = 2;
var value3 = 3;
var activeSheet = ss.getActiveSheet();
function HideColumn(); {
If(cell == value1) {
activeSheet.hideColumn("H:AA");
Else If(cell == value2) {
activeSheet.hideColumn("O:AA");
Else If(cell == value3) {
activeSheet.unhideColumn("H:AA");
}
}
}
}
Your code needs some modifications:
var ss=SpreadsheetApp.getActive();
var value1 = 1;
var value2 = 2;
var value3 = 3;
var activeSheet = ss.getActiveSheet();
var cell = activeSheet.getRange("D4").getValue();
function HideColumn() {
if(cell == value1) {
activeSheet.hideColumns(8, (27-8+1));
}
else if(cell == value2) {
activeSheet.hideColumns(15, (27-15+1));
}
else if(cell == value3) {
activeSheet.hideColumns(8, (27-8+1));
}
}
Explanations:
If you want to hide several columns rather than one, you need to use the methods hideColumns(columnIndex, numColumns) (mind the notation)
The if and else if statements should be separate rather than nested (note the brackets)
if and else statements in Apps Script are written in low case
The correct way to obtain the content of a cell is var cell = activeSheet.getRange("D4").getValue();
I hope my answer was helpful for you.

Is there any way to write these if statements more efficiently?

I have a spreadsheet where there are a large number of sheets detailing information for each "job" the person filling out the sheet can have. To clean this up, I wrote a script to hide or show the sheets based on which jobs they chose on the first page of the sheet - they can choose up to 3.
The script... works, but I've gotten errors saying it's trying to do too much at once and failed. I'm not exactly a great programmer so how to clean this up is, as of yet, fairly beyond me. I'm not looking for the most efficient, I'm just looking for something that works easily enough.
I googled the problem a few times, but a lot of the solutions I saw didn't seem to exactly fit what I was doing, and involved things like arrays and dictionaries?
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName("Character Sheet");
var sheet2 = ss.getSheetByName("Marauder Abilities");
var sheet3 = ss.getSheetByName("Warrior Abilities");
var sheet4 = ss.getSheetByName("Dark Knight");
var sheet5 = ss.getSheetByName("Gladiator");
var sheet6 = ss.getSheetByName("Paladin");
var sheet7 = ss.getSheetByName("Conjurer");
var sheet8 = ss.getSheetByName("White Mage");
var sheet9 = ss.getSheetByName("Arcanist");
var sheet10 = ss.getSheetByName("Scholar");
var sheet11 = ss.getSheetByName("Astrologian");
var sheet12 = ss.getSheetByName("Pugilist");
var sheet13 = ss.getSheetByName("Monk");
var sheet14 = ss.getSheetByName("Lancer");
var sheet15 = ss.getSheetByName("Dragoon");
var sheet16 = ss.getSheetByName("Rogue");
var sheet17 = ss.getSheetByName("Ninja");
var sheet18 = ss.getSheetByName("Samurai");
var sheet19 = ss.getSheetByName("Archer");
var sheet20 = ss.getSheetByName("Bard");
var sheet21 = ss.getSheetByName("Machinist");
var sheet22 = ss.getSheetByName("Summoner");
var sheet23 = ss.getSheetByName("Thaumaturge");
var sheet24 = ss.getSheetByName("Black Mage");
var sheet25 = ss.getSheetByName("Red Mage");
var sheet26 = ss.getSheetByName("Garlean Pureblood");
var cell1 = sheet1.getRange('B5');
var cell2 = sheet1.getRange('C5');
var cell3 = sheet1.getRange('D5');
if (cell1.getValue() != "Marauder"||cell2.getValue() != "Marauder"||cell3.getValue() != "Marauder") {
sheet2.hideSheet();
}
if (cell1.getValue() == "Marauder"||cell2.getValue() == "Marauder"||cell3.getValue() == "Marauder") {
sheet2.showSheet();
}
if (cell1.getValue() != "Warrior"||cell2.getValue() != "Warrior"||cell3.getValue() != "Warrior") {
sheet3.hideSheet();
}
if (cell1.getValue() == "Warrior"||cell2.getValue() == "Warrior"||cell3.getValue() == "Warrior") {
sheet3.showSheet();
}
if (cell1.getValue() != "Dark Knight"||cell2.getValue() != "Dark Knight"||cell3.getValue() != "Dark Knight") {
sheet4.hideSheet();
}
if (cell1.getValue() == "Dark Knight"||cell2.getValue() == "Dark Knight"||cell3.getValue() == "Dark Knight") {
sheet4.showSheet();
}
It goes on from there for all 26 sheets.
Is there an easier way to write this massive thing out?
Could I maybe do
if (cell1.getValue() == "Marauder"||cell2.getValue() == "Marauder"||cell3.getValue() == "Marauder")
{
sheet2.showSheet();
}
else
{
sheet2.hideSheet();
}
I think this does it:
function myFunction() {
var sA=["Marauder Abilities","Warrior Abilities","Dark Knight","Gladiator","Paladin","Conjurer","White Mage","Arcanist","Scholar","Astrologian","Pugilist","Monk","Lancer","Dragoon","Rogue","Ninja","Samurai","Archer","Bard","Machinist","Summoner","Thaumaturge","Black Mage","Red Mage","Garlean Pureblood"];
var cA=["Marauder","Warrior","Dark Knight","Gladiator","Paladin","Conjurer","White Mage","Arcanist","Scholar","Astrologian","Pugilist","Monk","Lancer","Dragoon","Rogue","Ninja","Samurai","Archer","Bard","Machinist","Summoner","Thaumaturge","Black Mage","Red Mage","Garlean Pureblood"];
var ss=SpreadsheetApp.getActive();
var sheet1=ss.getSheetByName("Character Sheet");
var cells=sheet1.getRange('B5:D5').getValues()[0];
var shts=ss.getSheets();
for(var i=0;i<shts.length;i++) {
var index=sA.indexOf(shts[i].getName());
if(index>-1) {
if(cells[0]!=cA[index] || cells[1]!=cA[index] || cells[2]!=cA[index]) {
shts[i].hideSheet();
}
if(cells[0]==cA[index] || cells[1]==cA[index] || cells[2]==cA[index]) {
shts[i].showSheet()
}
}
}
}

data-bind if statement on click function in knockout js

I need help on executing an one-liner if statement on my data-bind
this is the HTML
<div data-bind="text: Fields.FieldDescription, click:function() { $root.selectedToListRule.push(Fields.FieldCode());}" onmousedown="this.style.backgroundColor='#4BA6FF'"></div>
this is the js code
var RemovefieldCodeRule = function (parent, data) {
//console.log(parent.InitiatorFields(), data.selectedApproverFields());
var sel = selectedToListRule();
//console.log(sel);
for (var i = 0; i < sel.length; i++) {
var selCat = sel[i];
var result = data.selectedApproverFields.remove(function (item) {
return item.FieldCode() == selCat;
});
if (result && result.length > 0) {
//Add to WorkflowObject - InitiatorFields
var obj = dataservice.getEmptyObjects("WorkFlowObjectFields");
obj.done(function (data) {
var wfo = model.genericObject(data);
wfo.ID(result[0].ID);
wfo.WorkFlowObjects_ID(parent.ID());
wfo.FieldCode(result[0].FieldCode());
wfo.FieldDescription(result[0].FieldDescription());
parent.InitiatorFields.push(wfo);
});
}
}
selectedToListRule.removeAll();
parent.InitiatorFields.sort(function (left, right) { return left.FieldDescription() == right.FieldDescription() ? 0 : (left.FieldDescription() < right.FieldDescription() ? -1 : 1) });
data.selectedApproverFields.sort(function (left, right) { return left.FieldDescription() == right.FieldDescription() ? 0 : (left.FieldDescription() < right.FieldDescription() ? -1 : 1) });
}
on my data-bind, there's a click event wherein i will need to select from the dynamically generated div. what i want to happen is when the user clicks on a certain div (it gets selected) and if the user changes his mind to unselect the div he previously selected.
How can i can achieve this on one-liner if statement using knockout