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());
Related
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.
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 was having this problem when trying to pull from firebase. My database structure as such:
I am trying to pull the merchantName and its relevant branches details:
function getAllMerchant(){
var query = firebase.database().ref('merchants');
return query.once('value').then(data => {
var result = [];
data.forEach(snapshot => {
var merchantData = snapshot.val();
var merchantName = merchantData.merchantName;
var branchName = merchantData.branches.branchName;
var branchAddress = merchantData.branches.branchAddress;
console.log('check ' + merchantName + ' ' + branchName + ' ' + branchAddress);
result.push({merchantName: merchantName, branchName: branchName, branchAddress: branchAddress});
});
return result;
});
resolve(result);
}
However, when I printed out the branchName and branchAddress, I am getting undefined. I managed to print out merchantName though. Any ideas?
Thanks!
You're not iterating over the branches of the merchant.
data.forEach(merchantSnapshot => {
var merchantData = snapshot.val();
var merchantName = merchantData.merchantName;
console.log('check ' + merchantName);
merchantSnapshot.child("branches").forEach(brancheSnapshot => {
var branchName = brancheSnapshot.val().branchName;
var branchAddress = brancheSnapshot.val.branchAddress;
console.log(' ' + branchName + ' ' + branchAddress);
});
});
I'm trying to check if the team contains one of the following strings in the array. If yes then remove it. Why is it not doing this?
function changeName(name) {
var team = name;
var removearray = ['.CS', ' Dota2', '-Dota2', ' Esports', 'eSports', ' Tactics', 'DotCom', ' DotA2', ' - LoL', '-LoL', ' Carry', 'Â', ' LoL'];
removearray.forEach(function( word ) {
team = team.replace( new RegExp( word, 'g' ), '' );
});
return team;
}
Please note that "forEach" is not supported in certain browsers such as IE8.
Also, please consider the following implementation:
function changeName(name) {
var removearray = ['.CS', ' Dota2', '-Dota2', ' Esports', 'eSports', ' Tactics', 'DotCom', ' DotA2', ' - LoL', '-LoL', ' Carry', 'Â', ' LoL'];
return team = removearray.reduce(function(previous, current) {
return previous.replace( new RegExp( current, 'g' ), '' );
}, name);
}
The "reduce" method available in the Array Prototype in Javascript is a very nice way of doing tasks such as this one.
function changeName(name) {
var team = name;
var removearray = ['.CS', ' Dota2', '-Dota2',
' Esports', 'eSports', ' Tactics', 'DotCom',
' DotA2', ' - LoL', '-LoL', ' Carry', 'Â', ' LoL'];
for(var i = 0; i < removearray.length; i++){
while(team.indexOf(removearray[i]) > -1) {
var index = team.indexOf(removearray[i]);
if (index > -1) {
team = team.substr(0, index) +
team.substr(index + removearray[i].length);
}
}
}
return team;
}
var one = changeName('John'); // returns 'John'
var two = changeName('John Dota2 Doe'); // returns 'John Doe'
var three = changeName('John Dota2 Doe Dota2.CS') // returns 'John Doe'
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!)