Sprint View

Hw Blog

9 min read

Table of Contents

Arrays

Code Runner Challenge

Exercise 1 - Array Basics

View IPYNB Source
%%js

// CODE_RUNNER: Exercise 1 - Array Basics

// TODO: Write your code here for Exercise 1
// Create an array with 5 items
// Print the array
// Print the first element
// Print the last element
// Print the length

// Write your code here for Exercise 1

// Create an array 
let array = ["apple", "banana", "cherry", "orange", "grape"];

// Print the array
console.log("array:", array);

// Print the first element
console.log("First element:", array[0]);

// Print the last element (length - 1)
console.log("Last element:", array[array.length - 1]);

// Print the length
console.log("Length of array:", array.length);



Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Code Runner Challenge

Exercise 2 - Arrays Manipulation

View IPYNB Source
%%js

// CODE_RUNNER: Exercise 2 - Arrays Manipulation

// TODO: Write your code here for Exercise 2
// Start with the shopping list
// Modify it as described above


// TODO: Write your code here for Exercise 2
// Start with the shopping list
// Modify it as described above
// Start with the shopping list
let shoppingList = ["milk", "eggs", "bread", "cheese"];

// Print the  array
console.log("Original list:", shoppingList);

// Change the second item to butter
shoppingList[1] = "butter";

// Add yogurt to the end 
shoppingList.push("yogurt");

// Remove bread
shoppingList.splice(shoppingList.indexOf("bread"), 1);

// Print the array
console.log("Final list:", shoppingList);

Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Code Runner Challenge

Exercise 3 - Arrays Manipulation

View IPYNB Source
%%js

// CODE_RUNNER: Exercise 3 - Arrays Manipulation

// TODO: Write your code here for Exercise 3
// Create the numbers array
// Loop through and print each number
// Print each number multiplied by 2
// Calculate and print the sum


// Creates an array with numbers
let numbers = [10, 25, 30, 15, 20];

// Variable
let sum = 0;

// Loop through array
for (let n = 0; n < numbers.length; n++) {
    // Print each number 
    console.log("Number:", numbers[n]);

    // Print each number multiplied by 2
    console.log("Number * 2 =", numbers[n] * 2);

    // Add the number to the sum
    sum += numbers[n];
}

// Print the total sum of all numbers
console.log("Sum of all numbers:", sum);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Nested Conditionals

Code Runner Challenge

Make a nested conditional homework

View IPYNB Source
%%js

// CODE_RUNNER: Make a nested conditional homework




for (let num = 1; num <= 50; num++) {

    if (num % 5 === 0) {                 
        if (num % 25 === 0) {            
            if (num % 50 === 0) {
                console.log(num + " is divisible by 50, 25, 5, and 1");
            } else {
                console.log(num + " is divisible by 25, 5, and 1");
            }
        } else if (num % 10 === 0) {     
            console.log(num + " is divisible by 10, 5, 2, and 1");
        } else {
            console.log(num + " is divisible by 5 and 1");
        }

    } else if (num % 2 === 0) {          
        console.log(num + " is divisible by 2 and 1");

    } else {
        console.log(num + " is only divisible by 1");
    }
}
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Json

Code Runner Challenge

Create a resume as a JavaScript Object and print to the console as JSON

View IPYNB Source
%%js

// CODE_RUNNER: Create a resume as a JavaScript Object and print to the console as JSON


// 1. Create a JavaScript object named `resume` with the following properties:
const resume = {
    fullName: "Aarnav Jain", // Add your full name
    email: "aarnav0917@gmail.com",    // Add your email
    education: "9th Grade", // Add your grade
    address: {     // Nested object
        city: "San Diego",
        state: "California",
        country: "United States"
    },
    skills: ["math", "tennis", "gaming"]     // Array of strings
};

console.log(resume.fullName);
console.log(resume.email);
console.log(resume.address.city);


const jsonString = JSON.stringify(resume);
console.log(jsonString);


const parsedResume = JSON.parse(jsonString);
console.log(parsedResume);

Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Data Abstraction

Code Runner Challenge

Data Abstraction

View IPYNB Source
%%js

//CODE_RUNNER: Data Abstraction

function calculator(num1, num2, operator) {
    

    let result;

    

    if (operator === "+") {
        result = num1 + num2;
    } else if (operator === "-") {
        result = num1 - num2;
    } else if (operator === "*") {
        result = num1 * num2;
    } else if (operator === "/") {
        result = num1 / num2;
    } else {
        result = "Invalid operator";
    }

   

    return result;
}

console.log(calculator(10, 5, "+"));
console.log(calculator(10, 5, "-"));
console.log(calculator(10, 5, "*"));
console.log(calculator(10, 5, "/"));
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Code Runner Challenge

Data Abstraction

View IPYNB Source
%%js 

//CODE_RUNNER: Data Abstraction

class Pet {
    eat() {
        console.log("Nom nom nom");
    }
}

class Dog extends Pet {
    bark() {
        console.log("Woof woof!");
    }
}
const myDog = new Dog();
myDog.eat(); 
myDog.bark(); 

// Create the Dog class that extends Pet

// We would also like you to create a method like eat() but called bark() that prints "Woof woof!"

// Refer back to the syntax of the classes examples such as phone and smartphone if you need help!

// Write your code here


// Create the Dog class that extends Pet

// We would also like you to create a method like eat() but called bark() that prints "Woof woof!"

// Refer back to the syntax of the classes examples such as phone and smartphone if you need help!

// Write your code here
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Booleans

Code Runner Challenge

Create a function that checks if a number is positive and even

View IPYNB Source
%%js

// CODE_RUNNER: Create a function that checks if a number is positive and even

function isPositiveAndOdd(num) {
    let isPositive = num > 0;
    let isOdd = num % 2 === 1;
    return isPositive && isOdd;
}

console.log(isPositiveAndOdd(8)); // true
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Math

Code Runner Challenge

Homework Problem #1. Create a program that check if a number y is divisible by 5 or not, use modulo in this!

View IPYNB Source
%%js

// CODE_RUNNER: Homework Problem #1. Create a program that check if a number y is divisible by 5 or not, use modulo in this!
// hint: use challenge problem #3 code above to help you!

// hint: use challenge problem #3 code above to help you!

 let y = 5;
     
 if (y % 5 === 0 ) {
    console.log(y + " is divisble by 5");
 } else {
     console.log(y + " is not divisble by 5");
 }
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Code Runner Challenge

Homework Problem #1. Create a program that has three variables, a,b,and sum, which adds them together, use console.log to print the final output.

View IPYNB Source
%%js

// CODE_RUNNER: Homework Problem #1. Create a program that has three variables, a,b,and sum, which adds them together, use console.log to print the final output.
// hint: use challenge problem #2 code above to help you!
// hint: use challenge problem #2 code above to help you!
let a = 2;
let b = 3;
let sum = a + b;
console.log("the sum of " +  a + " + " + b + " is " + sum);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Strings

Code Runner Challenge

Strings

View IPYNB Source
%%js

// CODE_RUNNER: Strings

// Your code here:

 // Three strings 
let string1 = "Hi";
let string2 = "Aarnav";
let string3 = "Programming is fun";

// Print lengths
console.log(string1.length);
console.log(string2.length);
console.log(string3.length);

//  first and last letter
console.log(string1[0], string1[string1.length - 1]);
console.log(string2[0], string2[string2.length - 1]);
console.log(string3[0], string3[string3.length - 1]);

// Sentence using concatenation
let sentence1 = string1 + "! My name is " + string2 + " and " + string3 + ".";
console.log(sentence1);

// Sentence using interpolation
let sentence2 = `${string1}! My name is ${string2} and ${string3}.`;
console.log(sentence2);

Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Iterations

Code Runner Challenge

Iterations

View IPYNB Source
%%js

//CODE_RUNNER: Iterations

// 1. Set the number you want to use here
let inputNumber = 5; 

function calculateMath(n) {
    
    let sum = 0;
    // Loop 1: Adds numbers 1 through n
    for (let i = 1; i <= n; i++) {
        sum += i;
    }

    
    let product = 1;  
    // Loop: Multiplies numbers 1 through n
    for (let j = 1; j <= n; j++) {
        product = product * j;
    }

    // Console log the results 
    console.log("Results for the number: " + n);
    console.log("The total sum is: " + sum);
    console.log("The factorial is: " + product);
}

// 3. Run the function using the input number
calculateMath(inputNumber);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Course Timeline