Skip to content

Instantly share code, notes, and snippets.

- [ ] Test your concepts of arrays by determining the location of an item in an array.
```js
const indexOf = (arr, item) => {
// Your code here
}
```
```js
indexOf([1,2,3,4], 3) => 2
indexOf([5,6,7,8], 8) => 3
// All valid credit card numbers
const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9];
const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6];
const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5];
const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6];
// All invalid credit card numbers
const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5];
const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3];
function subLength(str, char){
let count = 0
for(let i = 0; i < str.length; i++){
if(str[i] === char) count++;
}
if(count !== 2) console.log(0)
else console.log (str.slice(str.indexOf(char), str.lastIndexOf(char)+ 1).length)
}
subLength('Saturday', 'a'); // returns 6
subLength('summer', 'm'); // returns 2
function kiemTra(mon1, mon2, mon3, khuVuc, doiTuong, diemChuan){
let totalDiem = mon1 + mon2 + mon3;
if(khuVuc === "A") totalDiem += 2
if(khuVuc === "B") totalDiem += 1
if(khuVuc === "C") totalDiem += 0.5
if(doiTuong === 1) totalDiem += 2.5
if(doiTuong === 2) totalDiem += 1.5
if(doiTuong === 3) totalDiem += 1
if ( mon1 !== 0 && mon2 !== 0 && mon3 !== 0 && totalDiem >= diemChuan)
console.log(`thi sinh duoc tong ${totalDiem} va dau`)
const trips = [
{to:'Brazil',distance:1000},
{to:'Chine',distance: 2500},
{to:'India',distance: 3000}
]
const initialValue = 0;
const sumWithInitial = trips.reduce(
(previousValue, currentValue) => previousValue += currentValue.distance,
initialValue
);
const channels = [
{name:'HBO',premium:true},
{name:'LIFE',premium:false},
{name:'Max',premium:true},
{name:'Cooking channel',premium:false},
{name:'WOBI',premium:false}
];
const preChannel = channels.filter(p => p.premium == true);
console.log(preChannel)
const cars = [
{name:'Ford',price:200},
{name:'Nissan',price:400},
{name:'Nissan',price:600}
]
const str = cars.forEach((value)=> {
console.log(`${value.name} is ${value.price}`);
})
const players = [
{jersey:56,name:"Djounte Murray",position:"Point guard",PPG:2.6},
{jersey:98,name:"Dennis Rodman",position:"Small Forward",PPG:10.8},
{jersey:1,name:"Michael Jordan",position:"Small Forward",PPG:345.6},
{jersey:2,name:"Lebron James",position:"Shooting Guard",PPG:26.7},
{jersey:33,name:"Patrick Ewing",position:"Center",PPG:20.2}
]
const player2 = players.map((key, value) => {
if (key === PPG) return Math.floor(value);
let firstName = "Nguyen";
let lastName = "Nhan";
let age = "24";
let job = "software engineering";
let salary = "100";
console.log(`My name is ${firstName} ${lastName}, i'm ${age} years old. I work as ${company} and make ${salary}`)
// 1
var fizzBuzz = function(n) {
let output = [];
for (let i = 1; i <= n; i++) {
let temp = "";
if (i % 3 == 0 && i % 5 == 0) temp += 'FizzBuzz';
else if (i % 3 == 0) temp += 'Fizz';
else if (i % 5 == 0) temp += 'Buzz';
else temp += i;