Angular seperate first name last initial - javascript

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."

Related

How do I make the boolean produce false for canCode?

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.

Get full word containing a specific element

Having the reference to a specific DOM element (e.g. <mark>), how can we get the full word containing that element?
For example :
H<mark>ell</mark>o Wor<mark>l</mark>d, and He<mark>llo</mark>, <mark>Pluto</mark>!
I expect to get the following output :
First <mark>: Hello
Second: World
Third: Hello
Fourth: Pluto
var $marks = $("mark");
var tests = [
"Hello",
"World",
"Hello",
"Pluto",
];
function getFullWord($elm) {
// TODO: How can I do this?
// This is obviously wrong.
return $elm.html();
}
var $marks = $("mark");
tests.forEach(function(c, i) {
var word = getFullWord($marks.eq(i));
if (word !== c) {
alert("Wrong result for index " + i + ". Expected: '" + c + "' but got '" + word + "'");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
H<mark>ell</mark>o Wor<mark>l</mark>d, and He<mark>llo</mark>, <mark>Pluto</mark>!
If you need fast and compact code (one-liner), try this:
var $marks = $('mark');
$marks.each(function() {
var wholeWord = (this.previousSibling.nodeValue.split(' ').pop() +
this.textContent +
this.nextSibling.nodeValue.split(' ')[0]
).replace(/[^\w\s]/gi, '');
});
JSFiddle (with logging into console and comments)

Check if a string contains one of the strings in array

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'

Two Contents Added With Space

I'm trying to add contents from two DIVs and inject them into an input field. The following works except there is no space in between the values entered.
With the code below the values in the text field is: JohnDoeAccountant
I'm looking for an output in the text field of: John Doe Accountant.
How could I ensure there is a space in between the values of the outputs?
var output1 = document.getElementById("firstname").innerHTML;
var output2 = document.getElementById("lastname").innerHTML;
var output3 = document.getElementById("job").innerHTML;
document.getElementById("user-submitted-tags").value = output1 + output2 + output3;
Try
document.getElementById("user-submitted-tags").value = output1 + ' ' + output2 + ' ' + output3;
....value = [output1, output2, output3].join(" ");
Or cut out some repetition:
document.getElementById("user-submitted-tags").value =
["firstname","lastname","job"].map(function(id) {
return document.getElementById(id).innerHTML;
}).join(" ")
Or like this:
document.getElementById("user-submitted-tags").value =
["firstname","lastname","job"].reduce(function(s, id) {
return s + " " + document.getElementById(id).innerHTML;
}, "")

Appending variables to a string in javascript

I am using prototype in my application but I am not sure how to add this correctly. Basically I have the following function and I need to construct the href of an anchor from which I already have the reference to a series of appended values
MyJavascriptClass.prototype.init = function() {
this.ToDate = $(this.Prefix + 'ToDate');
this.FromDate = $(this.Prefix + 'FromDate');
}
so in the following function I need to add those as parameters in the url attribute
MyJavascriptClass.prototype.btnClicked = function(evt) {
this.lnkShowLink.setAttribute('href', 'MyWebpage.aspx?StartDate=7/18/2012&EndDate=1/19/2012');
}
How can i do something like 'MyWebPage.aspx?StartDate=this.ToDate&EndDate=this.FromDate' ? Any help would be appreciated.
If you are using jquery, and $(this.Prefix + 'ToDate') and $(this.Prefix + 'FromDate') represent fields that contain values, then you can do this:
MyJavascriptClass.prototype.btnClicked = function(evt) {
this.lnkShowLink.setAttribute('href', 'MyWebpage.aspx?StartDate=' + this.ToDate.val() + '&EndDate=' + this.FromDate.val() + '');
}
It is difficult to tell from your code what they represent, and why you have them wrapped in $(..).
If ToDate and FromDate contain the two date values, then this should work...
'MyWebPage.aspx?StartDate=' + this.ToDate + '&EndDate=' + this.FromDate
If you don't know every properties:
var properties = [];
for(var i in this)
if(this.hasOwnProperty(i))
properties.push(i+'='+this[i]);
var url = 'MyWebPage.aspx?'+properties.join('&');
var string = "My name is: ",
name = "Bob",
punctuation = ".",
greeting = string + name + punctuation;
Or
var User = { name : "Bob", age : 32, sign : "Leo" },
welcome = "Hi, I'm " + User.name + ", and I'm " + User.age + " years old, I'm a " + User.sign + ", and I enjoy long walks on the beach.";

Categories