JavaScript Flashcards

1
Q

What is the result of below

“apple”.length

A

“5”

Remember, index does not matter here. It just count the number of words

Also, .length method cannot be applied in array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how many words is counted if there is \n inside a string

A

1 only, not two

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

in Javascript, what will console.log (0.2+0.4) result in?

A

0.6000000000000001, the reason is computer in binary digits, 在電腦的角度, 這些小數點是除不盡的. 所以衹要電腦是計算小數點, 都會有不精准的情況, 所以在計算價錢的時候要特別注意

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

const sentence = “Teck Academy is good”

console.log(sentence.substring(sentence.length-4))

What is the purpose of substring. what will come out

A

the argument in substring is that what LAST number of index of string the computer will show

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is NaN

A

“not a number” that’s a bug

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

how to use math method in Javascipt

A

console.log(Math.max(1,2,3,4,5))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

how to eliminate digitial number or usye the digitial number to round up to interger

A

Math.floor(1.xxx) = 1

Math.ceiling

Math.round

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Before Node.js is invented, where was Javascript run?

A

only on web brower

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

when is camel case format used? And how?

A

We normally name function using the camel case format. The name normally starts with a verb because a function is normally a function on its own.

// Correct
function sumOfArray(){
}
// Incorrect
function sum_of_array(){
}
// Incorrect
function sumofarray(){
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference between expression and statement?

A

The correct answer is: if-else block are statements while ternary operators condition ? true-value: false-value is expression.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is the symbol of END operator in Boolean. What are OR and NOT

A

&&, ||, !

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

comparsion operator >=口號, and !=

A

大於或等於

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

can 15% be 0.015 in Javascript?

A

NO. It does not exist and give error

Uncaught c:\Users\C\Documents\tacky_js_fund\001.js:69

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

can I declare x = 1, two times

A

no, the second time just declare “x = 1”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

how to add item in array

A

array.push(XXX), it will go to the last index of the array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how to remove the last item in array

A

array(/llzlazlease z

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

two methods that can access the value in object thought key? What are they?

A

chris = {‘height’ : 180}

chris[‘height’]

OR

chris.height

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

SPECIAL case in the key of object,

if in the key of object, there is for exmaple,
“living place” or “living-place”

can I type chris.living-place?

A

then you cant use

chris.living-place, because it just cant, computer will think it is minus sign

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

can you add a unnecessary comma after the last item in array and object

A

yes you can, there wont be error, also this is convenient for you to copy and paste paste

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

in object, what if you want to delete one key with its value?

A

you just write

delete chris[key]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

保持隊形

A

就算空了也要給個空的array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

what is good structure if I want to limit the while loop to the number of array?

A

while (n< nameList.length) THIS

n = 0
while (n< nameList.length){
    if (nameList[n].height > 170){
        console.log(nameList[n].name)
    }
    n += 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

arrry add and delete things?

A
// To add a new element to the end of the array
arr.push(5);
// To remove a element from the end of the array
arr.pop();
// To add a new element to the start of the array
arr.unshift(0);
// To remove a element from the start of the array
arr.shift();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

function return發生多少次

A

return 只會發生一次, 其他return就會被忽略

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

what should be the the name of assignement to array

A

arr

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

how to loop object>

A

for in loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

what scope is let and const, what scope is var

A

let and const are block scope, only effective inside a block, var is function scope, effective everywhere, except in a function

It’s short-coming is it let you define more than once.

for example,

var x = 2
var x = 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

how to see if there is an element in a array?

A

Example
Check if an array includes “Mango”:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.includes("Mango")   // Returns true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

how to set interval?

A

setIntervel(function(){

}, 1000)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Is this code ok?

let x = 2;
let x = 3;
const y = 2;
const y = 3;
A

No, it will say Uncaught

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

How would you define scope of variable?

A

Scope determines the accessibility (visibility) of variables.

JavaScript has 3 types of scope:

  1. Block scope
  2. Function scope
  3. Global scope
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Before ES6 (2015), what scope does JavaScript only had?

What did ES6 introduced?

A
  1. Global Scope
  2. Function Scope

ES6 introduced “let” and “const”, which provide BLOCK SCOPE.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

What is a block scope in JavaScript?

A

Variables declared inside a { } block cannot be accessed from outside the block:

{
  let x = 2;
}
// x can NOT be used here
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

What scope if one declares:

var x = 2;

What does it mean

A

Variables declared with the var keyword can NOT have block scope.

Variables declared inside a { } block can be accessed from outside the block.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

What is local scope and what does it mean?

A

Variables declared within a JavaScript function, become LOCAL to the function.

// code here can NOT use carName

function myFunction() {
  let carName = "Volvo";
  // code here CAN use carName
}

// code here can NOT use carName

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

What is local scope and what does it mean?

A

Variables declared within a JavaScript function, become LOCAL to the function.

Local variables have Function Scope:
They can only be accessed from within the function.

// code here can NOT use carName

function myFunction() {
  let carName = "Volvo";
  // code here CAN use carName
}

// code here can NOT use carName

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

Since local variables are only recognized inside their functions, can we use the same name in different functions?

A

Yes, we can use the same name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

What happened after a local variable is used in a function?

A

Local variables are created when a function starts, and deleted when the function is completed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

A variable declared outside a function, becomes __________ ?

A

GLOBAL

let carName = "Volvo";
// code here can use carName
function myFunction() {
// code here can also use carName
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

Local variables have _______?

A global variable has _______?

A

Local variables have Function Scope:
They can only be accessed from within the function.

A global variable has Global Scope:
All scripts and functions on a web page can access it.

41
Q

What will a variable become automatically Global?

A

If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable.

This code example will declare a global variable carName, even if the value is assigned inside a function.

myFunction();

// code here can use carName

function myFunction() {
  carName = "Volvo";
}
42
Q

Why we should not create global variable unless we intend to?

A

Do NOT create global variables unless you intend to.

Your global variables (or functions) can overwrite window variables (or functions).
Any function, including the window object, can overwrite your global variables and functions.

43
Q

What is the lifetime of JavaScript variable

A

The lifetime of a JavaScript variable starts when it is declared.

Function (local) variables are deleted when the function is completed.

In a web browser, global variables are deleted when you close the browser window (or tab).

44
Q

What’s the error below?

Uncaught TypeError:
Assignment to constant variable at VM968 pen.js:2

A

The code is below:

const Pi = 3.1415
Pi = 1

variable declared as const should not be change.

45
Q

var vs let

A

var has functional scope while let has block scope.

What does block scope means? E.g. for loop

if “let variable” is defined in for loop, it can only be accessed in for loop, not BEFORE and AFTER it.

if “var variable” is defined in for loop, it can be accesssd in the entire program. If you call it BEFORE the for loop, it will say “undefined” (the program actually RECOGNISED it”) If you ccll it after the for loop, then it can be accessed.

46
Q

Can this code run?

function square(number) {
    return number * number;
}

console.log(number)

A

NO

> Uncaught ReferenceError: Number is not defined

47
Q

Can this code run?

function myFun() {
    console.log(someVar)
}

myFun()

var someVar = “Shoes”

A

YES. but with some problem

“undefined”

Because the program can still recognize it EVEN the variable is declared after

48
Q
function myFun() {
    console.log(someVar)
}

var someVar = “Shoes”

myFun()

A

YES

“shoes”

49
Q

if I use var and let INSIDE a function, do the variables have a global scope?

A

NO, they have functional scope in a FUNCTION.

50
Q

Can a function inside a function be called outside?

A

NO

51
Q

if a function is BELOW the code that is calling it, can it run??? e.g.

myFun()

function myFun(){
      console.log("hi nubs")
}
A

YES, without any problem.

a function declaration is hoisted which means no matter where it is in the code it’s kind of brought to the top of the code

52
Q

What does below code show?

console. log( 1 == “1”
console. log( 1 === “1” )

A

true
false

What is == in JavaScript?
Double equals (==) is a comparison operator, which transforms the operands having the same type before comparison.

So, when you compare string with a number, JavaScript converts any string to a number. An empty string is always converts to zero. A string with no numeric value is converts to NaN (Not a Number), which returns false.

What is === in JavaScript?
=== (Triple equals) is a strict equality comparison operator in JavaScript, which returns false for the values which are not of a similar type. This operator performs type casting for equality. If we compare 2 with “2” using ===, then it will return a false value.

53
Q

When using “this” in Node JS, what will happen in below code? (that is the only code)

function dcode() {
console.log(this)
}
dcode();

A

this will refer to the global window object. Below is what happened.

Object [global] {
global: [Circular *1],
clearInterval: [Function: clearInterval],
clearTimeout: [Function: clearTimeout],
setInterval: [Function: setInterval],
setTimeout: [Function: setTimeout] {
[Symbol(nodejs.util.promisify.custom)]: [Getter]
},
queueMicrotask: [Function: queueMicrotask],
clearImmediate: [Function: clearImmediate],
setImmediate: [Function: setImmediate] {
[Symbol(nodejs.util.promisify.custom)]: [Getter]
}
}

54
Q

What is the definition of “this” according to “Programming with Mosh”?

A

The object that is executing the current function.

55
Q

if the function is part of an object, what do we call it?

A

method

56
Q

What is the inside the object?

const hot_girl = {
    cup: 'f'
}

hot_girl.height = ‘170 cm’

A

{cup: ‘f’, height: ‘170 cm’}

57
Q

What is inside the object?

const hot_girl = {
    cup: 'f'
}

hot_girl.height = ‘170 cm’

A

{cup: ‘f’, height: ‘170 cm’}

58
Q

what does the array slice method do?

A

The array slice method returns a shallow copy of a portion of an array meaning that it won’t modify the original array on which is a called upon

59
Q

What’ll be the result of number 2?

const numbers = [1, 2, 3, 4, 5]

const number2 = numbers.slice(1,4 )

A

[2,3,4]

because

array.slice[FIRST INDEX WANTED, THE INDEX YOU STOP WANTING, this index is not included)

60
Q

What’ll be the result of number 2?

const numbers = [1, 2, 3, 4, 5]

const number2 = numbers.slice()
const number3 = numbers.slice(0)
const number4 = number.slice(2,)
A

[1,2,3,4,5]
[1,2,3,4,5]
[3,4,5]

because
array.slice[FIRST INDEX WANTED, THE INDEX YOU STOP WANTING, this index is not included)

the second argument does not need to be filled

61
Q

Can this code run?

const numbers = [1, 2, 3, 4, 5]

const number2 = numbers.slice(, 3)
const number3 = numbers.slice(   ,  )
A

NO, it just doesn’t work

“Uncaught c:\Users\C\Documents\tacky_js_fund\001.js:67”

62
Q

How to clear the console?

A

console.clear()

63
Q

How to get the last three items in the shortest way.

const numbers = [1, 2, 3, 4, 5]

A

numbers.slice(-3)

Additional knowledge:
The convention slice rule still apply. If you want to slice start from let say , -3, to the end

numbers.slice(-3) (NO SECOND NUMBER AS 0, it will GET NOTHING

if you want to get last three and last two item.
use number.slice(-3,-1)

64
Q

What datatype does slice method apply to?

A

array and string, I find them the same (not 100% sure)

65
Q

Will the code run?

const numbers = [1, 2, 3, 4, 5]
const number2 = numbers.slice(2, 689)
A

YES, if gives [3, 4, 5], that’s a strange thing about slice.

The second argument can be infinitely big, it does not matter

66
Q

How to get the index of items in array OR string

const names = 'apple' 
const nameArray = [a, p, p, l, e]

How to get the index of “l”

A

console.log(names.indexOf(‘l’))

“3”

console.log(nameArray.indexOf(‘l’))

“3”

67
Q

What will be the result of:

const names = ‘apple’

console. log(names.indexOf(‘p’))
console. log(names.indexOf(‘x’))

A

First result, is “1”

Tho “p” appear more than once,
the method indexOf will only find the FIRST char or item that match

Second result is “-1”
Strange thing again, if the char cannot be find, it will just say -1. This property can be useful such as checking if that element is present in a string or array.

68
Q

How to check if certain variable is in a array? What is the easiest way? E.g.

const names = [‘Florin’, ‘Ivan’, ‘Liam’]

A

console.log(names.includes(“Ivan”))

“true”

69
Q

If I want the index of second “p”, what should I do?

const names = ‘apple’

A

console.log(names.lastIndexOf(‘p’))

“2”

70
Q

What is the result of

str123 = “apple”
console.log(str123.slice[0, 4])

A

“undefined”

because I write slice[0,4]

That’s fucking strange, I do not know, may learn it later.

71
Q

what data type can method includes be used?

A

at least both string and array

72
Q

Will the below code run? What will be the result? What problem does it have?

function addNew(“fuck”)

A

“uncaught”

In problems of VScode, it may say “identifier expected”

73
Q

What are the ways to identify if there is a certain string at the beginning of the string?

A

if string.indexOf(‘certain_String’) == 0{

74
Q

What is the result of this:

function multiply(a, b) {
a * b
}
console.log(multiply(2, 3))

A

undefined….

because cannot display a funciton? I need to write return in order to see the work

75
Q

what does the method splice do?

A

First, it deletes items from an array and can be stored in variables. The array is changed. (CANNOT BE USED IN STRING, it should be split for it)

array.splice(index start slicing, numbers of element u want to slice after that start index, element u want to add(can be as many as you want)

const numbers = [1,2,3,4,5]
deleted = numbers.splice(2,3)
console.log(numbers)
console.log(deleted)

[1,2]
[3,4,5]

Second, it can add new elements to the array.

76
Q

How to sort numbers in a array?
e.g.

var numbers = [4, 2, 5, 1, 3]

A
var numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
  return a - b;
});
console.log(numbers);

// [1, 2, 3, 4, 5]

77
Q

How to check if a number is an integer?

A

Number.isInteger(23.00)

“true”

Note that it can 23.000 is still integer

78
Q

What problem will the below code have?

if (1 == 1)

A

“Express expected”

79
Q

How to make a number to a string?

A
let num = 15;
let n = num.toString();
80
Q

Does this code run?

a = []
a.push(“2” + “fuck”)

A

[‘2fuck’]

81
Q

how to change string to lower and upper case

A

String.toUpperCase()

String.toLowerCase()

82
Q

How to make a for loop that simply loop through all element in string or array and stop?

A
function accum(s) {
    s = s.toLowerCase()
    for(i = 0, i==s.length, i++)
}
83
Q

How to multiply a string, let say ‘hi”*3?

A
let str = "hi ";
let multiStr = str.repeat(3);
84
Q

when I use for loop and the problem is ‘;’ expected, what is the usual mistake that I make

A

for (i = 0, i == s.length, i++){

85
Q

We know how to change the elements in array, but how to change sting?

A

735

In JavaScript, strings are immutable, which means the best you can do is to create a new string with the changed content and assign the variable to point to it.

86
Q

What is the problem of the below function? (I spend 20 minutes debugging this)

function nbYear(p0, percent, aug, p) {
    let years = 0
    let decPercent = percent * 0.01
    for (i = p0; i < p; i * (1 + decPercent) + aug) {
        years += 1
        console.log(i)
    }
    return years
}
A

U don’t write: i * (1 + decPercent) + aug)

You write: i = i * (1 + decPercent) + aug)

87
Q

How to combine elements in an array into a string

A

const elements = [‘Fire’, ‘Air’, ‘Water’];

console.log(elements.join());
// expected output: "Fire,Air,Water"
console.log(elements.join(''));
// expected output: "FireAirWater"
console.log(elements.join('-'));
// expected output: "Fire-Air-Water"
88
Q

When checking data type, how to check typeof conditional?

A

No frails, juz check if it is equal to string of the name of that data typeof. Also, that string is LOWER CASE

console. log(typeof x == “string”)
console. log(typeof x)

89
Q

how to convert string to number?

A

Number(“string”)

UPPER CASE for the first letter

90
Q

How to make a comment in a paragraph? What symbol?

A
/*
Everything in between is a comment.
*/
91
Q

what are offcial terms for for loop?

A
for (initializer; condition; final-expression) {
  // code to run
}

Inside the parentheses we have three items, separated by semi-colons:
An initializer — this is usually a variable set to a number, which is incremented to count the number of times the loop has run. It is also sometimes referred to as a counter variable.
A condition — as mentioned before, this defines when the loop should stop looping. This is generally an expression featuring a comparison operator, a test to see if the exit condition has been met.
A final-expression — this is always evaluated (or run) each time the loop has gone through a full iteration. It usually serves to increment (or in some cases decrement) the counter variable, to bring it closer to the point where the condition is no longer true.
Some curly braces that contain a block of code — this code will be run each time the loop iterates.

92
Q

Things about NaN part 1

A
NaN
NaN (Not a Number) is a numeric data type that means an undefined value or value that cannot be represented, especially results of floating-point calculations.

For example, NaNs can represent infinity, result of division by zero, missing value, or the square root of a negative (which is imaginary, whereas a floating-point number is real).

Practically speaking, if I divide two variables in a JavaScript program, the result may be NaN, which is predefined in JavaScript as “undefined”. Hence this division may break the program. Now, if this computation was a small part of a much larger algorithm, it would be really painful to figure out where the error actually occurs. Fortunately, since the result will be NaN and I know my divisor may turn out to be 0, I can set up testing conditions that prevent any such computations in the first place or notify me of where they happen.

93
Q

Things about NaN part 2

A

Description
NaN is a property of the global object. In other words, it is a variable in global scope.

The initial value of NaN is Not-A-Number — the same as the value of Number.NaN. In modern browsers, NaN is a non-configurable, non-writable property. Even when this is not the case, avoid overriding it. It is rather rare to use NaN in a program.

There are five different types of operations that return NaN:

Number cannot be parsed (e.g. parseInt(“blabla”) or Number(undefined))
Math operation where the result is not a real number (e.g. Math.sqrt(-1))
Operand of an argument is NaN (e.g. 7 ** NaN)
Indeterminate form (e.g. 0 * Infinity, or undefined + undefined)
Any operation that involves a string and is not an addition operation (e.g. “foo” / 3)
Examples
Testing against NaN
NaN compares unequal (via ==, !=, ===, and !==) to any other value – including to another NaN value. Use Number.isNaN() or isNaN() to most clearly determine whether a value is NaN. Or perform a self-comparison: NaN, and only NaN, will compare unequal to itself.

NaN === NaN;        // false
Number.NaN === NaN; // false
isNaN(NaN);         // true
isNaN(Number.NaN);  // true
Number.isNaN(NaN);  // true
function valueIsNaN(v) { return v !== v; }
valueIsNaN(1);          // false
valueIsNaN(NaN);        // true
valueIsNaN(Number.NaN); // true
Copy to Clipboard
However, do note the difference between isNaN() and Number.isNaN(): the former will return true if the value is currently NaN, or if it is going to be NaN after it is coerced to a number, while the latter will return true only if the value is currently NaN:

isNaN(‘hello world’); // true
Number.isNaN(‘hello world’); // false
Copy to Clipboard
For the same reason, using a bigint value will throw an error with isNaN() and not with `Number.isNaN():

isNaN(1n); // TypeError: Conversion from ‘BigInt’ to ‘number’ is not allowed.
Number.isNaN(1n); // false
Copy to Clipboard
Additionally, some array methods cannot find NaN, while others can.

let arr = [2, 4, NaN, 12];

arr. indexOf(NaN); // -1 (false)
arr. includes(NaN); // true
arr. findIndex(n => Number.isNaN(n)); // 2

94
Q

What will be the result

console.log(typeof (Number(“abc”)))

A

“number”

Since it gives NaN which is a number data type.

95
Q

What are the difference of comparsion of:

!=
!==

give example

A

They are subtly not the same.

!= checks the value
!== checks the value and type

‘1’ != 1 // false (these two are the same)
‘1’ !== 1 // true (these two are not the same).
In the previous example. The first half of the expression is a string, the second half is an integer.

96
Q

What is the result of below?

console. log(Number(str[0]) == NaN)
console. log(Number(str[0]) == “NaN”)

A

“false”,

you just cannot do this, you should use isNaN()

Since even
NaN === NaN; // false

Additionally, some array methods cannot find NaN, while others can.

let arr = [2, 4, NaN, 12];

arr. indexOf(NaN); // -1 (false)
arr. includes(NaN); // true
arr. findIndex(n => Number.isNaN(n)); // 2

97
Q

why .length dont have (), why .substr(), .replace(), .split(), have ()

A

.length is property, it is like my property is 180 cm.

.split() is a method, it is just like HooSum.sayfoullanguage is my method. and method ultimately are functions

98
Q

Are they the same?

abc = function (num) {
console.log(‘it worked’)
console.log(num)
}

abc(2)

function def(num) {
console.log(‘it worked’)
console.log(num)
}

def(3)

A

Yes, they are the same. When you use the function, it is exactly the same

99
Q

When you try to loop through a object, it says

Uncaught TypeError: xxx.xxx is not iterable

A

it means the xxx may not present in the data