Khan Academy project "Bookshelf" is experiencing an issue where the For Loop is not

"This new program showcases an array of books with the use of a loop, allowing multiple books to be displayed in a row."

I am looking to implement the for loop to showcase each book stored in the array. Currently, the 'i' variable remains at 0 and needs to be manually set to 1 to display the next book.

var books = [
{title: "Pride and Prejudice",
stars: 5,
like:true},
{title: "To Kill a Mockingbird",
stars: 4,
like:false}
];

// draw shelf
for (var s = 1; s < 4; s++) {
fill(173, 117, 33);
rect(0, s * 120, width, 10);
}

// draw all books
for (var i = 0; i < books.length; i++) {
var currentBook = books[i]; //accessing individual books from the array
fill(214, 255, 219);
rect(i * 97, 20, 90, 100);
fill(0, 0, 0);
text(currentBook.title, i * 97, 29, 70, 100);
for (var rating = 0; rating < currentBook.stars; rating++) {
    image(getImage("cute/Star"), i * 100 + rating * 19, 90, 18, 30);
}
}

Answer №1

To avoid confusion, it is recommended to use a different variable name for an individual book compared to the book array:

for (var j = 0; j<book.length; j++){
    var bookItem = book[j];        
    fill(214, 255, 219);
    rect(j*97, 20, 90, 100);
    fill(0, 0, 0);
    text(bookItem.title, j*97, 29, 70, 100);
    for (var rating = 0; rating < bookItem.stars; rating++) {
        image(getImage("cute/Star"),j*100+ rating * 19, 90, 18, 30);
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error message thrown by Angular JS: [$injector:modulerr] – error was not caught

I have been working on a weather forecasting web application using AngularJS. Here's a snippet of my code: var myApp = angular.module("myApp", ["ngRoute", "ngResource"]); myApp.config(function($routeProvider){ $routeProvider .when('/',{ ...

Separate the array into subarrays where the total sum equals a specified value, with each relevant element grouped together within the split arrays

Is there a way to split an array by grouping elements that add up to a specified value? For example, if I want to group elements whose sum is less than or equal to 250. Example Array ( [42] => 10 [55] => 20 [56] => 10 [57] => ...

Tips for utilizing AJAX to send data from a specific row

<table> <tr data-id="1"> <input type="text" name="name_1" value="abc"> <input type="text" name="value_1" value="1"> <a href="load_edit_row(this)">Edit</a> </tr> <tr data-id="2"> <input t ...

What is the appropriate exception type to use when attempting to add to an array in a scenario where it is not permitted

In this scenario, imagine a class that implements ArrayAccess but restricts the addition of new data. Existing values can be set, but the size cannot be increased. $arrayAccessible[1] = new StdClass(); //allowed $arrayAccessible[] = new StdClass(); //shou ...

What are some tips for incorporating vue.js into a basic web application?

I've been trying to incorporate vue.js into my web application, but for some reason, it's not loading and the HTML is not firing in the browser. Please take a look at the code snippet below: <!DOCTYPE html> <html> < ...

What is the best way in jQuery to show each element of an array one at a time with a space in an input field?

I have a large array filled with strings that I want to display one by one in a text field, automatically concatenated with a space bar. Below is my code: <script> x = 0; function abc() { for (i = 0; i < words[x].length; i++) { ...

JS code not functioning properly when echoed with PHP outputs

After clicking the send button on my form, an Ajax POST request is triggered. The form is then replaced with a processing graphic while the data is sent to my PHP script for validation. If everything checks out, a thank you message replaces the processin ...

Connecting an admin dashboard to a MERN e-commerce application: A step-by-step guide

In the process of developing an e-commerce platform, I find myself using React.js for the frontend and Node.js/Express.js for the backend. My current challenge lies in creating a seamless dashboard to manage items within the app. One possible solution wo ...

After the submit button is disabled, the form fails to be submitted

Hello, I am having an issue with disabling my button after the form is submitted. The button gets disabled, but the PHP code does not execute. I have tried various scripts from the internet, but they all seem to have the same result: the button gets disab ...

Using Javascript to iterate through an array of images can display a placeholder image as the previous or next image when the loop reaches the beginning or end of the array

https://i.sstatic.net/Hd1N1.png //array of images const programmingLanguages = [jsIcon, htmlIcon, cssIcon, csharpIcon]; Functions for displaying next and previous images. function incrementLanguage() { if (languageIndex + 1 === programmingLangu ...

Can an image map area be identified using radial coordinates?

While I have experience with online Imagemap Generators for rectangular or circular areas in HTML pages, things get a bit tricky when dealing with pie-shaped sections. When trying to make each pie slice a separate area, the image map code generated by thes ...

Cannot locate module using absolute paths in React Native with Typescript

I recently initiated a new project and am currently in the process of setting up an absolute path by referencing this informative article: https://medium.com/geekculture/making-life-easier-with-... Despite closely following the steps outlined, I'm en ...

Guide to importing an external JSON file into a JavaScript-based quiz on an HTML webpage

I am currently diving into the world of JavaScript and trying my hand at creating a quiz. Check out my simple quiz here Here are my specific inquiries: Is there a way to save the questions and answers in an external JSON file? Can I use a different fil ...

Checking for an index in a Java Array

After assigning a value from an int array to a variable using a random index, I am curious if there is a way to determine the index in the array that the value came from. Random rand = new Random(); private int[] cards = {2, 3, 4, 5, 6, 7, 8, 9, 10}; in ...

Execute a function upon mounting of an API endpoint

I have been working with an Express router in my application. I am trying to execute a function when a specific route is mounted. The setup involves two files, index.route.js and currentUser.route.js. index.route.js import currentUserRoutes from './ ...

Exploring the process of passing parameters in Material-UI React styled components

Recently, I developed a new component const CategoryDialog = (props) => { const classes = useStyles(); console.log(props); return ( <div> <Dialog fullScreen open={props.dilaogOpenProp} TransitionCompone ...

unable to gather information from suppliers

I'm currently building a login page with nextjs and spotify js, but I've run into the following error https://i.sstatic.net/cKiM8.png Here is the code snippet that is causing the issue: import React from 'react' import { getProviders ...

What is the best method for making certain rows uneditable in jqgrid?

Can rows be set as uneditable in jqgrid after the first edit is done? I attempted to add a class called not-editable-row but it did not work. This is how I currently make all rows editable: onSelectRow: function(id){ if(id && id!==lastsel){ g ...

Having trouble sending the selected value from a dropdown list to the server using $.ajax

I am embarking on my first project using JQuery (jquery-3.0.0.min.js) and JavaScript. My goal is to build a straightforward web form that connects to a database through a Web API. The form consists of various input text fields and two select dropdowns. ...

Tips for Creating and Verifying JWE in Node.js

I attempted to use the following code in order to create an RSA-OAEP and A128GCM JWE generator and validator. It successfully encrypts claims and generates the JWE, then decrypts it and provides me with the original claims when running on node.js. However, ...