Basic Java Script Syntax

19 flashcards · Programming Languages · by Antonio Anaya Tapia

  1. Name at least three of the primitive data types in Javascript.

    boolean, number, string

  2. Name one non-primitive data type.

    object

  3. What is the difference between a function parameter and a function argument?

    The parameters are the aliases for the values that will be passed to the function. The arguments are the actual values. var foo = function( a, b, c ) {}; // a, b, and c are the parameters foo( 1, 2, 3 ); // 1, 2, and 3 are the arguments

  4. Write at least 3 of the comparison operators in Javascript.

    ===, >, <=

  5. Name 3 keywords that can be used to declare a variable.

    let const var

  6. Create an array called `myArray`.

    const myArray = [ ];

  7. Create an object called `myObject`.

    const myObject = {};

  8. Write two functions: one using the `fat arrow` syntax and one using the `function` keyword syntax.

    const func1 = () => {} const func2 = function() {}

  9. Write a function that returns the number 5.

    const func = () => { return 5; }

  10. Write a function that takes two numbers and returns the sum of them.

    const func = (num1, num2) => { return num1 + num2; }

  11. Write a function that takes an array and returns the first index of the array.

    const func = (array) => { return array[0]; }

  12. Write function that takes an array of numbers and returns the sum of all of the numbers.

    const func = (array) => { return array.reduce((a, b) => a + b, 0) }

  13. Create an object with two properties and show two different ways to access them.

    const myObject = { propertyOne: 1, propertyTwo: 2 } myObject.propertyOne; myObject[propertyTwo];

  14. Create a function that takes two numbers and returns the number of times that the first number is divisible by the second number.

    const func = (num1, num2) => { return Math.floor(num2 / num1); }

  15. List three array methods.

    map() reduce() includes()

  16. In the case of the roman numeral converter when while (remainder) { for (i = 0; i < numbers.length; i++) { if (remainder >= numbers [i] ) { remainder -= numbers [i]; romanNumeralString += romanNumeralArray [i]; break } } } return romanNumaralString; } What does the break statement do ?

    reset the loop to start at index 0 again

  17. In the case of the roman numeral converter when while (remainder) { for (i = 0; i < numbers.length; i++) { if (remainder >= numbers [i] ) { remainder -= numbers [i]; romanNumeralString += romanNumeralArray [i]; break } } } return romanNumaralString; } What does the subtraction assignment (-=) do ?

    subtracts the value of the right operand from a variable and assigns the result to the variable

  18. In the case of the roman numeral converter when while (remainder) { for (i = 0; i < numbers.length; i++) { if (remainder >= numbers [i] ) { remainder -= numbers [i]; romanNumeralString += romanNumeralArray [i]; break } } } return romanNumaralString; } What does the addition assignment (+=) do ?

    adds the value of the right operand to a variable and assigns the result to the variable

  19. In the case of the rot13 problem function rot13(str) { /*The words are shifted from the 13th letter so A = N, B = O we create two strings that are defined as encoded which is the regular alphabet and decoded which is the shifted alphabet*/ let encoded = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' let decoded = 'NOPQRSTUVWXYZABCDEFGHIJKLM' //We use the replace method in order to replace all the decoded values into the decoded values, the key is to use a range of values a-z. return str.replace(/[a-z]/gi, letter…

    you replace.(/[a-z]/gi, you replace a range of values in a global instance.