Skip to content

Instantly share code, notes, and snippets.

@voratham
Created April 13, 2022 15:16
Show Gist options
  • Save voratham/2ab8b10384c257d0a1ba7b2fbb0209d2 to your computer and use it in GitHub Desktop.
Save voratham/2ab8b10384c257d0a1ba7b2fbb0209d2 to your computer and use it in GitHub Desktop.
js101-with-p-peck
console.log("hello world !")
var x = 2;
function test(){
x = 3
}
// function sum(a ,b){
// return a +b;
// }
const sum = (a,b) => a + b
function main(){
x = 1;
console.log('sum ::' ,sum(1,2));
// variable
// string , number (float , double) , boolean
console.log('x : ',x)
const a = 1;
const PI = 3.14;
let b = true
b = false
console.log('b : ',b)
// flow-control
let score = 100;
if(score > 90){
score = "A"
} else if(score > 80) {
score = "B"
}else {
score = "C"
}
console.log('score :' , score)
let score2 = score > 90 ? "A" : score > 80 ? "B" : "C"
console.log('score2 :' , score)
let _a = 1;
let _b = 2;
let _c = 3;
console.log('debug : ',_a , _b , _c)
const name = ""
switch (name) {
case 'dream':{
console.log('hello', name)
break;
}
default:
console.log('hello guest')
break;
}
// Loop // tuple
const arr = [1,2,3,4,5,6]
arr.push(7)
arr.push(8)
// const test = arr.pop()
console.log('test : ', "arr[0]", arr[0], " arr[last] :", arr[arr.length - 1])
// for (let index = 0; index < arr.length; index++) {
// console.log('index :' , index , " value", arr[index])
// }
// const newArr = []
// for (let index = 0; index < arr.length; index++) {
// if(arr[index] != 2){
// newArr.push(arr[index])
// }
// }
// console.log('newArr :: ',newArr)
// const newArr = []
// arr.forEach( (val, index) => {
// console.log('index :' ,index ,'val :: ',val)
// if(val !== 2){
// newArr.push(val)
// }
// })
// console.log('newArr :: ',newArr)
// const newArr = arr.filter((val) => {
// if(val !== 2){
// return val
// }
// })
// console.log('arr :: ',arr)
// console.log('newArr : ',newArr)
// Filter input -> f() -> output
// Map f(x) -> input -> f() -> output
const pow2 = (value) => value * value
const newPow2 = arr.map(pow2).map(pow2).filter( value => {
if(value > 1000 ){
return value
}
})
console.log('newPow2 : ',newPow2)
// Reduce
const numbers = [1,2,3,4,5]
const result = numbers.reduce( (acc, value) => {
acc = acc + value
return acc
}, 0)
console.log('result : ',result)
// Find
const numbers2 = [5,1,2,3,4,5]
const target5 = numbers2.find(val => val == 5)
console.log("🚀 target5", target5)
const a1 = "2";
if(a1 !== 2){
console.log('pass')
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment