Whenever there's a divergence between what your brain thinks is happening, and what the computer does, that's where bugs enter the code.
// ex
let x = 2;
++x; // 3
// this means
x = x +1
let x = "2"
++x; // shoudl be 21
// but it's 3 beacuse JS is written like this
❓🤔 Evrey thing inside JS is an object ???
💁🏻♀️ Evereh thing can bevhave as an object
- undefined
- string
- number
- boolean
- object
- symbol: in ES6 used for suto private keys
- bigInt (future): let x = 34n
function & arrays are a subtype of a object type
- undefined • string • number • boolean • object • symbol • null • bigint (future)⇒Not
- object • function • array ⇒ Objects
In JavaScript, variables don't have types, values do.
- typeof : always return string
- function & arrays not types of the top level they are sub types of object, but when using typeof array ⇒ object while function ⇒ function
- type of null = object it is a bug in JS, ih they want to correct it a lot of things will fail in the JS
(Special Values)is the only value that is not equall to it self
NaN type is number (invalid number), becuase it cmoes from numeric operations
isNan()
// evaluate any argument to number then cheak weather it's a Nan or not
Number.isNan()
// better than the prevous
isNaN("Sarah") // ✅ true
Number.isNaN("Sarah") // ❌ false
it’s built in cheaker
better way for cheaking equality (better than ===)
// we might use -0 for directons in some applecaions which the sign means direc.
-0 === 0 // ✅ true
Object.is(-0 ,0 ) // ❌ false
=== failed in Nan & -0
// TODO: define polyfill for `Object.is(..)`
if (!Object.is || true){ // to disaple the built in method & build my own
Object.is = function ObjectIs(x,y){
const xNegZero = isItNegZero(x)
const yNegZero = isItNegZero(y)
if (yNegZero || xNegZero ){
return yNegZero && xNegZero
}else if (isItNane(x) && isItNane(y)){
return true
}else {
return x===y
}
function isItNegZero(v){
return v===0 && (1/v)=== -Infinity
}
function isItNane(v){
return v !==v
}
}
}
// tests:
console.log(Object.is(42,42) === true);
console.log(Object.is("foo","foo") === true);
console.log(Object.is(false,false) === true);
console.log(Object.is(null,null) === true);
console.log(Object.is(undefined,undefined) === true);
console.log(Object.is(NaN,NaN) === true);
console.log(Object.is(-0,-0) === true);
console.log(Object.is(0,0) === true);
console.log(Object.is(-0,0) === false);
console.log(Object.is(0,-0) === false);
console.log(Object.is(0,NaN) === false);
console.log(Object.is(NaN,0) === false);
console.log(Object.is(42,"42") === false);
console.log(Object.is("42",42) === false);
console.log(Object.is("foo","bar") === false);
console.log(Object.is(false,true) === false);
console.log(Object.is(null,undefined) === false);
console.log(Object.is(undefined,null) === false);
type conversion
(null).toString() // "null"
undefined.toString() // "undefined"
true.toString() // "true"
false.toString() // "false"
3.14159.toString() // "3.14159"
(0).toString() // "0"
(-0).toString() // "0"
([]).toString() // ""
[1, 2, 3].toString() // "1,2,3"
[null, undefined].toString() // ","
[[[], [], []], []].toString() // ",,,"
([,,,,]).toString() // ",,,"
{} "[object Object]"
{a:2} "[object Object]"
// using Number(x)
"" // 0
"0" // 0
"-0" // -0
" 009 " // 9
"3.14159" // 3.14159
"0." // 0
".0" // 0
"." // NaN
"0xaf" // 175 // consvert hexacecimal to decimal
false // 0
true // 1
null // 0
undefined // Nan
[""] // 0
["0"] // 0
["-0"] // 0-
[null] // 0
[undefined] // 0
[1,2,3] // NaN
[[[[]]]] // 0
{ .. } // NaN
3 > 2 > 1
(true) > 1
1 > 1 // false !!!!
// Falsy values:
“”
0, -0
null
NaN
false
undefined
// anything eles will be truthy value
// coercions:
// string concatination calls toString method
"Sarah" + 15
`hhh ${variable}`
// also use toString
`hh ${variable.toString()}
// impicit `coercion, primitve types don't have methods
// all of the above are implicit
// explicit
String(variable)
My solution:
const makeServerRequest = new Promise((resolve, reject) => {
// responseFromServer represents a response from a server
let responseFromServer;
if(responseFromServer) {
resolve("We got the data")
} else {
reject("Data not received")
}
});
My solution:
const makeServerRequest = new Promise((resolve, reject) => {
// responseFromServer is set to true to represent a successful response from a server
let responseFromServer = true;
if(responseFromServer) {
resolve("We got the data");
} else {
reject("Data not received");
}
});
makeServerRequest.then(result => {
console.log(result);
});
My solution:
const makeServerRequest = new Promise((resolve, reject) => {
// responseFromServer is set to false to represent an unsuccessful response from a server
let responseFromServer = false;
if(responseFromServer) {
resolve("We got the data");
} else {
reject("Data not received");
}
});
makeServerRequest.then(result => {
console.log(result);
}).catch(error=>{
console.log(error)
})
My solution:
let a = 5;
let b = 1;
a++;
// Only change code below this line
console.log(a)
let sumAB = a + b;
console.log(sumAB);
My solution:
// Open your browser console.
let output = "Get this to show once in the freeCodeCamp console and not at all in the browser console";
// Use console.log() to print the output variable.
console.log(output);
// Use console.clear() to clear the browser console.
console.clear();
My solution:
function solution(str){
let sliced = str.slice('')
sliced+= sliced.length%2 !==0 ? '_' : ''
const ans = []
for (let i=0; i<sliced.length; i+=2){
ans.push(sliced[i]+sliced[i+1])
}
return ans
}
My solution:
function solution(roman) {
const data = { M: 1000, D: 500, C: 100, L: 50, X: 10, V: 5, I: 1 };
const numbers = roman.split('');
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
if (data[numbers[i]] < data[numbers[i + 1]]) {
sum += data[numbers[i + 1]] - data[numbers[i]];
i++;
}
else {
sum += data[numbers[i]];
}
}
return sum;
}