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
what should be the the name of assignement to array
arr
26
how to loop object>
for in loop
27
what scope is let and const, what scope is var
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 ```
28
how to see if there is an element in a array?
Example Check if an array includes "Mango": ``` const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.includes("Mango") // Returns true ```
29
how to set interval?
setIntervel(function(){ | }, 1000)
30
Is this code ok? ``` let x = 2; let x = 3; ``` ``` const y = 2; const y = 3; ```
No, it will say Uncaught
31
How would you define scope of variable?
Scope determines the accessibility (visibility) of variables. JavaScript has 3 types of scope: 1. Block scope 2. Function scope 3. Global scope
32
Before ES6 (2015), what scope does JavaScript only had? What did ES6 introduced?
1. Global Scope 2. Function Scope ES6 introduced "let" and "const", which provide BLOCK SCOPE.
33
What is a block scope in JavaScript?
Variables declared inside a { } block cannot be accessed from outside the block: ``` { let x = 2; } // x can NOT be used here ```
34
What scope if one declares: var x = 2; What does it mean
Variables declared with the var keyword can NOT have block scope. Variables declared inside a { } block can be accessed from outside the block.
35
What is local scope and what does it mean?
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
36
What is local scope and what does it mean?
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
37
Since local variables are only recognized inside their functions, can we use the same name in different functions?
Yes, we can use the same name
38
What happened after a local variable is used in a function?
Local variables are created when a function starts, and deleted when the function is completed.
39
A variable declared outside a function, becomes __________ ?
GLOBAL ``` let carName = "Volvo"; // code here can use carName ``` ``` function myFunction() { // code here can also use carName } ```
40
Local variables have _______? A global variable has _______?
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
What will a variable become automatically Global?
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
Why we should not create global variable unless we intend to?
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
What is the lifetime of JavaScript variable
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
What's the error below? Uncaught TypeError: Assignment to constant variable at VM968 pen.js:2
The code is below: ``` const Pi = 3.1415 Pi = 1 ``` variable declared as const should not be change.
45
var vs let
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
Can this code run? ``` function square(number) { return number * number; } ``` console.log(number)
NO >Uncaught ReferenceError: Number is not defined
47
Can this code run? ``` function myFun() { console.log(someVar) } ``` myFun() var someVar = "Shoes"
YES. but with some problem "undefined" Because the program can still recognize it EVEN the variable is declared after
48
``` function myFun() { console.log(someVar) } ``` var someVar = "Shoes" myFun()
YES "shoes"
49
if I use var and let INSIDE a function, do the variables have a global scope?
NO, they have functional scope in a FUNCTION.
50
Can a function inside a function be called outside?
NO
51
if a function is BELOW the code that is calling it, can it run??? e.g. myFun() ``` function myFun(){ console.log("hi nubs") } ```
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
What does below code show? console. log( 1 == "1" console. log( 1 === "1" )
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
When using "this" in Node JS, what will happen in below code? (that is the only code) function dcode() { console.log(this) } dcode();
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
What is the definition of "this" according to "Programming with Mosh"?
The object that is executing the current function.
55
if the function is part of an object, what do we call it?
method
56
What is the inside the object? ``` const hot_girl = { cup: 'f' } ``` hot_girl.height = '170 cm'
{cup: 'f', height: '170 cm'}
57
What is inside the object? ``` const hot_girl = { cup: 'f' } ``` hot_girl.height = '170 cm'
{cup: 'f', height: '170 cm'}
58
what does the array slice method do?
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
What'll be the result of number 2? const numbers = [1, 2, 3, 4, 5] const number2 = numbers.slice(1,4 )
[2,3,4] because array.slice[FIRST INDEX WANTED, THE INDEX YOU STOP WANTING, this index is not included)
60
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,) ```
[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
Can this code run? const numbers = [1, 2, 3, 4, 5] ``` const number2 = numbers.slice(, 3) const number3 = numbers.slice( , ) ```
NO, it just doesn't work "Uncaught c:\Users\C\Documents\tacky_js_fund\001.js:67"
62
How to clear the console?
console.clear()
63
How to get the last three items in the shortest way. const numbers = [1, 2, 3, 4, 5]
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
What datatype does slice method apply to?
array and string, I find them the same (not 100% sure)
65
Will the code run? ``` const numbers = [1, 2, 3, 4, 5] const number2 = numbers.slice(2, 689) ```
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
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"
console.log(names.indexOf('l')) "3" console.log(nameArray.indexOf('l')) "3"
67
What will be the result of: const names = 'apple' console. log(names.indexOf('p')) console. log(names.indexOf('x'))
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
How to check if certain variable is in a array? What is the easiest way? E.g. const names = ['Florin', 'Ivan', 'Liam']
console.log(names.includes("Ivan")) "true"
69
If I want the index of second "p", what should I do? const names = 'apple'
console.log(names.lastIndexOf('p')) "2"
70
What is the result of str123 = "apple" console.log(str123.slice[0, 4])
"undefined" because I write slice[0,4] That's fucking strange, I do not know, may learn it later.
71
what data type can method includes be used?
at least both string and array
72
Will the below code run? What will be the result? What problem does it have? function addNew("fuck")
"uncaught" In problems of VScode, it may say "identifier expected"
73
What are the ways to identify if there is a certain string at the beginning of the string?
if string.indexOf('certain_String') == 0{
74
What is the result of this: function multiply(a, b) { a * b } console.log(multiply(2, 3))
undefined.... because cannot display a funciton? I need to write return in order to see the work
75
what does the method splice do?
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
How to sort numbers in a array? e.g. var numbers = [4, 2, 5, 1, 3]
``` var numbers = [4, 2, 5, 1, 3]; numbers.sort(function(a, b) { return a - b; }); console.log(numbers); ``` // [1, 2, 3, 4, 5]
77
How to check if a number is an integer?
Number.isInteger(23.00) "true" Note that it can 23.000 is still integer
78
What problem will the below code have? if (1 == 1)
"Express expected"
79
How to make a number to a string?
``` let num = 15; let n = num.toString(); ```
80
Does this code run? a = [] a.push("2" + "fuck")
['2fuck']
81
how to change string to lower and upper case
String.toUpperCase() | String.toLowerCase()
82
How to make a for loop that simply loop through all element in string or array and stop?
``` function accum(s) { s = s.toLowerCase() for(i = 0, i==s.length, i++) } ```
83
How to multiply a string, let say 'hi"*3?
``` let str = "hi "; let multiStr = str.repeat(3); ```
84
when I use for loop and the problem is ';' expected, what is the usual mistake that I make
for (i = 0, i == s.length, i++){
85
We know how to change the elements in array, but how to change sting?
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
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 } ```
U don't write: i * (1 + decPercent) + aug) You write: i = i * (1 + decPercent) + aug)
87
How to combine elements in an array into a string
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
When checking data type, how to check typeof conditional?
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
how to convert string to number?
Number("string") UPPER CASE for the first letter
90
How to make a comment in a paragraph? What symbol?
``` /* Everything in between is a comment. */ ```
91
what are offcial terms for for loop?
``` 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
Things about NaN part 1
``` 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
Things about NaN part 2
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
What will be the result console.log(typeof (Number("abc")))
"number" Since it gives NaN which is a number data type.
95
What are the difference of comparsion of: != !== give example
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
What is the result of below? console. log(Number(str[0]) == NaN) console. log(Number(str[0]) == "NaN")
"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
why .length dont have (), why .substr(), .replace(), .split(), have ()
.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
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)
Yes, they are the same. When you use the function, it is exactly the same
99
When you try to loop through a object, it says Uncaught TypeError: xxx.xxx is not iterable
it means the xxx may not present in the data