Msgbox if YES or NO - javascript

This should be super easy! The code merely consists of a message box with a Yes or No choice. Given that choice, a corresponding string is written into cell A1. I see no problem with it compared to google's script examples. What am I missing?
function msgbox() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var answer = Browser.msgBox("Confirm", "Does this work?", Browser.Buttons.YES_NO);
//if user clicks yes
if(answer === Browser.Buttons.YES) {
//then enter YES into cell A1
s.getRange("A1").setValue() === "YES";
} else {
//else enter NO into cell A1
s.getRange("A1").setValue() === "NO";
};
}

I have never used Google App Scripts, but based on my other programming experiences, I feel like
s.getRange("A1").setValue() === "YES";
might be the problem. Usually, the '===' operator is used to compare 2 values.
'aaa' === 'aaa' --> return true
In your case, don't you want to set a value?
s.getRange("A1").setValue("YES");
is what I would feel appropriate.

You are simply using setValue() the wrong way.
Refer to the documentation here.
It should simply be s.getRange("A1").setValue('YES')

Related

how can you take out extra space in Javascript?

I'm trying to give a welcome message to recruiters that view my html webpage. The javascript code I'm using is:
function myFunction() {
var person = prompt("Please enter your name", "");
if (person == null) {
var message = document.getElementById("demo").innerHTML =
"Hello! Thank you for viewing my resume.";
message.innerHTML.replace((/\s/g,''));
}
else {
document.getElementById("demo").innerHTML =
"Hello " + person + "! Thank you for viewing my resume.";
}
}
The output in the html looks like Hello ! Thanks for viewing my resume. I've tried flipping the else and if outputs but it still adds the extra space. How can I fix this? I know there are similar questions on stack but the solutions haven't worked in my case. Thanks for your help.
in this case I think you can use Template literals (Template strings), one feature of ES6.
function myFunction() {
const person = prompt("Please enter your name", "");
if (!person) {
const message = document.getElementById("demo").innerHTML =
"Hello! Thank you for viewing my resume.";
message.innerHTML.replace((/\s/g,''));
}
else {
document.getElementById("demo").innerHTML =
`Hello ${person}! Thank you for viewing my resume.`;
}
}
The input would be labeled as Undefined which does not necessarily equal Null in JavaScript. My advice would be try creating a different way to check the name. Perhaps just give them the option "Continue as Guest" instead of checking for an empty value.
message.innerHTML.replace((/\s/g,''));
That line is doing absolutely nothing, replace() returns a new string, it doesn't modify the old one
And even if it replaced the old one, the desired output wouldn't really be what you're looking for, it would remove all spaces in the string, all of them.
The way you remove extra spaces is by using .trim(), trim() removes any extra spaces at the start and/or at the end of the string, so you would do: message.innerHTML = message.innerHTML.trim();
if (person == null) {
This wouldn't work, since prompt would return an empty string if the user didn't provide anything and your explicitly checking if it is equal to null instead, the most "optimal" way for checking for that, is as #Anh Tuan did, if (!person), this works because empty strings, undefined, or null all are falsy, so this is a nice way to check for all of them.
use:
if (!person)
instead of:
if (person == null)
and it should work fine. Everything else can stay the same.

Trying to use IF STATEMENTS to automatically fill cells

As shown in the image, on column C they are supposed write down the homework they have for each class. I want columns D, E, and F to be automatically filled with "N/A" if the student puts "None" on column C (as in they didn't receive homework for a class). But, the columns automatically fill with N/A, even if column C doesn't say None! I don't understand this.
This is what I have so far
function myFunction() {
var app = SpreadsheetApp;
var activeSheet = app.getActiveSpreadsheet().getActiveSheet();
for(var i = 2;i<=183; i++) {
var homework = activeSheet.getRange(i,3).getValue();
if(homework = "None"){
activeSheet.getRange(i,4).setValue("N/A");
activeSheet.getRange(i,5).setValue("N/A");
} else{
activeSheet.getRange(i,4).setValue("");
activeSheet.getRange(i,5).setValue("");
}
}
}
That looks like JS.
if that is JS, you are using an assignment in the place of what you mean to be an equality check between the homework variable and the value "None"
Try if(homework === "None"){
and see if your results change towards your liking.
Disregarding language, your behavior is manifesting as if your predicate is evaluating the truthiness of an assignment to "None" and tripping your conditional. You likely want to investigate your equality operators to verify that the single equals sign is appropriate for equality comparisons. In most languages, it is not.

Why cant I add var in if statement

I'm trying to run a simple program to teach my friend to code:
var name = prompt("Whats your name");
if name = "Jason" {alert ("your name is Jason");
but for some reason it just wont let me ad the variable "name". What should I do?
I think you need
if (name === "Jason") { /*Do something */}
Rather than the single equals. Double/triple equals compares whereas single equals sets.

In a series of prompts, need to keep asking the last prompt over and over until answer

I'm creating a joke script for a site that does a series of prompts asking the user if they agree I know javascript. After they answer negative the first time, a slightly modified version of the question is restated. And finally a mocking version of the question is asked and if answered negatively will just ask it again and again until it's answered yes. I can get to the last part just fine. But I can't figure out how to get the code to go back to the last question over and over again and then switch to "thank you" response after they finally are forced to say yes. any help would be appreciated.
my code:
javascriptExample.addEventListener("click", function(){
const answer = prompt("This prompt was created using Javascript. Are you now satisfied we know Javascript? Y/n: ");
if (answer == "n") {
answerTwo = prompt("Sigh. Fine. What about now? Y/n: ");
if (answerTwo = "n") {
do{
const answerThree = prompt("Look, I can do this all day long. So, why don't you knock it off and just go ahead and say yes already, okay? Y/n: ");
} while(answerThree == "n");
} else {
alert("About time. Thank you for your cooperation and vote of confidence. I'm wasn't sure I could've kept it up forever.");
}
} else {
alert("Thanks for the vote of confidence. You're a real mensch!");
}
});
while(["A","B","C"].every(el=>!confirm(el))){}
alert("Fine");
A,B and C are your questions. Just a bit shortified...
If you wanna keep your exact answer structure:
if(confirm("A") || confirm("B")){
alert("nice of you!");
return;
}
while(true){
if(confirm("C")){
alert("youve got it");
return;
}
}
Note that variables defined using an ES6 const or let are block scoped rather than function scoped like variables defined using var.
For this reason, answerThree is no longer is scope inside the while condition and the comparison fails.
Instead, define the variable before the loop:
let answerThree;
do {
answerThree = prompt("Look, I can do this all day long. So, why don't you knock it off and just go ahead and say yes already, okay? Y/n: ");
} while (answerThree == "n");
This will lead to your desired result. Also note that you cannot reassign consts, which is why a let needs to be used here.
switch to "thank you" response after they finally are forced to say yes
This does not seems like a great idea to me because people might feel annoyed and really want to skip this question!
get the code to go back to the last question over and over again
However if you need to ask multiple questions in multiple patters than I would suggest to store questions in an array (a 2D might be more helpful)
var question = {
'1' : {
'1' : 'joke1.1',
'2' : 'joke1.2'
},
'2' : {
'1' : 'joke2.1',
'2' : 'joke2.2'
}
};
question_no=2;
question_version=1
if (answer == "n") {
question_version++;
prompt( question_no[question_no][question_version]+ Y/n: ");
}else{
question_no++;
}

Evaluate prompt value wont

I'm very new to JS - only a couple days in.
Trying to write a very basic prompt evaluated by an if statement.
When I run the code below, the user is prompted, but the statement is never evaluated by the if statement.
Any help? -- I realize the answer is probably simple and obvious, but as a SUPER beginner, what do I do?
var bool = prompt("What is an example of a boolean?");
if (typeof(bool) === "boolean") {
print("correct! that is a boolean");
print(bool) ;
};
In this case, assuming the user inputs something in the prompt, the type of the bool variable will always be a string. You'd rather check if the input compares to the string "true" or "false" etc., like this:
if (bool.toLowerCase() == "true" || bool.toLowerCase() == "false") {
...
}

Categories

Resources