/* The Fiscal Code Each person in Italy has an unique identifying ID code issued by the national tax office after the birth registration: the Fiscal Code (Codice Fiscale). Check the Resources tab for more info on this. Given an object containing the personal data of a person (name, surname, gender and date of birth) return the 11 code characters as a string following these steps: Generate 3 capital letters from the surname, if it has: At least 3 consonants then the first three consonants are used.(Newman -> NWM). Less than 3 consonants then vowels will replace missing characters in the same order they appear (Fox -> FXO | Hope -> HPO). Less than three letters then "X" will take the third slot after the consonant and the vowel (Yu -> YUX). Generate 3 capital letters from the name, if it has: Exactly 3 consonants then consonants are used in the order they appear (Matt -> MTT). More than 3 consonants then first, third and fourth consonant are used (Samantha -> SNT | Thomas -> TMS). Less than 3 consonants then vowels will replace missing characters in the same order they appear (Bob -> BBO | Paula -> PLA). Less than three letters then "X" will take the the third slot after the consonant and the vowel (Al -> LAX). Generate 2 numbers, 1 letter and 2 numbers from date of birth and gender: Take the last two digits of the year of birth (1985 -> 85). Generate a letter corresponding to the month of birth (January -> A | December -> T) using the table for conversion included in the code. For males take the day of birth adding one zero at the start if is less than 10 (any 9th day -> 09 | any 20th day -> 20). For females take the day of birth and sum 40 to it (any 9th day -> 49 | any 20th day -> 60). Examples fiscalCode({ name: "Matt", surname: "Edabit", gender: "M", dob: "1/1/1900" }) ➞ "DBTMTT00A01" fiscalCode({ name: "Helen", surname: "Yu", gender: "F", dob: "1/12/1950" }) ➞ "YUXHLN50T41" fiscalCode({ name: "Mickey", surname: "Mouse", gender: "M", dob: "16/1/1928" }) ➞ "MSOMKY28A16" Notes Code letters must be uppercase. Date of birth is given in D/M/YYYY format. The conversion table for months is already in the starting code. Y is not a vowel. */ const months = { 1: "A", 2: "B", 3: "C", 4: "D", 5: "E", 6: "H", 7: "L", 8: "M", 9: "P", 10: "R", 11: "S", 12: "T" }; //console.log("months[1]: " + months[1]); let vowels = ["A", "E", "I", "O", "U"]; const firstCode = (person) => { let firstCode = []; let surname = person.surname.toUpperCase(); //console.log(surname); let a = 0; let b = ""; for (let i = 0; i < surname.length; i++) { for (let j = 0; j < vowels.length; j++) { if (surname[i] == vowels[j]) { //console.log("surname[i]: " + surname[i]); //console.log("vowels[j]: " + vowels[j]); //console.log("i: " + i); //console.log("j: " + j); a += 1; } } if (a === 0) { firstCode.push(surname[i]); b += surname[i]; } a = 0; if (firstCode.length === 3) { break; } } let c = firstCode.length; if (firstCode.length < 3) { for (let i = 0; i < surname.length; i++) { for (let j = 0; j < vowels.length; j++) { if (surname[i] == vowels[j]) { firstCode.push(surname[i]); b += surname[i]; c += 1; //console.log("c: " + c); //console.log("firstCode.length : " + firstCode.length); } } if (c === 3) { break; } } } return b; } /* console.log(firstCode({ name: "Matt", surname: "uy", gender: "M", dob: "1/1/1900" })); */ const secondCode = (person) => { let secondCode = []; let name = person.name.toUpperCase(); //console.log(name); let a = 0; let b = ""; for (let i = 0; i < name.length; i++) { for (let j = 0; j < vowels.length; j++) { if (name[i] == vowels[j]) { //console.log("name[i]: " + name[i]); //console.log("vowels[j]: " + vowels[j]); //console.log("i: " + i); //console.log("j: " + j); a += 1; } } if (a === 0) { secondCode.push(name[i]); } a = 0; } let d = 0; for(let i = 0; i < secondCode.length; i++) { if (secondCode.length <= 3) { b += secondCode[i]; } else { if(i === 1) { continue; } d += 1; b += secondCode[i]; if (d === 3) { break; } } } let c = secondCode.length; if (secondCode.length < 3) { for (let i = 0; i < name.length; i++) { for (let j = 0; j < vowels.length; j++) { if (name[i] == vowels[j]) { secondCode.push(name[i]); b += name[i]; c += 1; //console.log("c: " + c); //console.log("secondCode.length : " + secondCode.length); } } if (c === 3) { break; } } } //console.log(secondCode); return b; }; /* console.log(secondCode({ name: "amoa", surname: "uy", gender: "M", dob: "1/1/1900" })); */ const thirdCode = (person) => { let thirdCode = ""; let gender = person.gender.toUpperCase(); //console.log("gender: " + gender); let dobinDdMm = person.dob; dobinDdMm = dobinDdMm.split("/"); let dob = [dobinDdMm[1], dobinDdMm[0], dobinDdMm[2]].join("/"); //console.log("dob: " + dob); let a = []; let dobData = new Date(dob); date = dobData.getDate(); let month = dobData.getMonth(); month += 1; let f = []; let h = ""; let FullYear = dobData.getFullYear(); FullYear = parseInt(FullYear); while (FullYear > 99) { f.unshift(FullYear % 10); FullYear = parseInt(FullYear / 10); } f = f[0].toString() + f[1].toString(); //console.log("typeof f: " + typeof f); let g = months[month]; if (gender === "F") { date += 40; } date = date.toString(); let dateArr = []; for(let i = 0; i < 2; i++) { dateArr.push(date[i]); } if (dateArr[1] === undefined) { dateArr[1] = dateArr[0]; dateArr[0] = "0"; h = dateArr[0] + dateArr[1]; } else h = date; thirdCode = f + g + h; return thirdCode; //console.log("h: " + h); //console.log("g: " + g); //console.log(f); //console.log("date: " + date); // console.log("typeof date: " + typeof date); // console.log("FullYear: " + FullYear); }; /* console.log(thirdCode({ name: "Matt", surname: "yu", gender: "f", dob: "1/12/1950" })); */ function fiscalCode(person) { let code1 = firstCode(person); let code2 = secondCode(person); let code3 = thirdCode(person); //console.log("code1.length: " + code1.length); //console.log("code1: " + code1); if (code1.length < 3) { for (let i = code1.length; i < 3; i++) { code1 += "X"; } } if (code2.length < 3) { for (let i = code2.length; i < 3; i++) { code2 += "X"; } } let finalCode = code1 + code2 + code3; return finalCode; }; console.log(fiscalCode({ name: "Matt", surname: "Edabit", gender: "M", dob: "1/1/1900" })); console.log(fiscalCode({ name: "Helen", surname: "Yu", gender: "F", dob: "1/12/1950" })); console.log(fiscalCode({ name: "Mickey", surname: "Mouse", gender: "M", dob: "16/1/1928" }));
Write, Run & Share Javascript code online using OneCompiler's JS online compiler for free. It's one of the robust, feature-rich online compilers for Javascript language. Getting started with the OneCompiler's Javascript editor is easy and fast. The editor shows sample boilerplate code when you choose language as Javascript and start coding.
Javascript(JS) is a object-oriented programming language which adhere to ECMA Script Standards. Javascript is required to design the behaviour of the web pages.
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
rl.on('line', function(line){
console.log("Hello, " + line);
});
Keyword | Description | Scope |
---|---|---|
var | Var is used to declare variables(old way of declaring variables) | Function or global scope |
let | let is also used to declare variables(new way) | Global or block Scope |
const | const is used to declare const values. Once the value is assigned, it can not be modified | Global or block Scope |
let greetings = `Hello ${name}`
const msg = `
hello
world!
`
An array is a collection of items or values.
let arrayName = [value1, value2,..etc];
// or
let arrayName = new Array("value1","value2",..etc);
let mobiles = ["iPhone", "Samsung", "Pixel"];
// accessing an array
console.log(mobiles[0]);
// changing an array element
mobiles[3] = "Nokia";
Arrow Functions helps developers to write code in concise way, it’s introduced in ES6.
Arrow functions can be written in multiple ways. Below are couple of ways to use arrow function but it can be written in many other ways as well.
() => expression
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const squaresOfEvenNumbers = numbers.filter(ele => ele % 2 == 0)
.map(ele => ele ** 2);
console.log(squaresOfEvenNumbers);
let [firstName, lastName] = ['Foo', 'Bar']
let {firstName, lastName} = {
firstName: 'Foo',
lastName: 'Bar'
}
const {
title,
firstName,
lastName,
...rest
} = record;
//Object spread
const post = {
...options,
type: "new"
}
//array spread
const users = [
...adminUsers,
...normalUsers
]
function greetings({ name = 'Foo' } = {}) { //Defaulting name to Foo
console.log(`Hello ${name}!`);
}
greet() // Hello Foo
greet({ name: 'Bar' }) // Hi Bar
IF is used to execute a block of code based on a condition.
if(condition){
// code
}
Else part is used to execute the block of code when the condition fails.
if(condition){
// code
} else {
// code
}
Switch is used to replace nested If-Else statements.
switch(condition){
case 'value1' :
//code
[break;]
case 'value2' :
//code
[break;]
.......
default :
//code
[break;]
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
ES6 introduced classes along with OOPS concepts in JS. Class is similar to a function which you can think like kind of template which will get called when ever you initialize class.
class className {
constructor() { ... } //Mandatory Class method
method1() { ... }
method2() { ... }
...
}
class Mobile {
constructor(model) {
this.name = model;
}
}
mbl = new Mobile("iPhone");