I am wondering how I can call the summarizeUser function and make the canCode part log to the console as false?
Thank you in advance.
var name = 'Maya';
var age = 24;
var canCode = true;
function summarizeUser(userName, userAge, userHasHobby){
return(
`Name is ${name}, age is ${age}, can code = ${canCode}`
);
}
console.log(summarizeUser('Maya', 24, canCode));
var name = 'Maya';
var age = 24;
var canCode = true;
function summarizeUser(userName, userAge, userHasHobby){
canCode=false;
console.log( `Name is ${name}, age is ${age}, can code = ${canCode}`);
}
summarizeUser(name,age,canCode);
console.log(canCode);
I'm not really sure that I understood what you want to do. If I understood it right, then your code does not make sense ;-)
var name = 'Maya';
var age = 24;
var canCode = true;
function summarizeUser(userName, userAge, userHasHobby){
console.log( 'Name is ' + userName + ', age is ' + userAge + ', canCode = ' + userHasHobby);
// if you want to write false if user has a hobby, then you can do the following:
console.log( 'Name is ' + userName + ', age is ' + userAge + ', canCode = ' + !userHasHobby);
}
summarizeUser(name,age,canCode);
Easy, set canCode=false in the function and log it out.
Related
I have never written in JavaScript before, and I'm sure it shows. This will likely make some of you cringe but its the best way I know how to parse and check the spreadsheet data. I need an email alert to go out when the date of calibration is less than a specified cutoff date. I've tried a few configurations, some send emails for every item regardless of the date comparison and some send nothing such as the following script. Please help me make this work! I'm sure its mostly syntax or lack of specification for certain data structures.
'''
function checkCal(){
// Fetch the equipment name, calibration date, and today's date
var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1xKzfW5vX3-eDWKuKmc3R_xin4FMFKhDHaDAd7vdoaLE/edit#gid=0');
SpreadsheetApp.setActiveSpreadsheet(ss);
var calibrationDateRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Equipment").getRange("H2:H12").getValues();
var vendList = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Equipment").getRange("C2:C12").getValues();
var modelList = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Equipment").getRange("D2:D12").getValues();
var cutoffDate = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Emails").getRange("D1").getValue();
var shipped = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Equipment").getRange("I2:I12").getValues();
var notes = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Equipment").getRange("J2:J12").getValues();
var sn = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Equipment").getRange("E2:E12").getValues();
var loc = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Equipment").getRange("A2:A12").getValues();
// Check date
for (var i = 0; i < calibrationDateRange.length; i++){
var calDate = Date(calibrationDateRange[i].getValue());
//console.log('comparing' + calDate + ' to ' + cutoffDate);
if (calDate.getTime() <= cutoffDate.getTime() + shipped[i] == false){
// Fetch the email address
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Emails").getRange("B2");
var emailAddress = emailRange.getValue();
// Send Alert Email.
var message = 'Calibration due date is approaching for ' + vendList[i] + ' ' + modelList[i] + ' S/N: ' + sn[i] + ', located at ' + loc[i] + ', on ' + calDate[i] + '. Please reference the equipment spreadsheet to verify this date and the serial number of the referenced equipment. Note: ' + notes[i]; // body of email using associated variables
var subject = 'Equipment Calibration Alert';
MailApp.sendEmail(emailAddress, subject, message);
}
}
}
'''
Try it this way:
function checkCal() {
var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1xKzfW5vX3-eDWKuKmc3R_xin4FMFKhDHaDAd7vdoaLE/edit#gid=0');
const msh = ss.getSheetByName("Emails");
const esh = ss.getSheetByName("Equipment");
const evs = esh.getRange(2, 1, esh.getLastRow() - 1, esh.getLastColumn()).getValues();
const codt = new Date(msh.getRange("D1").getValue());
const emailAddress = msh.getRange(msh.getRange("B2").getValue()).getValue();
evs.forEach((r, i) => {
let calDate = new Date(r[7]);
if (calDate.valueOf() <= codt.valueOf() && !r[8]) {
let message = 'Calibration due date is approaching for ' + r[2] + ' ' + modelList[i] + ' S/N: ' + r[4] + ', located at ' + r[0] + ', on ' + calDate + '. Please reference the equipment spreadsheet to verify this date and the serial number of the referenced equipment. Note: ' + r[9];
var subject = 'Equipment Calibration Alert';
MailApp.sendEmail(emailAddress, subject, message);
}
});
}
new to coding and JS for that matter, early on I learned you can change reassign var but in the case below trying to change skyscraper to London is not working.
var city = 'New York City';
const logCitySkyline = () => {
var skyscraper = 'Empire State Building';
return 'The stars over the ' + skyscraper + ' in ' + city;
skyscraper = "London";
};
console.log(logCitySkyline());
In your function logCitySkylin the return statement breaks out of the function meaning no more code after it will be executed. There would be no point in changing the value anyways as you're never using the variable again in that function call.
var city = 'New York City';
const logCitySkyline = () => {
var skyscraper = 'Empire State Building';
return 'The stars over the ' + skyscraper + ' in ' + city;
};
console.log(logCitySkyline());
var city = 'New York City';
const logCitySkyline = (skyscraper) => {
return 'The stars over the ' + skyscraper + ' in ' + city;
};
console.log(logCitySkyline('Empire State Building'));
console.log(logCitySkyline('London'));
your function isn't returning because it isn't running any line after the return statement.
What you can do that you can pass the skyscrapper as an argument to a function.
your variable skyscraper does not change because you try to do this inside the function logCitySkyline after return. At return the function ends, so the last line of your function will not execute.
You can try this
var city = 'New York City';
const logCitySkyline = () => {
var skyscraper = 'Empire State Building';
skyscraper = "London";
return 'The stars over the ' + skyscraper + ' in ' + city;
};
console.log(logCitySkyline());
or/and this
var city = 'New York City';
const logCitySkyline = () => {
var skyscraper = 'Empire State Building';
return 'The stars over the ' + skyscraper + ' in ' + city;
};
skyscraper = "London";
console.log(logCitySkyline());
I am using Angular. I currently am using ng-repeat to display my data from a rest endpoint. I want to have the name field not be the full name but rather - First Name - Last Initial. Example:
John Doe -> John D.
You need to write a method and use it in your project.
public customName (name) {
let newName = name.split(' ');
newName = newName[0] + ' ' + newName[1].slice(0, 1) + '.';
return newName;
}
Below is an example of how the code will work.
const customName = (name) => {
let newName = name.split(' ');
newName = newName[0] + ' ' + newName[1].slice(0, 1) + '.';
console.log(newName);
}
customName('John Doe');
this is simple, just with javascript
use charAt and split
var name_full = 'John Doe'
var name_array = name_full.split(' ');
name_array[0] + ' ' + name_array[1].charAt(0) + '.'; // "John D."
I am trying to take the data from "create_name()" and write the outcome to the
<p id="name"> </p>
updating the variable name to show the new version.
however I am a bit lost in honesty and instead it returns "undefined" as the name.
the code obviously gets to this part and then fails:
Writedata(create_name); // now write our username
menu_function();
}
function Writedata(nameUser){
document.getElementById("name").innerHTML='hello ' + nameUser + ' below are your options'; // write the username.
}
You can see the code for the entire section I am trying to make work here.
I hope I explained clearly enough.
Any Questions? feel free to ask, all help is appreciated!
init(); // lets start the inital func
function init(){ // Do some asking shit
var name = prompt('hello, what is your name?'); // ask for the name
document.write('<p id="name">hello ' + name + ' </p>');
document.write('<p id="user" >1 - create a username</p>');
document.write('<p id ="play" >2 - Play Quiz<p>');
document.write('<p id="reload">3 - quit and reload<p>');
Writedata(name); // tell our witer to write the name
}
function menu_function() {
var choice = prompt("hello " + name + " please select an option from the list to your left");
switch (choice) {
case "1": opt1_function(); break;
case "2": opt2_function(); break;
case "3": reload_method(); break;
default: menu_function(); break;
}
}
menu_function();
function opt1_function() {
alert(name + " you have selected option 1");
create_name();
}
function create_name() {
var forename = prompt("what is your forename?");
var surname = prompt("what is your surname?");
var username = alert("hello " + forename + " " + surname);
var string1 = forename.substring(0 , 1);
var create_name = alert(string1 + surname);
Writedata(create_name); // now write our username
menu_function();
}
function Writedata(nameUser){
document.getElementById("name").innerHTML='hello ' + nameUser + ' below are your options'; // write the username.
}
stop assigning alert() function return values to variables, alert() has no return value look at this alert definition
change this line
var create_name = alert(string1 + surname);
to
alert(string1 + surname);
var create_name = string1 + surname;
Changed the data within the create_name() function to this:
function create_name() {
var forename = prompt("what is your forename?");
var surname = prompt("what is your surname?");
var username = alert("hello " + forename + " " + surname);
var string1 = forename.substring(0 , 1);
var create_name = string1 + surname;
Writedata(create_name); // now write our username
menu_function();
}
function Writedata(nameUser){
document.getElementById("name").innerHTML='hello ' + nameUser + ' below are your options'; // write the username.
}
and now it works!
I apologize in advance if I'm vague or my code is difficult to understand, I'm still learning this stuff. I'm trying to display information that is stored within an array. I want to display this information when a button is clicked and when it is clicked again, the next index in the array displays its information..
I need help setting up a function that advances to the next index of the array. Thanks!
(function(){
var students =[ //array of information
{name:'john',
address:{
address:'821 Imaginary St',
city:'Chicago',
state:'Il'},
gpa:[4.0,3.5,3.8]},
{name:'jim',
address:{
address:'127 fake Rd',
city:'Orlando',
state:'Fl'},
gpa:[2.5,3.3,3.6]}];
var redBut = document.querySelector('.buttonred');
redBut.onclick = getInfo;
var count = 0;
function getInfo(){
var stn = students[0];
if(count<3){
count++;
document.getElementById('name').innerHTML = 'Name: ' + stn.name; //this is what is to be displayed when the button is clicked
document.getElementById('address').innerHTML = 'Address: ' + stn.address.address + " " + stn.address.city + ", " + stn.address.state;
document.getElementById('gpa').innerHTML = 'GPA: ' + stn.gpa[0] +", " + stn.gpa[1] + ", " + stn.gpa[2];
document.getElementById('date').innerHTML = 'Date: ' + d.toLocaleDateString();
document.getElementById('gpaavg').innerHTML = 'Average GPA: ' + gpas;
}
}
I think you want: var stn = students[count];
And not: var stn = students[0];
(DOH!)