Skip to content

Instantly share code, notes, and snippets.

let a = 5
let b = 10
function swapValue(a, b) {
let temp = a
a = b
b = temp
console.log(a);
console.log(b);
}
// Exercise 1
let input = 2
if (input % 2) {
console.log('odd');
} else {
console.log('even');
}
// Exercise 2
let nums = [1, 3, 5, 21, 56, 2, 7, 11, 15]
let target = 9
function twoSum(nums, target) {
let output = []
for (var i = 0; i < nums.length; i++) {
output.push(i)
output.push(i + 1)
if (nums[output[0]] + nums[output[1]] === target) {
return output
// Sign of the Product of an Array.
var arraySign = function(nums) {
let product = 1
for (let i = 0; i < nums.length; i++) {
product *= nums[i]
}
if (product > 0) {
return 1
} else if (product < 0) {
return -1
const peopleNameArray = ["William", "Collin", "Stephen"];
const showPeopleName = function (name) {
console.log("Hello " + name);
}
peopleNameArray.forEach(showPeopleName)
peopleNameArray.forEach(showPeopleName)
// Challenge 1
const fName = "Vinh"
const lName = "Truong"
const age = 25
const profession = "FE Dev"
const salary = 10
console.log(`My name is ${fName} ${lName}, i'm ${age} years old. I work as ${profession} and make ${salary}$`);
const tinhDiem = (mon1, mon2, mon3, khuVuc, doiTuong, diemChuan) => {
if (mon1 ===0 || mon2 === 0 || mon3 === 0) {
return 'Rớt'
}
let tongDiem = mon1 + mon2 + mon3
if (khuVuc === 'A') {
tongDiem += 2
} else if (khuVuc === 'B') {
tongDiem += 1
} else if (khuVuc === 'C') {
@Merce0897
Merce0897 / index.js
Last active September 24, 2022 03:40
// Test 1
const subLength = (string,occurencies) => {
let indexArray = []
for (let i = 0; i < string.length; i++) {
(string[i] === occurencies) ? indexArray.push(i) : false
}
return (indexArray.length > 2 || indexArray.length < 2) ? 0 : indexArray[1] - indexArray[0] + 1
}
// Test 2
let person = {
first: "Alberto",
last: "Montalesi"
}
const {first , last} = person
console.log(first,last);
const { facebook } = {
...person,
// Exercise 1
const indexOf = (arr, item) => {
let output
if (!arr.includes(item)) { output = -1 }
for (const i in arr) {
(arr[i] === item) ? output = i : false
}
return output
}
console.log(indexOf([1, 2, 3, 4], 3))