카테고리 없음
10 Days of JavaScript
BenzhaminKim
2021. 1. 20. 21:43
Day 0: Hello, World!
1. console.log() is to print.
Day 0: Data Types
Input Format
Data TypeParameterDescription
string | secondInteger | The string representation of an integer you must sum with . |
string | secondDecimal | The string representation of a floating-point number you must sum with . |
string | secondString | A string of one or more space-separated words you must append to . |
In order to sum with string type of number, we need to convert string to number, so we use new Number().
function performOperation(secondInteger, secondDecimal, secondString) {
// Declare a variable named 'firstInteger' and initialize with integer value 4.
const firstInteger = 4;
// Declare a variable named 'firstDecimal' and initialize with floating-point value 4.0.
const firstDecimal = 4.0;
// Declare a variable named 'firstString' and initialize with the string "HackerRank".
const firstString = 'HackerRank ';
// Write code that uses console.log to print the sum of the 'firstInteger' and 'secondInteger' (converted to a Number type) on a new line.
console.log(firstInteger+new Number(secondInteger));
// Write code that uses console.log to print the sum of 'firstDecimal' and 'secondDecimal' (converted to a Number type) on a new line.
console.log(firstDecimal+new Number(secondDecimal));
// Write code that uses console.log to print the concatenation of 'firstString' and 'secondString' on a new line. The variable 'firstString' must be printed first.
console.log(firstString + secondString);
}
Day 1: Functions
For the recursive loop, you need to set the base case which is to stop the function and recurrence relation that reduces all other cases.
function factorial(n){
if(n===0){ // base case
return 1;
}
return n * factorial(n-1); // recurrence relation
}
Day 1: Let and Const
Let : you can change the variable.
Const : you cannot change the variable.
Day 2: Conditional Statements: If-Else
function getGrade(score) {
let grade;
// Write your code here
if(score>25 && score< 30){
grade = 'A';
}
else if(score > 20 && score <=25){
grade = 'B';
}
else if(score > 15 && score <=20){
grade = 'C';
}
else if(score > 10 && score <=15){
grade = 'D';
}
else if(score > 5 && score <=10){
grade = 'E';
}
else if(score > 0 && score <=5){
grade = 'F';
}
return grade;
}
Day 2: Loops
function vowelsAndConsonants(s) {
let vowels = ["a", "e", "i", "o", "u"];
for(let v of s) {
if(vowels.includes(v))
console.log(v);
}
for(let v of s) {
if(!vowels.includes(v))
console.log(v);
}
}
function vowelsAndConsonants(s) {
for(let i=0; i<s.length; i++){
if(s[i] === 'a' ||s[i] === 'e' ||s[i] === 'i' ||s[i] === 'o' ||s[i] === 'u'){
console.log(s[i]);
}
}
for(let i=0; i<s.length; i++){
if(s[i] !== 'a' && s[i] !== 'e' && s[i] !== 'i' && s[i] !== 'o' && s[i] !== 'u'){
console.log(s[i]);
}
}
}
Day 3: Arrays
let fruits = ['Apple', 'Banana']
let newLength = fruits.push('Orange')
// ["Apple", "Banana", "Orange"]
let last = fruits.pop() // remove Orange (from the end)
// ["Apple", "Banana"]
let first = fruits.shift() // remove Apple from the front
// ["Banana"]
let newLength = fruits.unshift('Strawberry') // add to the front
// ["Strawberry", "Banana"]
Day 4: Classes
'use strict';
function Fruit (type) {
this.type = type;
this.color = 'unknown';
this.getInformation = function() {
return 'This ' + this.type + ' is ' + this.color + '.';
}
}
let lime = new Fruit('Mexican lime');
console.log(lime.getInformation());
lime.color = 'green';
console.log(lime.getInformation());
'use strict';
function Fruit (type) {
this.type = type;
this.color = 'unknown';
}
Fruit.prototype.getInformation = function() {
return 'This ' + this.type + ' is ' + this.color + '.';
}
let lime = new Fruit('Mexican lime');
console.log(lime.getInformation());
lime.color = 'green';
console.log(lime.getInformation());