Skip to the content.

Javascript Data Types/Lists • 6 min read

Description

A Tech Talk on javascript data types and how to use with lists

Home HTML Data Types DOM JavaScript JS Debugging

Hacks

Create a JavaScript snippet below with the following requirements:

  • Create an object representing yourself as a person. The object should have keys for your name, age, current classes, interests, and two more of your choosing
  • Your object must contain keys whose values are arrays. The arrays can be arrays of strings, numbers, or even other objects if you would like
  • Print the entire object with console.log after declaring it
  • Manipulate the arrays within the object and print the entire object with console.log as well as the specific changed key afterwards
  • Perform mathematical operations on fields in your object such as +, -, /, % etc. and print the results with console.log along with a message contextualizing them
  • Use typeof to determine the types of at least 3 fields in your object
%%html
<div>
    <input type=text id="nameInput" placeholder="Name">
    <input type="text" id="genderInput" placeholder="Gender">
    <input type="text" id="ageInput" placeholder="Age">
    <input type="text" id="classInput" placeholder="Class">
    <input type="text" id="giftInput" placeholder="Starting Gift">
    <button onclick="updateChar()">
        Choose attribute
    </button>
</div>

<script>
    disable = [" stumbles easily ", " no legs ", " No arms ", " forgets everything ", "Allergic to grass", "Crippling fear of crochet"]
    var character = {
        name: "",
        gender: "",
        class: "",
        StartingGift: "",
        age: null,
        deBuffs: []
    }
    
    function updateChar()
    {
        var nameInput = document.getElementById("nameInput").value;
        var genderInput = document.getElementById("genderInput").value;
        var ageInput = document.getElementById("ageInput").value;
        var classInput = document.getElementById("classInput").value;
        var giftInput = document.getElementById("giftInput").value;
        character.name = nameInput;
        character.gender = genderInput;
        character.age = ageInput; 
        character.class = classInput;
        character.StartingGift = giftInput
        console.log("Here is your character!:")
        console.log(character);
        
        if (character.age < 30)
        {
            console.log("new adventurer?");
            console.log("I'd say you've got about " + (40 - character.age) + " years until you can survive out there alone.")
        }
        else
        {
            console.log("an experienced adventurer? Your looks have aged like fine wine. You don't look a day over " + (character.age - 2) + "!")
        }
        
    }
    for (let i = 0; i < 2; i++){
        character.deBuffs.push(disable[(Math.floor(Math.random() * disable.length))])
    }
    console.log("character gets a fun list of random deBuffs: " + character.deBuffs)
    console.log("lets check out the parts of the object")
    console.log("the age attribute is an " + typeof(character.age))
    console.log("The debuff properties are contained in an " + typeof(character.deBuffs))
    console.log("Your name is a " + typeof(character.name))
</script>