Learn JavaScript on the Now Platform: Lesson 8 - Data type conversions

preview_player
Показать описание
Lesson 8 shows you how to convert an integer to a string, a string to an integer, and how to determine what type of variable you have if you're not sure.

Рекомендации по теме
Комментарии
Автор

Question goes:

1. Create 3 string variables.
2. print the length of each string.
3. Concatenate not using space but by using new line character and save that to a new variable
4. Then print the length of that new variable.

gulshanzahoor
Автор

var a = 'Sarah'
var b = 'Layla';
var c = 'Jaffar';




console.log('Length of a = ' + a.length);
console.log('Length of b = ' + b.length);
console.log('Length of c = ' + c.length);



var newLine = (a.length + '\n' + b.length + '\n' + c.length);
console.log(newLine);
console.log(newLine.length);

Output is 5 when we concatenate all the lengths of a, b, c, and the 2 new line characters.

LXX
Автор

var a ='Naveen';
var b ='Prince';
var c ='Singh';

gs.log( "lenght of name a=" + a.length);
gs.log( "lenght of name b=" + b.length);
gs.log( "lenght of name c=" + c.length);

var concat=a +' '+ b +' '+ c;
gs.log("Concatenation of all the strings =" + concat);
var concat1= a + '\n' +b + '\n' +c;
gs.log(" Concatenation of strings in different lines =\n" + concat1 );
gs.log("length of the concatenated string =" + concat.length);

NaveenSingh-srxb
Автор

Thanks Chuck for guiding us,
I wrote in pure javascript but entirely concept is same




<script>
var stra="ramesh"
console.log(" Lenght od stra "+stra.length);
var strb=" Suresh";
console.log(" Lenght of strb is :"+ strb.length);
var strc=" Sumansh";
console.log(" Lenght of strb is :"+ strb.length);

var strd=(stra +"\n"+ strb +"\n"+ strc);
var stre=(stra + strb + strc);
console.log("Lenght of strd:"+strd);
console.log("Lenght of strd:"+strd.length);
console.log("Lenght of stre:"+stre.length);
</script>




Out put :
Lenght od stra 6
unte.html:28 Lenght of strb is :7
unte.html:30 Lenght of strb is :7
unte.html:34 Lenght of strd:ramesh
Suresh
Sumansh
unte.html:35 Lenght of strd:23
unte.html:36 Lenght of stre:21

sumanbattu
Автор

Best instructor EVER! Thank you so much Chuck.

kimaly
Автор

// Define Variables
var fName = 'Stephen';
var wName = 'Melody';
var lName = 'Crocker';

var jammedName = fName + wName + lName;
var retName = fName + '\n' + wName + '\n' + lName;

gs.log('My first name is ' + fName + '. and it is ' + fName.length + ' characters long');
gs.log('My wife\'s name is ' + wName + '. and it is ' + wName.length + ' characters long');
gs.log('Our last name is ' + lName + '. and it is ' + lName.length + ' characters long');

gs.log('If you cram all of them together you will get ' + jammedName + ' which is ' + jammedName.length + ' characters long.');
gs.log('Now we can print them on separate lines like this \n' + retName);
gs.log('Interesting how the length is not what we expected it is ' + retName.length + ' in length. 2 more then the jammedName variable');

// Output
*** Script: My first name is Stephen. and it is 7 characters long
*** Script: My wife's name is Melody. and it is 6 characters long
*** Script: Our last name is Crocker. and it is 7 characters long
*** Script: If you cram all of them together you will get StephenMelodyCrocker which is 20 characters long.
*** Script: Now we can print them on separate lines like this
Stephen
Melody
Crocker
*** Script: Interesting how the length is not what we expected it is 22 in length. 2 more then the jammedName variable

winkguy
Автор

//Declare 3 string variables
var a = 'this';
var b = 'or';
var c = 'that';

//Print length of the 3 string variables
/gs.info(a.length);
gs.info(b.length);
gs.info(c.length);

//Concatonate the 3 variable using a new line character
gs.info ('length of a:' + ' ' + a.length);
gs.info ('length of b:' + ' ' + b.length);
gs.info ('length of c:' + ' ' + c.length);

//Save length to a new variable
var newVar = (a + "\n" + b + "\n" + c);

//Print length of that new variable
//gs.info (newVar);
gs.info('length of new variable:' + ' ' + newVar.length);

KaliAlexander
Автор

<script>var name = 'Bosun';
var age = 'Twenty';
var work = 'ServiceNow';



gs.info(name.length);
gs.info(age.length);
gs.info(work.length);

var comb = name + '\n' + age + '\n' + work;

gs.info(comb.length);<script>




Output
Script: 5
Script: 6
Script: 10
Script: 23

olatubosunogundimu
Автор

var x = "cat";
var y = "dog";
var z = "elephant";

gs.print("length of x:" + " " + x.length);
gs.print("length of y:" + " " + y.length);
gs.print("length of z:" + " " + z.length);

var a = x.concat(y, z);
var b = (x + "\n" + y + "\n" + z);

gs.print(a.length);
gs.print(b.length);

Results:
*** Script: length of x: 3
*** Script: length of y: 3
*** Script: length of z: 8
*** Script: 14
*** Script: 16

Hatellen
Автор

var firstName = 'Raju'
var middleName = 'Singh'
var lastName = 'Sardar'

var fullName = (firstName +'\n' + middleName +'\n' + lastName);
gs.info('The length of my Full Name with new line variables is '+ fullName.length);
gs.info('The length of my Full Name with no new line is '+


Output: Script execution history and recovery available here
*** Script: The length of my Full Name with new line variables is 17
*** Script: The length of my Full Name with no new line is 15

sardarsa
Автор

var a = 'Aman'
var b= 'kumar'
var c= 'Singh'

var d = a+'\n'+b+'\n'+c;

var lenA = a.length;
var lenB = b.length;
var lenC = c.length;

var lenTotal =d.length;


gs.info(' Length of a =' +lenA+'\n'+' Length of b =' +lenB+'\n'+' Length of c =' +lenC+ '\n' + ' Total Length =' +lenTotal);


Output:


*** Script: Length of a =4
Length of b =5
Length of c =5
Total Length =16

Amanu
Автор

Script:
var nr1 = 'No access';
var nr2 = "All smiles on my face";
var nr3 = '5';

gs.info('length of Nr1: '+nr1.length);
gs.info('length of Nr2: '+nr2.length);
gs.info('length of Nr3: '+nr3.length);

var nr4= nr1 + '\n' + nr2 + '\n' + nr3;

gs.info('Concatenated string:\n' + nr4);
gs.info('Length of concatenated string: ' + nr4.length);




Result:
*** Script: length of Nr1: 9
*** Script: length of Nr2: 21
*** Script: length of Nr3: 1
*** Script: Concatenated string:
No access
All smiles on my face
5
*** Script: Length of concatenated string: 33

ljukzoi
Автор

var string1 = 'something';
var string2 = 'nothing';
var string3 = 'anything';

gs.info('length of string1 = ' + string1.length);
gs.info('length of string2 = ' + string2.length);
gs.info('length of string3 = ' + string3.length);

var string4 =
gs.info('total length of all strings combined with a new line = ' + (string4.length));

hamadbaseer
Автор

I decided not to do the length of each string but just added the length of all the strings together


var firstName = 'Andre';
var middleName = 'Jermaine';
var lastName = 'Snowden';

var name = firstName + '\n' + middleName + '\n ' + lastName;
gs.info(name);



gs.info('length of the whole name=' + name.length);


out put:

*** Script: Andre
Jermaine
Snowden
*** Script: length of the whole name=23

snowdenfam
Автор

I got the same result using new lines that I got with spaces.

var x = "xray";
var y = "yahtzee";
var z = "zoom";

gs.info(x.length);
gs.info(y.length);
gs.info(z.length);

gs.print("length of x:" + " " + x.length);
gs.print("length of y:" + " " + y.length);
gs.print("length of z:" + " " + z.length);

var usingSpaces = x + " " + y + " " + z;


var sumStr = x + "\n" + y + "\n" + z;
gs.print(sumStr.length);

*** Script: 4
*** Script: 7
*** Script: 4
*** Script: length of x: 4
*** Script: length of y: 7
*** Script: length of z: 4
*** Script: 17
*** Script: 17

randomly-genrated
Автор

var name1="car";
var name2="bike";
var name3="truck";
gs.log( "lenght of name 1=" +' ' + name1.length);
gs.log( "lenght of name 2=" +' ' + name2.length);
gs.log( "lenght of name 3=" +' ' + name3.length);
var concat=name1 + name2 + name3;
gs.log("concatenation of all the string =" + concat);
var concat1= name1 + '\n' +name2 + '\n' +name3;
gs.log(" concatenation of strings in different lines =\n" + concat1 );
gs.log("length of the concatenated string =" + concat.length);
gs.log("length of the concatenated string of diff lines =" + concat1.length);

abhayagarwal
Автор

var a = 'gopi';
var b = 'chandra';
var c = 'kumar'
var d = a + b + c;
gs.info(d);
gs.info(typeof d);
gs.info(d.length);
var e = 'gopi\nchandra\nkumar'
gs.info(e);
gs.print(e.length)

kumar-egkf
Автор

var newVar1 = "Firstname";
var newVar2 = "Lastname";
var newVar3 = "City";
var newVar4 =
var n1 = newVar1.length;
var n2 = newVar2.length;
var n3 = newVar3.length;
var n4 = newVar4.length;

gs.info("length of n1 = "+ n1);
gs.info("length of n2 = "+ n2);
gs.info("length of n3 = "+ n3);
gs.info("length of n4 = "+ n4);

Script Result
*** Script: length of n1 = 9
*** Script: length of n2 = 8
*** Script: length of n3 = 4
*** Script: length of n4 = 23 (9+8+4 = 21 + 2 is for new lines = 23)

satyabratpatra
Автор

var v1 = 'Touqeer';
var v2 = 'Hamza';
var v3 = 'Awais';

gs.info('Length of v1 is '+v1.length);
gs.info('Length of v2 is '+v2.length);
gs.info('Length of v3 is '+v3.length);

vF = (v1)+'\n'+(v2)+'\n'+v3;
gs.info(vF.length);

//output
*** Script: Length of v1 is 7
*** Script: Length of v2 is 5
*** Script: Length of v3 is 5
*** Script: 19

touqeerahmad
welcome to shbcf.ru