Posts

Showing posts from January, 2023

readLine code

  let fs = require ( "fs" ); let data = fs . readFileSync ( 0 , 'utf-8' ); let idx = 0 ; data = data. split ( ' \n ' ); function readLine () { idx ++ ; return data[idx - 1 ]. trim (); }

Review

INTRODUCTION TO JAVASCRIPT Review Let’s take one more glance at the concepts we just learned: Data is printed, or logged, to the console, a panel that displays messages, with  console.log() . We can write single-line comments with  //  and multi-line comments between  /*  and  */ . There are 7 fundamental data types in JavaScript: strings, numbers, booleans, null, undefined, symbol, and object. Numbers are any number without quotes:  23.8879 Strings are characters wrapped in single or double quotes:  'Sample String' The built-in arithmetic operators include  + ,  - ,  * ,  / , and  % . Objects, including instances of data types, can have properties, stored information. The properties are denoted with a  .  after the name of the object, for example:  'Hello'.length . Objects, including instances of data types, can have methods which perform actions. Methods are called by appending the object or instance with ...

Built-in Objects(Math objects)

  INTRODUCTION TO JAVASCRIPT Built-in Objects In addition to  console , there are other  objects built into JavaScript . Down the line, you’ll build your own objects, but for now these “built-in” objects are full of useful functionality. For example, if you wanted to perform more complex mathematical operations than arithmetic, JavaScript has the built-in  Math  object. The great thing about objects is that they have methods! Let’s call the  .random()  method from the built-in  Math  object: console . log ( Math . random ()); // Prints a random number between 0 and 1 In the example above, we called the  .random()  method by appending the object name with the dot operator, the name of the method, and opening and closing parentheses. This method returns a random number between 0 (inclusive) and 1 (exclusive). To generate a random number between 0 and 50, we could multiply this result by 50, like so: Math . random () *  ...

Methods(*Important! examples string.toUpperCase();, example string.trim();)

  INTRODUCTION TO JAVASCRIPT Methods Remember that  methods  are actions we can perform. Data types have access to specific methods that allow us to handle instances of that data type. JavaScript provides a number of string methods. We  call , or use, these methods by appending an instance with: a period (the dot operator) the name of the method opening and closing parentheses E.g.  'example string'.methodName() . Does that syntax look a little familiar? When we use  console.log()  we’re calling the  .log()  method on the  console  object. Let’s see  console.log()  and some real string methods in action! console . log ( 'hello' . toUpperCase ()); // Prints 'HELLO' console . log ( 'Hey' . startsWith ( 'H' )); // Prints true Let’s look at each of the lines above: On the first line, the  .toUpperCase()  method is called on the string instance  'hello' . The result is logged to the console. This method returns a...

Properties( *Important! examples string.length)

  INTRODUCTION TO JAVASCRIPT Properties When you introduce a new piece of data into a JavaScript program, the browser saves it as an instance of the data type. All data types have access to specific properties that are passed down to each instance. For example, every string instance has a property called  length  that stores the number of characters in that string. You can retrieve property information by appending the string with a period and the property name: console . log ( 'Hello' . length ); // Prints 5 The  .  is another operator! We call it the  dot operator . In the example above, the value saved to the  length  property is retrieved from the instance of the string,  'Hello' . The program prints  5  to the console, because  Hello  has five characters in it. Instructions: 1. Use the  .length  property to log the number of characters in the following string to the console: 'Teaching the world how to code' ...

String Concatenation

  INTRODUCTION TO JAVASCRIPT String Concatenation Operators aren’t just for numbers! When a  +  operator is used on two strings, it appends the right string to the left string: console . log ( 'hi' +  'ya' ); // Prints 'hiya' console . log ( 'wo' +  'ah' ); // Prints 'woah' console . log ( 'I love to ' +  'code.' ) // Prints 'I love to code.' This process of appending one string to another is called  concatenation . Notice in the third example we had to make sure to include a space at the end of the first string. The computer will join the strings exactly, so we needed to make sure to include the space we wanted between the two strings. console . log ( 'front ' +  'space' ); // Prints 'front space' console . log ( 'back' +  ' space' ); // Prints 'back space' console . log ( 'no' +  'space' ); // Prints 'nospace' console . log ( 'middl...