Skip to content

Instantly share code, notes, and snippets.

@ilovelili
Created September 25, 2020 14:23
Show Gist options
  • Save ilovelili/c20f203606b25f90ec8315fa78bc8a97 to your computer and use it in GitHub Desktop.
Save ilovelili/c20f203606b25f90ec8315fa78bc8a97 to your computer and use it in GitHub Desktop.
const c = (x) => console.log(x);
// Declare a variable named challenge and assign it to an initial value '30 Days Of JavaScript'
let cha = "30 Days Of JavaScript";
// What is the character code of J in '30 Days Of JavaScript' string using charCodeAt()
const index = cha.indexOf("J");
c(cha.charCodeAt(index));
//Check if typeof '10' is exactly equal to 10. If not make it exactly equal.
c(typeof('10'));
c(parseInt('10'));
//Check if parseFloat('9.8') is equal to 10 if not make it exactly equal with 10.
c(parseFloat('9.8'));
c(Math.round(parseFloat('9.8')));
//Access the 'JavaScript' string characters using a random number.
c("JavaScript"[Math.round(Math.random() * 10)]);
//Use substr to slice out the phrase because because because from the following sentence:'You cannot end a sentence with because because because is a conjunction'
let phr = "You cannot end a sentence with because because because is a conjunction";
c(phr.indexOf("b"));
const length = "because because because".length;
c(phr.substr(31,length));
console.log(3 !== 3);
//Ternary Operators 看不懂
// Declare name, age, isMarried, year variable
let name = "Xiaxia";
let age = 18;
let isMarried = true;
let year = 2020;
let fruits = ['apple', 'pear'];
// and assign value to it and use the typeof operator to check different data types.
c(typeof name);
c(typeof age);
c(typeof isMarried);
c(typeof year);
c(typeof fruits);
//Check if type of '10' is equal to 10
c(typeof '10' == 10);
//Check if parseInt('9.8') is equal to 10
c(parseInt('9.8') == 10);
//Boolean value is either true or false.
c(3 >= 3);
let truev = 4<3;
c(truev);
//Write three JavaScript statement which provide truthy value.
//Write three JavaScript statement which provide falsy value.
//Figure out the result of the following comparison expression first without using console.log(). After you decide the result confirm it using console.log()
c('>>>>>>>>>>>>>>>>>>>>>>>>>>>');
c(4 != '4'); // ?? false
c('>>>>>>>>>>>>>>>>>>>>>>>>>>>');
//There is no 'on' in both dragon and python
c("dragon" && "python".includes("on"));
//Use the Date object to do the following activities
//What is the year today?
let today = new Date();
c(today.getFullYear());
//What is the month today as a number?
c(parseInt(today.getMonth()));
//What is the date today?
c(today.getDate());
//What is the day today as a number?
c(parseInt(today.getDay()));
//What is the hours now?
c(today.getHours());
//What is the minutes now?
c(today.getMinutes());
//Find out the numbers of seconds elapsed from January 1, 1970 to now.
c(today.getTime());
c(Date.now());
// let height = prompt("enter height");
// height = parseInt(height);
// let input = prompt("enter demision, split by ','");
// const demision = input.split(',')
// const width = parseInt(demision[0]);
// const height = parseInt(demision[1]);
// const result = 0.5 * width * height
// document.querySeletor("#id").innerHTML = `<p>${result}</p>`;
// var checkNum = (num) => {
// if (num > 10) {
// c("big");
// }
// else if (num > 5) {
// c("medium");
// }
// else if (num > 0) {
// c("small");
// }
// else {
// c("negative");
// }
// }
// == swtich
var checkNumber = (num) => {
console.log(`num is ${num}`);
switch (true) {
case (num > 10): {
console.log("10");
// break;
}
case (num > 5): {
console.log("5");
// break;
}
case (num > 0): {
console.log("0");
break;
}
default: {
console.log("other");
}
}
}
checkNumber(20);
// var checkNum = (num) => {
// if (num > 10) {
// c("big");
// }
// if (num > 5) {
// c("medium");
// }
// if (num > 0) {
// c("small");
// }
// else {
// c("negative");
// }
// }
var kaka1 = (num) => {
if (num > 0) {
console.log(">0")
} else {
console.log("<=0")
}
}
var kaka2 = (num) => {
num > 0 ? console.log(">0") : console.log("<=0")
}
var kaka3 = (num) => {
const result = num > 0 ? ">0" : "<=0"
console.log(result)
}
var kaka4 = (num) => {
console.log(num > 0 ? ">0" : "<=0")
}
var kaka5 = (num) => {
console.log(
// the engineer has left. Do NOT touch the code! It just works and no one understands
num > 10 ?
(num > 20 ? ">20" : ">10 <=20") :
(num < 5 ? "<5" : ">5 <=10")
)
}
kaka5(16);
// checkNum(8);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment