HTML and Javascript connection [duplicate]

I’m trying to draw a rectangle on a yellow background using javascript but there seems to be something wrong in the connection between two files as there is a blank screen on the browser window.

henry.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="style.css">
        <script type="text/javascript" src="henry.js"></script>
    </head>

    <body>
        <canvas id="canvas" ></canvas>
    </body>
</html>

henry.js

let canvas = document.getElementById("canvas");
let context = canvas.getContext("2d");

var window_height = window.innerHeight;
var window_width = window.innerWidth;

canvas.width = window_width;
canvas.height = window_height;

canvas.style.background="#ff8";

context.fillRect(100,0,100,200);
context.fillRect(100,500,100,200);

Why do I need to click button few times to call one function?

When I call functions in function and have that function render when a button is clicked, I have to click the button for the same number of times as the functions.

 const createResult = e => {
    refine();
    handleColor();
    handleResult();
  };

This functions get rendered when I l click button but I have to click three times and each time I click, it calls refine, handleColor, and then handleResult. So I have to click three times in total.

 <button className="cancel" onClick={createResult}>
            submit
 </button>

I do not really understand how to solve this issue and why this is happening and any idea would be appreciated. Thank you!

Using this in redux initial state? [duplicate]

I want to set certain property of initial state to be relative to other property. So I used this inside of it.

const initialState = {
  canvas_1: {
    canvasName: "canvas_1",
    top: 500,
    left: 600,
    width: 390,
    height: 800,
    xAxisSnap: [0, this.width / 2, this.width],   <-- here
    yAxisSnap: [0, this.height / 2, this.height], <-- here
    selectedShapes: [],
    children: ["hi"],
  },
};

const canvasSlice = createSlice({
  name: "canvas",
  initialState: initialState,
  reducers: {},
});

export const selectShapesOnCanvas = (state) => state.canvas.present.children;

export const {} = canvasSlice.actions;

export default canvasSlice.reducer;

But this is what I got in return from webpack.

var initialState = {
  canvas_1: {
    canvasName: "canvas_1",
    top: 500,
    left: 600,
    width: 390,
    height: 800,
    xAxisSnap: [0, undefined.width / 2, undefined.width],   <-- here
    yAxisSnap: [0, undefined.height / 2, undefined.height], <-- here
    selectedShapes: [],
    children: ["hi"]
  }
};
var canvasSlice = (0,_reduxjs_toolkit__WEBPACK_IMPORTED_MODULE_0__.createSlice)({
  name: "canvas",
  initialState: initialState,
  reducers: {}
});
var selectShapesOnCanvas = function selectShapesOnCanvas(state) {
  return state.canvas.present.children;
};

_objectDestructuringEmpty(canvasSlice.actions);


/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (canvasSlice.reducer);

Am I using this the wrong way, or is it not allowed to use this in RTK?

How to convert this code to functional component Reactjs? [closed]

I’m trying to convert this class component code to functional. Can someone help please?

Code:

import React from "react";
import "./styles.css";

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      BillingAddress: "",
      ShippingAddress: "",
      copybilling: false
    };
  }

  changeDetails = (e) => {
    const { name, value, checked } = e.target;
    if (name === "copybilling") {
      if (checked) {
        this.setState({
          [name]: true,
          ShippingAddress: this.state.BillingAddress
        });
      } else {
        this.setState({
          [name]: false,
          ShippingAddress: ""
        });
      }
    } else {
      this.setState({
        [name]: value
      });
    }
  };

  render() {
    return (
      <div>
        <textarea
          name="BillingAddress"
          value={this.state.BillingAddress}
          onChange={this.changeDetails}
        />
        <textarea
          name="ShippingAddress"
          value={this.state.ShippingAddress}
          onChange={this.changeDetails}
        />
        Copy :
        <input
          type="checkbox"
          name="copybilling"
          checked={this.state.copybilling}
          onChange={this.changeDetails}
        />
      </div>
    );
  }
}

export default App;

rxjs from array execute one by one in same order

I need to execute observables created from array. The condition is next observable should wait until previous observable has been completed. Thanks in advance :-).

export class AppComponent{
    arr: number[] = [5, 4, 1, 2, 3];    
    fetchWithObs() {
        from(this.arr)
          .pipe(
            map((value) => {
              return this.getData(value);
            })
          )
          .subscribe((data) => {
            console.log(data);
            /**
             * The expoected output should be
             * 5 completed
             * 4 completed
             * 1 completed
             * 2 completed
             * 3 completed
             */
          });
      }
    
      getData(p: number) {
        return new Observable<string>((s) => {
          setTimeout(() => {
            s.next(`${p} completed`);
            s.complete();
          }, p * 1000);
        });
      }
}

Delphi, WebView2 – ExecuteScript – get the complete source website

how to use ExecuteScript to get the complete page source – something like browser does with

view-source: https//..any..web..page

If I call ExecuteScript
wbBrowser.ExecuteScript('document.documentElement.outerHTML')
i only get what is displayed on the page. However, there are web pages that display data in a table and currently only display a portion of the records (e.g. 1-10 out of 1000).
By calling
wbBrowser.ExecuteScript('document.documentElement.outerHTML')
I only get the current 1-10, but the browser will display the complete data using view-source: https//****).

Unfortunately, for data protection reasons I can’t show the source of the page. I hope I have described it clearly, sorry for the English

Get all the related posts except the current post using WPGraphQl in reactjs

I’m trying to grab the related posts except the current post using WPGraphQL. My thought is to exclude the ID of the current post and display all related posts within the category. I’m new to GraphQL, I don’t know how to figure out to solve it. is this possible in GraphQL? Please help

query MyQuery {
  category(idType: DATABASE_ID, id: 1) {
    posts {
      edges {
        node {
          id
          title
        }
      }
    }
  }
}

Suggestion to refactor code into simple way -React

I have multiple with the same class name and method with different parameter
i want to refactor the below code to a simpler way any suggestion would be helpful.

<table class="greyGridTable">
  <tbody>
    <tr>
      <td>AA</td>
      <td className = 'table-container'>{formatDate(someMethod1(param1,a)}</td>
    </tr>
    <tr>
      <td>BB</td>
      <td className = 'table-container'>{formatDate(someMethod1(param1,b)}</td>
    </tr>
    <tr>
      <td>CC</td>
      <td className = 'table-container'>{formatDate(someMethod1(param1,c)}</td>
    </tr>
  </tbody>
</table>

I want to refactor the code with the same component.

How to make the checking more robust and shorter?

I wrote this code, but I dont uderstand why it works this way, especially using the third and fourth examples as input. Why the ‘middle’ position remains so behind? -in the number 5 (or index 2) using the [1, 3, 5, 6] array and the number 7 as target??

And how to make it better??
I cant think of a shorter or better way to check the if/elses when the target value is not in the array, especially if the input is an array with only one value and the target to find is 0.
Maybe a better way to check the possible different scenarios.
Or how to better check the correct place of the target without so many if/elses.

For example, is this code good enough to a coding interview? What can I do better?

from LeetCode:

Search Insert Position

Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You must write an algorithm with O(log n) runtime complexity.

Example 1:
Input: nums = [1,3,5,6], target = 5
Output: 2

Example 2:
Input: nums = [1,3,5,6], target = 2
Output: 1

Example 3:
Input: nums = [1,3,5,6], target = 7
Output: 4

And especially this one:
Example 4:
Input: nums=[1], target= 0

Constraints:
1 <= nums.length <= 104
-104 <= nums[i] <= 104
nums contains distinct values sorted in ascending order.
-104 <= target <= 104


this is my code:

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var searchInsert = function(nums, target) {
    let left = 0;
    let right = nums.length -1;
    let middle;
    while(left <= right){
        middle = nums.length>1 ? (Math.floor(left + (right - left)/2)) : 0;
        if(nums[middle] === target){
            return middle;
        } else if(target < nums[middle]){
            right = middle -1;
        } else {
            left = middle + 1;
        }
    }
    
    console.log(`Middle: ${middle}`);
    console.log(`Middle-1: ${nums[middle-1]}`);
    if(nums.lenght === 1){
        return 0;
    } else {
        if((target < nums[middle] && target > nums[middle-1] )|| (target < nums[middle] && nums[middle-1] === undefined)){ /*
No more items to the left ! */
        return middle; 
    } else if(target<nums[middle] && target<nums[middle-1]){
        return middle-1;
    } else if(target > nums[middle] && target > nums[middle + 1]) {
        return middle + 2; /* Why the 'middle' is so behind here? using the THIRD example as input?? */
    } else {
        return middle + 1;
    }
    }
};

rock paper scissors function is not returning the expected output [closed]

hello im trying to do a rock paper scissors program, im just starting and my function should console log test when my computerSelection === playerSelection but im getting some weird results

enter code here

let computerItems = ["rock", "paper", "scissors"];

let computerSelection;

let playerSelection = "paper";

function computerPlay() {

    return computerItems[~~(Math.random() * computerItems.length)];

  }
console.log(computerPlay(computerItems));

function playRound (playerSelection, computerSelection) {
    computerSelection = computerPlay();
    if (computerSelection === playerSelection){
        console.log("test");
    }
}

playRound (playerSelection, computerSelection);

Image extension validation not working with jquery validation

i am working with jquery validation on image field,Right now i want to validate “image” field
i want user can only upload “png,jpg” images only, and my link work if i entered any url in field,How can i do this ?
Right now extension for image and link field not validating properly,Here is my code

<form method="post" action="<?php echo base_url('admin/addbook'); ?>" enctype="multipart/form-data" name="addblog" id="addblog"> 
<input class="form-control validate[required]" type="file" id="file" name="file" >
 <input class="form-control validate[required]" type="text" id="text" name="text">
<input type="submit" name="submit" class="btn btn-primary" value="Submit">  
</form>

Here is my script

  $(document).ready(function () {
     $('#addblog').validate({
        rules: {
            'file': {
              required:true,
              extension: "jpg|png"
            },
            'text': {
              required:true
              }
        },
        messages: {
            file: "Please enter your image",
            
        }
     });
});

React-Intl’s (5.24.8) FormattedDate breaks in production

<FormattedDate
 value={new Date(1656360692499)}
 day="2-digit"
 month="long"
 year="numeric"
/>

The provider looks like that

<IntlProvider
 locale={locale}
 messages={messages}
 textComponent="span"
 defaultRichTextElements={{
  lineBreak: <br />,
  }}
 >
...
</IntlProvider>

The error message is the standard react one: “Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.”

Has it happened to anyone?

Blobs don’t work in latest Safari (MacOS) or any iOS browser

I’ve been trying to use blob URLs instead of revealing the true source and have been able to successfully use this code on desktop: set video objects source file to a blob url

It does not, however, work on any iOS (14.8 and 15.4) browser I’ve tried (Safari, Firefox, Firefox Focus, or Chrome).

Similar code doesn’t work on the latest MacOS Monterey Safari. I don’t own this version of MacOS so I haven’t been able to test the exact linked code yet.

And last I checked, it also doesn’t work on Android. I currently don’t own a working Android device, so maybe someone can confirm or correct me on that.

So far I’ve tried chrome://inspect on my mobile device but no error shows up. Anyone know how to get this to work?