xxxxxxxxxx
function isLGSeven(num) {
if (isNaN(num) || num < 0) {
return "please input a valid number"
}
const minusSeven = num - 7;
if (minusSeven < 7) {
return minusSeven;
} else if (minusSeven >= 7) {
return num * 2;
}
}
const num = 15;
console.log(isLGSeven(num))
//output: 30
xxxxxxxxxx
function gemsToDiamond(friendOne, friendTwo, friendThree) {
if ((isNaN(friendOne) || isNaN(friendTwo) || isNaN(friendThree))) {
return "input a valid number"
}
const firstFriend = friendOne * 21;
const secondFriend = friendTwo * 32;
const thirdFriend = friendThree * 43;
let totalGamsOfFriend = firstFriend + secondFriend + thirdFriend;
if (totalGamsOfFriend >= 1000 * 2) {
const total = totalGamsOfFriend - 2000;
return total
} else {
return totalGamsOfFriend;
}
}
const friendOneGems = 10;
const friendTwoGems = 20;
const friendThreeGems = 30;
console.log(gemsToDiamond(friendOneGems, friendTwoGems, friendThreeGems))
//output: 140
xxxxxxxxxx
// Problem: Find the maximum value in an array
const numbers = [5, 2, 9, 1, 7];
// Approach 1: Using Math.max() and spread operator
const max1 = Math.max(numbers);
console.log(max1); // Output: 9
// Approach 2: Using a loop
let max2 = numbers[0];
for (let i = 1; i < numbers.length; i++) {
if (numbers[i] > max2) {
max2 = numbers[i];
}
}
console.log(max2); // Output: 9
xxxxxxxxxx
function printLinkedList(head) {
let cur=head
while(cur){
console.log(cur.data)
cur=cur.next
}
}