Javascript Flashcards
(64 cards)
Implementing a function in JavaScript and call the function
function greet(name) {
console.log(Hello, ${name}!
);
}
// Call the function
greet(“Camila”); // Output: Hello, Camila!
how to console.log a value
console.log(Hello, ${name}!
);
how to put default parameters
function greet(name = “Guest”) {
console.log(Hello, ${name}!
);
}
how to call it with desired parameters?
greet(“Camila”); // Output: Hello, Camila!
mention 7 array methods ( do not need to explain )
map()
filter()
reduce()
forEach()
sort()
slice()
splice()
mention 4 strings methods ( do not need to explain )
split()
join()
substring()
slice()
mention 3 Object Methods
Object.keys()
Object.values()
Object.entries()
mention 5 loops
for Loop
while Loop
do…while Loop
for…of Loop
for…in Loop
steps to approach a new problem (4)
- declare a function, name it, call it
- send input as default params
- write in comment which is datatype of input/output
- console.log the desired return
- detect with data type is the input, think of avaiable methods/loops
diference de ! y !!
! (Logical NOT):
The ! operator negates a value. It first coerces the value into a boolean, and then it inverts
console.log(!0); // true (0 is falsy, so !0 is true)
console.log(!’hello’); // false (non-empty string is truthy, so !’hello’ is false)
!! (Double Logical NOT):
console.log(!!0); // false (0 is falsy, so !!0 is false)
console.log(!!’hello’); // true (non-empty string is truthy, so !!’hello’ is true)
diference de == y ===
==: Realiza comparación flexible con conversión de tipos implícita.
===: Realiza comparación estricta sin conversión de tipos, verificando tanto el valor como el tipo.
console.log(5 == ‘5’); // true (porque ‘5’ se convierte a número)
console.log(null == undefined); // true (considerados iguales en comparación débil)
console.log(0 == false); // true (0 y false son considerados iguales)
check if its an array
Array.isArray(cleanWord)
how to remove a space of letter with js?
let result = str.replace(/\s/g, ‘’); // Removes all spaces
let result = str.replace(/l/g, ‘’); // Removes all lowercase “l”
how to loop easy over an array?
arr.map((item, index) => {
if (item === target) { indexFounded = index; } })
.map() is meant for transforming arrays (i.e., creating a new array from the existing one).
why .map is not good idea to use it for a loop?
how to re do this? arr.map((item, index) => {
if (item === target) { indexFounded = index; } })
arr.forEach((item, index) => {
if (item === target) {
indexFounded = index;
}
});
why this is wrong? arr.length()
arr.length (length is a property, not a method).
sort Method, for arrays.
give an example to order: [1, 2, 5, 5, 6, 9]
orderArray.sort((a, b) => a - b);
how to order this manually?
without sort:
orderArray.sort((a, b) => a - b);
4 steps
we need a nested loop.
- we need a forEach item
arr.forEach(item => {
for each item in the array we need to see where to add it.
- for (let i = 0; i < orderArray.length; i++) {
} - inside the secound loop:
if (item < orderArray[i]) {
orderArray.splice(i, 0, item); // Insert at index i
inserted = true;
break;
}
if it dosent get in here then, we put it in the last place.
- if (!inserted) {
orderArray.push(item);
}
what method can i use for:
1.array
2.modify add, remove into an existing one
splice method is used to add, remove, or replace elements in an array. It modifies the original array in place and returns an array containing the removed elements
how to use splice just to add an item and avoid deleting.
splice(i, 0, item) Works Here
i:
This is the index where the item will be inserted in the orderArray.
0:
The deleteCount is 0, meaning no elements in the orderArray are removed. The array will just expand to accommodate the new item.
item:
This is the element being added to the orderArray.
why the function sortNamesByLength () was inserting double in the new array
function sortNamesByLength(arrNames = [“Alice”, “Bob”, “Charlie”, “David”]) {
//input is an array
//output new arrau sorred
let sortedPerLenght = [];
//add into new array
// if its smaller than other, then add it in the index of that. inserted it
arrNames.forEach((item) => {
let inserted = false;
for (let i = 0; i < sortedPerLenght.length; i++) { if (item.length < sortedPerLenght[i].length) { console.log(sortedPerLenght[i]) console.log(sortedPerLenght[i].length) sortedPerLenght.splice(i, 0, item); break; } } if (!inserted) { sortedPerLenght.push(item); } });
return sortedPerLenght;
}
console.log(sortNamesByLength());
You need to update inserted to true once the item is successfully inserted using splice().
in why the function sortNamesByLength () why a break is needed in:
for (let i = 0; i < sortedPerLenght.length; i++) {
if (item.length < sortedPerLenght[i].length) {
console.log(sortedPerLenght[i])
console.log(sortedPerLenght[i].length)
sortedPerLenght.splice(i, 0, item);
break;
}
}
Imagine an array sortedPerLenght = [3, 5, 8] and you want to insert 4. After comparing with 3:
You insert 4 between 3 and 5.
Continuing to check 4 against 5 or 8 doesn’t make sense because the array is already sorted, and 4 will always be smaller than 5 and 8.
what is the issue here: with index
arr.forEach((item, index) => {
for (let i=index; i < arr.length ; i++){ if (item + arr[i] === target ) { uniquePairsArray.push([item, arr[i]]); break; } } } ) console.log(uniquePairsArray); debugger return uniquePairsArray;
Inner loop: Starts from index + 1 to avoid pairing the element with itself.
The inner loop should not start at the same index as the outer loop, as that would lead to pairing a number with itself. Instead, you should ensure that the inner loop starts from the next index to avoid duplicates like [item, item].
in interview how to test 3 times your function…
we need 3 console.log and 3 params